Skip to content

Commit

Permalink
Memoize entity encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 10, 2021
1 parent ced65a7 commit f5bae9e
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* @template T
* @param {T} fn
* @returns {T}
*/
function memoize(fn) {
const cache = new Map();
return (arg) => {
let res = cache.get(arg);
if (!res) {
res = fn(arg);
cache.set(arg, res);
}
return res;
};
}

// DOM properties that should NOT have "px" added when numeric
export const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|^--/i;

Expand All @@ -9,10 +26,15 @@ const tagsToReplace = {
'"': '"'
};
const replaceTag = (tag) => tagsToReplace[tag] || tag;
export function encodeEntities(s) {

/**
* @param {any} s
* @returns {string}
*/
export const encodeEntities = memoize((s) => {
if (typeof s !== 'string') s = String(s);
return s.replace(HTML_ENTITY_REG, replaceTag);
}
});

export let indent = (s, char) =>
String(s).replace(/(\n+)/g, '$1' + (char || '\t'));
Expand Down

0 comments on commit f5bae9e

Please sign in to comment.