1 | /*
|
2 | MIT License
|
3 |
|
4 | Copyright (c) 2018-2023 Simon Y. Blackwell
|
5 |
|
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | of this software and associated documentation files (the "Software"), to deal
|
8 | in the Software without restriction, including without limitation the rights
|
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | copies of the Software, and to permit persons to whom the Software is
|
11 | furnished to do so, subject to the following conditions:
|
12 |
|
13 | The above copyright notice and this permission notice shall be included in all
|
14 | copies or substantial portions of the Software.
|
15 |
|
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22 | SOFTWARE.
|
23 | */ var $cf838c15c8b009ba$var$assign = Object.assign;
|
24 | if (typeof $cf838c15c8b009ba$var$assign !== "function") $cf838c15c8b009ba$var$assign = function() {
|
25 | var a = arguments, o = arguments[0];
|
26 | if (o === null || o === undefined) throw new TypeError("Cannot convert undefined or null to object");
|
27 | o = Object(o);
|
28 | for(var i = 1; i < a.length; i++){
|
29 | if (a[i] && typeof a[i] === "object") for(var k in a[i])o[k] = a[i][k];
|
30 | }
|
31 | return o;
|
32 | };
|
33 | function $cf838c15c8b009ba$var$vrgs(f) {
|
34 | var s = f + "", i = s.indexOf("...");
|
35 | return i >= 0 && (i < s.indexOf(")") || s.indexOf("arguments") >= 0);
|
36 | }
|
37 | function $cf838c15c8b009ba$export$22f15dd4e5be7e52(fn, o) {
|
38 | /*o = {
|
39 | serializer, // used to serialize arguments of single argument functions, multis are not serialized
|
40 | equals, // equality tester, will force use of slower multiarg approach even for single arg functions
|
41 | maxAge, // max cache age is ms, set > 0 && < Infinity if you want automatic clearing
|
42 | maxArgs, // max args to use for signature
|
43 | vargs = vrgs(fn) // set to true if function may have variable or beyond-signature arguments, default is best attempt at infering
|
44 | } = {}
|
45 | */ o || (o = {});
|
46 | var vargs = o.vargs || $cf838c15c8b009ba$var$vrgs(fn), s = Object.create(null), k = [], v = [], z, cache = new Map(), d = function(key, c, k) {
|
47 | return setTimeout(function() {
|
48 | if (k) {
|
49 | c.splice(key, 1);
|
50 | k.splice(key, 1);
|
51 | return;
|
52 | } // dealing with single arg function, c is a WekMap or Object
|
53 | c instanceof Map ? c.delete(key) : delete c[key];
|
54 | }, o.maxAge);
|
55 | }, c = o.maxAge > 0 && o.maxAge < Infinity ? d : 0, eq = o.equals ? o.equals : function(a, b) {
|
56 | return a === b;
|
57 | }, maxargs = o.maxArgs, srlz = o.serializer, f, u; // flag indicating a unary arg function is in use for clear operation
|
58 | if (fn.length === 1 && !o.equals && !vargs) {
|
59 | // for single argument functions, just use a Map lookup
|
60 | f = function(a) {
|
61 | if (srlz) a = srlz(a);
|
62 | var r;
|
63 | return cache.get(a) || (!c || c(a, cache), cache.set(a, r = fn.call(this, a)), r);
|
64 | };
|
65 | u = 1;
|
66 | } else // for multiple arg functions, loop through a cache of all the args
|
67 | // looking at each arg separately so a test can abort as soon as possible
|
68 | f = function() {
|
69 | var l = maxargs || arguments.length, kl = k.length, i = -1;
|
70 | while(++i < kl){
|
71 | var args = k[i];
|
72 | if (maxargs != null || args.length === l) {
|
73 | var j = 0;
|
74 | while(j < l && eq(arguments[j], args[j]))j++;
|
75 | if (j === l) return v[i];
|
76 | // the args matched;
|
77 | }
|
78 | }
|
79 | // set change timeout only when new value computed, hits will not push out the tte, but it is arguable they should not
|
80 | return !c || c(i, v, k), v[i] = fn.apply(this, k[i] = arguments);
|
81 | };
|
82 | // reset all the caches
|
83 | f.clear = function() {
|
84 | cache.clear();
|
85 | s = Object.create(null);
|
86 | k = [];
|
87 | v = [];
|
88 | z = undefined;
|
89 | };
|
90 | f.keys = function() {
|
91 | return u ? [
|
92 | ...cache.keys()
|
93 | ] : k.slice();
|
94 | };
|
95 | f.values = function() {
|
96 | return u ? [
|
97 | ...cache.values()
|
98 | ] : v.slice();
|
99 | };
|
100 | return f;
|
101 | }
|
102 |
|
103 |
|
104 | export {$cf838c15c8b009ba$export$22f15dd4e5be7e52 as nanomemoize, $cf838c15c8b009ba$export$22f15dd4e5be7e52 as default};
|
105 | //# sourceMappingURL=index.js.map
|