all files / semantic-graphql/src/graph/ memorize.js

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 3/3
1 2 3 4 5 6 7 8 9      88×      
// Caches the result of a function in the graph
// Allows user override
// Makes sure the a (deterministic/pure) function is not called twice with the same args
function memorize(fn, key) {
  return (g, iri) => typeof g[iri][key] !== 'undefined' ? g[iri][key] : g[iri][key] = fn(g, iri);
}
 
module.exports = memorize;