@gkucmierz/utils 1.18.0 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.mjs +5 -1
- package/package-lock.json +2 -2
- package/package.json +1 -1
- package/spec/memoize.spec.mjs +32 -0
- package/src/memoize.mjs +38 -0
package/main.mjs
CHANGED
|
@@ -29,6 +29,9 @@ import {
|
|
|
29
29
|
import {
|
|
30
30
|
lcm, lcmBI
|
|
31
31
|
} from './src/lcm.mjs'
|
|
32
|
+
import {
|
|
33
|
+
memoize
|
|
34
|
+
} from './src/memoize.mjs'
|
|
32
35
|
import {
|
|
33
36
|
mod, modBI
|
|
34
37
|
} from './src/mod.mjs'
|
|
@@ -58,6 +61,7 @@ export * from './src/get-type.mjs';
|
|
|
58
61
|
export * from './src/gpn.mjs';
|
|
59
62
|
export * from './src/herons-formula.mjs';
|
|
60
63
|
export * from './src/lcm.mjs';
|
|
64
|
+
export * from './src/memoize.mjs';
|
|
61
65
|
export * from './src/mod.mjs';
|
|
62
66
|
export * from './src/phi.mjs';
|
|
63
67
|
export * from './src/pow-mod.mjs';
|
|
@@ -66,5 +70,5 @@ export * from './src/square-root.mjs';
|
|
|
66
70
|
export * from './src/tonelli-shanks.mjs';
|
|
67
71
|
|
|
68
72
|
export default [
|
|
69
|
-
SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, heronsFormula, heronsFormulaBI, lcm, lcmBI, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
|
|
73
|
+
SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, heronsFormula, heronsFormulaBI, lcm, lcmBI, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
|
|
70
74
|
];
|
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.19.0",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.19.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"husky": "^8.0.1",
|
package/package.json
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
memoize,
|
|
4
|
+
} from '../src/memoize.mjs';
|
|
5
|
+
|
|
6
|
+
describe('memoize', () => {
|
|
7
|
+
it('called once', () => {
|
|
8
|
+
let cnt = 0;
|
|
9
|
+
const add = memoize((a, b) => {
|
|
10
|
+
++cnt;
|
|
11
|
+
return a + b;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
expect(add(1, 2)).toBe(3);
|
|
15
|
+
expect(cnt).toBe(1);
|
|
16
|
+
expect(add(1, 2)).toBe(3);
|
|
17
|
+
expect(cnt).toBe(1);
|
|
18
|
+
|
|
19
|
+
expect(add(2, 1)).toBe(3);
|
|
20
|
+
expect(cnt).toBe(2);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('variable args length', () => {
|
|
24
|
+
const fn = memoize((...args) => args.length);
|
|
25
|
+
|
|
26
|
+
expect(fn()).toBe(0);
|
|
27
|
+
expect(fn(1)).toBe(1);
|
|
28
|
+
expect(fn(1, 2)).toBe(2);
|
|
29
|
+
expect(fn(1, 2, 3)).toBe(3);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
package/src/memoize.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
export const memoize = fn => {
|
|
3
|
+
const maps = [];
|
|
4
|
+
|
|
5
|
+
const getLeafMap = args => {
|
|
6
|
+
if (!maps[args.length]) maps[args.length] = new Map();
|
|
7
|
+
const map = maps[args.length];
|
|
8
|
+
|
|
9
|
+
const leaf = args.reduce((map, arg) => {
|
|
10
|
+
if (map.has(arg)) return map.get(arg);
|
|
11
|
+
const tmp = new Map();
|
|
12
|
+
map.set(arg, tmp);
|
|
13
|
+
return tmp;
|
|
14
|
+
}, map);
|
|
15
|
+
|
|
16
|
+
return leaf;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const zero = {
|
|
20
|
+
called: false,
|
|
21
|
+
ret: null,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return function() {
|
|
25
|
+
const args = [...arguments];
|
|
26
|
+
if (args.length === 0) {
|
|
27
|
+
if (zero.called) return zero.ret;
|
|
28
|
+
zero.called = true;
|
|
29
|
+
return zero.ret = fn();
|
|
30
|
+
}
|
|
31
|
+
const [firstArg, ...restArgs] = args;
|
|
32
|
+
const map = getLeafMap(restArgs);
|
|
33
|
+
if (map.has(firstArg)) return map.get(firstArg);
|
|
34
|
+
const ret = fn(...args);
|
|
35
|
+
map.set(firstArg, ret);
|
|
36
|
+
return ret;
|
|
37
|
+
};
|
|
38
|
+
};
|