Skip to content

Commit

Permalink
Use memize to memoize
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Mar 16, 2022
1 parent 95a03be commit ba1812d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 20 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"colord": "^2.7.0",
"hpq": "^1.3.0",
"lodash": "^4.17.21",
"memize": "^1.1.0",
"rememo": "^3.0.0",
"showdown": "^1.9.1",
"simple-html-tokenizer": "^0.5.7",
Expand Down
21 changes: 1 addition & 20 deletions packages/blocks/src/api/parser/get-block-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { parse as hpqParse } from 'hpq';
import { flow, mapValues, castArray } from 'lodash';
import memoize from 'memize';

/**
* WordPress dependencies
Expand Down Expand Up @@ -181,26 +182,6 @@ export function isValidByEnum( value, enumSet ) {
return ! Array.isArray( enumSet ) || enumSet.includes( value );
}

/**
* Memoize one-parameter function using a WeakMap.
*
* @param {Function} fn Function to memoize.
*
* @return {Function} The same function memoized.
*/
function memoize( fn ) {
const cache = new WeakMap();

return function ( param ) {
let result = cache.get( param );
if ( result === undefined ) {
result = fn( param );
cache.set( param, result );
}
return result;
};
}

/**
* Returns an hpq matcher given a source object.
*
Expand Down

0 comments on commit ba1812d

Please sign in to comment.