1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.memoize = memoize;
|
7 | exports.memoizeCompare = memoizeCompare;
|
8 | exports.maxArgumentCount = maxArgumentCount;
|
9 |
|
10 | 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); }
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function 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 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function 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 |
|
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 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function 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 |