-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlodash.memoize-test.js
More file actions
65 lines (50 loc) · 1.9 KB
/
lodash.memoize-test.js
File metadata and controls
65 lines (50 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
const assert = require('node:assert/strict');
(async () => {
const memoize = require('lodash.memoize');
assert.equal(typeof memoize, 'function');
// Memoize a simple computation
let computeCount = 0;
const square = memoize((n) => {
computeCount++;
return n * n;
});
assert.equal(square(4), 16);
assert.equal(computeCount, 1, 'first call should compute');
assert.equal(square(4), 16);
assert.equal(computeCount, 1, 'second call with same arg should use cache');
assert.equal(square(5), 25);
assert.equal(computeCount, 2, 'different argument should trigger new computation');
// Verify the cache property exists and works
assert.ok(square.cache instanceof Map || typeof square.cache === 'object',
'memoized function should have a cache property');
assert.equal(square.cache.get(4), 16);
assert.equal(square.cache.get(5), 25);
// Test with a custom resolver
let resolverCount = 0;
const greet = memoize(
(first, last) => {
resolverCount++;
return `Hello ${first} ${last}`;
},
(first, last) => `${first}-${last}`
);
assert.equal(greet('John', 'Doe'), 'Hello John Doe');
assert.equal(resolverCount, 1);
assert.equal(greet('John', 'Doe'), 'Hello John Doe');
assert.equal(resolverCount, 1, 'same args should be cached via resolver key');
assert.equal(greet('Jane', 'Doe'), 'Hello Jane Doe');
assert.equal(resolverCount, 2, 'different args should trigger new call');
// Verify cache uses the resolver key
assert.equal(greet.cache.get('John-Doe'), 'Hello John Doe');
// Test clearing the cache
square.cache.clear();
computeCount = 0;
assert.equal(square(4), 16);
assert.equal(computeCount, 1, 'should recompute after cache clear');
console.log('lodash.memoize-test:ok');
})().catch((err) => {
console.error('lodash.memoize-test:fail');
console.error(err && err.stack ? err.stack : String(err));
process.exit(1);
});