UNPKG

3.27 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.memoize = memoize;
7exports.memoizeCompare = memoizeCompare;
8exports.maxArgumentCount = maxArgumentCount;
9
10function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
11
12// function utils
13
14/**
15 * Memoize a given function by caching the computed result.
16 * The cache of a memoized function can be cleared by deleting the `cache`
17 * property of the function.
18 *
19 * @param {function} fn The function to be memoized.
20 * Must be a pure function.
21 * @param {function(args: Array)} [hasher] A custom hash builder.
22 * Is JSON.stringify by default.
23 * @return {function} Returns the memoized function
24 */
25function memoize(fn, hasher) {
26 return function memoize() {
27 if (_typeof(memoize.cache) !== 'object') {
28 memoize.cache = {};
29 }
30
31 var args = [];
32
33 for (var i = 0; i < arguments.length; i++) {
34 args[i] = arguments[i];
35 }
36
37 var hash = hasher ? hasher(args) : JSON.stringify(args);
38
39 if (!(hash in memoize.cache)) {
40 memoize.cache[hash] = fn.apply(fn, args);
41 }
42
43 return memoize.cache[hash];
44 };
45}
46/**
47 * Memoize a given function by caching all results and the arguments,
48 * and comparing against the arguments of previous results before
49 * executing again.
50 * This is less performant than `memoize` which calculates a hash,
51 * which is very fast to compare. Use `memoizeCompare` only when it is
52 * not possible to create a unique serializable hash from the function
53 * arguments.
54 * The isEqual function must compare two sets of arguments
55 * and return true when equal (can be a deep equality check for example).
56 * @param {function} fn
57 * @param {function(a: *, b: *) : boolean} isEqual
58 * @returns {function}
59 */
60
61
62function memoizeCompare(fn, isEqual) {
63 var memoize = function memoize() {
64 var args = [];
65
66 for (var i = 0; i < arguments.length; i++) {
67 args[i] = arguments[i];
68 }
69
70 for (var c = 0; c < memoize.cache.length; c++) {
71 var cached = memoize.cache[c];
72
73 if (isEqual(args, cached.args)) {
74 // TODO: move this cache entry to the top so recently used entries move up?
75 return cached.res;
76 }
77 }
78
79 var res = fn.apply(fn, args);
80 memoize.cache.unshift({
81 args: args,
82 res: res
83 });
84 return res;
85 };
86
87 memoize.cache = [];
88 return memoize;
89}
90/**
91 * Find the maximum number of arguments expected by a typed function.
92 * @param {function} fn A typed function
93 * @return {number} Returns the maximum number of expected arguments.
94 * Returns -1 when no signatures where found on the function.
95 */
96
97
98function maxArgumentCount(fn) {
99 return Object.keys(fn.signatures || {}).reduce(function (args, signature) {
100 var count = (signature.match(/,/g) || []).length + 1;
101 return Math.max(args, count);
102 }, -1);
103}
\No newline at end of file