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