-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtest.ts
More file actions
1124 lines (934 loc) · 28.5 KB
/
Copy pathtest.ts
File metadata and controls
1124 lines (934 loc) · 28.5 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import test from 'ava';
import delay from 'delay';
import serializeJavascript from 'serialize-javascript';
import memoize, {memoizeDecorator, memoizeClear, memoizeIsCached} from './index.js';
test('memoize', t => {
let index = 0;
const fixture = (a?: unknown, b?: unknown) => index++;
const memoized = memoize(fixture);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(undefined), 0);
t.is(memoized(undefined), 0);
t.is(memoized('foo'), 1);
t.is(memoized('foo'), 1);
t.is(memoized('foo'), 1);
t.is(memoized('foo', 'bar'), 1);
t.is(memoized('foo', 'bar'), 1);
t.is(memoized('foo', 'bar'), 1);
t.is(memoized(1), 2);
t.is(memoized(1), 2);
t.is(memoized(null), 3);
t.is(memoized(null), 3);
t.is(memoized(fixture), 4);
t.is(memoized(fixture), 4);
t.is(memoized(true), 5);
t.is(memoized(true), 5);
// Ensure that functions are stored by reference and not by "value" (e.g. their `.toString()` representation)
t.is(memoized(() => index++), 6);
t.is(memoized(() => index++), 7);
});
test('cacheKey option', t => {
let index = 0;
const fixture = (..._arguments: any) => index++;
const memoized = memoize(fixture, {cacheKey: ([firstArgument]) => String(firstArgument)});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(memoized('1'), 0);
t.is(memoized('2'), 1);
t.is(memoized(2), 1);
});
test('memoize with multiple non-primitive arguments', t => {
let index = 0;
const memoized = memoize((a?: unknown, b?: unknown, c?: unknown) => index++, {cacheKey: JSON.stringify});
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized({foo: true}, {bar: false}), 1);
t.is(memoized({foo: true}, {bar: false}), 1);
t.is(memoized({foo: true}, {bar: false}, {baz: true}), 2);
t.is(memoized({foo: true}, {bar: false}, {baz: true}), 2);
});
test('memoize with regexp arguments', t => {
let index = 0;
const memoized = memoize((a?: unknown) => index++, {cacheKey: serializeJavascript});
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(/Sindre Sorhus/v), 1);
t.is(memoized(/Sindre Sorhus/v), 1);
t.is(memoized(/Elvin Peng/v), 2);
t.is(memoized(/Elvin Peng/v), 2);
});
test('memoize with Symbol arguments', t => {
let index = 0;
const argument1 = Symbol('fixture1');
const argument2 = Symbol('fixture2');
const memoized = memoize((a?: unknown) => index++);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(argument1), 1);
t.is(memoized(argument1), 1);
t.is(memoized(argument2), 2);
t.is(memoized(argument2), 2);
});
test('maxAge option', async t => {
let index = 0;
const fixture = (a?: unknown) => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
await delay(50);
t.is(memoized(1), 0);
await delay(200);
t.is(memoized(1), 1);
});
test('maxAge option deletes old items', async t => {
let index = 0;
const fixture = (a?: unknown) => index++;
const cache = new Map<number, number>();
const deleted: number[] = [];
const _delete = cache.delete.bind(cache);
cache.delete = item => {
deleted.push(item);
return _delete(item);
};
const memoized = memoize(fixture, {maxAge: 100, cache});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.true(cache.has(1));
await delay(50);
t.is(memoized(1), 0);
t.is(deleted.length, 0);
await delay(200);
t.is(memoized(1), 1);
t.is(deleted.length, 1);
t.is(deleted[0], 1);
});
test('maxAge items are deleted even if function throws', async t => {
let index = 0;
const fixture = (a?: unknown) => {
if (index === 1) {
throw new Error('failure');
}
return index++;
};
const cache = new Map();
const memoized = memoize(fixture, {maxAge: 100, cache});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(cache.size, 1);
await delay(50);
t.is(memoized(1), 0);
await delay(200);
t.throws(() => {
memoized(1);
}, {message: 'failure'});
t.is(cache.size, 0);
});
test('cache option', t => {
let index = 0;
const fixture = (..._arguments: any) => index++;
const memoized = memoize(fixture, {
cache: new WeakMap(),
cacheKey: <ReturnValue>([firstArgument]: [ReturnValue]): ReturnValue => firstArgument,
});
const foo = {};
const bar = {};
t.is(memoized(foo), 0);
t.is(memoized(foo), 0);
t.is(memoized(bar), 1);
t.is(memoized(bar), 1);
});
test('promise support', async t => {
let index = 0;
const memoized = memoize(async (a?: unknown) => index++);
t.is(await memoized(), 0);
t.is(await memoized(), 0);
t.is(await memoized(10), 1);
});
test('preserves the original function name', t => {
t.is(memoize(function foo() {}).name, 'foo'); // eslint-disable-line func-names, @stylistic/curly-newline, @typescript-eslint/no-empty-function
});
test('.clear()', t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture);
t.is(memoized(), 0);
t.is(memoized(), 0);
memoizeClear(memoized);
t.is(memoized(), 1);
t.is(memoized(), 1);
});
test('prototype support', t => {
class Unicorn {
index = 0;
foo() {
return this.index++;
}
}
Unicorn.prototype.foo = memoize(Unicorn.prototype.foo);
const unicorn = new Unicorn();
t.is(unicorn.foo(), 0);
t.is(unicorn.foo(), 0);
t.is(unicorn.foo(), 0);
});
test('memoizeDecorator()', t => {
let returnValue = 1;
const returnValue2 = 101;
class TestClass {
@memoizeDecorator()
counter() {
return returnValue++;
}
@memoizeDecorator()
counter2() {
return returnValue2;
}
}
const alpha = new TestClass();
t.is(alpha.counter(), 1);
t.is(alpha.counter(), 1, 'The method should be memoized');
t.is(alpha.counter2(), 101, 'The method should be memoized separately from the other one');
const beta = new TestClass();
t.is(beta.counter(), 2, 'The method should not be memoized across instances');
});
test('memoizeDecorator() does not call getters during decoration', t => {
class TestClass {
isInitialized = true;
@memoizeDecorator()
get value() {
if (!this.isInitialized) {
throw new Error('constructor has not been called');
}
return Math.random();
}
}
const instance = new TestClass();
const {value} = instance;
t.is(instance.value, value);
});
test('memoizeDecorator() applies options to getters', async t => {
class TestClass {
value = 0;
@memoizeDecorator({maxAge: 50})
get nextValue() {
this.value++;
return this.value;
}
}
const instance = new TestClass();
t.is(instance.nextValue, 1);
t.is(instance.nextValue, 1);
await delay(70);
t.is(instance.nextValue, 2);
});
test('memoizeDecorator() memoizes getters per instance', t => {
class TestClass {
value = 0;
@memoizeDecorator()
get nextValue() {
this.value++;
return this.value;
}
}
const firstInstance = new TestClass();
const secondInstance = new TestClass();
t.is(firstInstance.nextValue, 1);
t.is(firstInstance.nextValue, 1);
t.is(secondInstance.nextValue, 1);
t.is(secondInstance.nextValue, 1);
});
test('memoizeDecorator() works with static methods', t => {
let value = 0;
class BaseClass {
value = 0;
@memoizeDecorator()
static nextValue(key: string) {
value++;
return `${this.name}-${key}-${value}`;
}
}
class ChildClass extends BaseClass {} // eslint-disable-line @stylistic/curly-newline
t.false(Object.hasOwn(ChildClass, 'nextValue'));
t.is(BaseClass.nextValue('a'), 'BaseClass-a-1');
t.is(BaseClass.nextValue('a'), 'BaseClass-a-1');
t.true(memoizeIsCached(BaseClass.nextValue, 'a'));
t.is(ChildClass.nextValue('a'), 'ChildClass-a-2');
t.is(ChildClass.nextValue('a'), 'ChildClass-a-2');
t.true(memoizeIsCached(ChildClass.nextValue, 'a'));
t.true(Object.hasOwn(ChildClass, 'nextValue'));
t.false(Object.prototype.propertyIsEnumerable.call(ChildClass, 'nextValue'));
memoizeClear(BaseClass.nextValue);
t.is(BaseClass.nextValue('a'), 'BaseClass-a-3');
t.is(ChildClass.nextValue('a'), 'ChildClass-a-2');
});
test('memoizeDecorator() keeps static method caches isolated per receiver', t => {
let value = 0;
class BaseClass {
value = 0;
@memoizeDecorator()
static nextValue(key: string) {
value++;
return `${this.name}-${key}-${value}`;
}
}
class ChildClass extends BaseClass {} // eslint-disable-line @stylistic/curly-newline
t.not(BaseClass.nextValue, ChildClass.nextValue);
t.is(BaseClass.nextValue('a'), 'BaseClass-a-1');
t.is(ChildClass.nextValue('a'), 'ChildClass-a-2');
t.true(memoizeIsCached(BaseClass.nextValue, 'a'));
t.true(memoizeIsCached(ChildClass.nextValue, 'a'));
memoizeClear(ChildClass.nextValue);
t.true(memoizeIsCached(BaseClass.nextValue, 'a'));
t.false(memoizeIsCached(ChildClass.nextValue, 'a'));
t.is(BaseClass.nextValue('a'), 'BaseClass-a-1');
t.is(ChildClass.nextValue('a'), 'ChildClass-a-3');
});
test('memoizeDecorator() preserves late binding for deep static inheritance', t => {
let value = 0;
class BaseClass {
value = 0;
@memoizeDecorator()
static nextValue(key: string) {
value++;
return `${this.name}-${key}-${value}`;
}
}
class ChildClass extends BaseClass {} // eslint-disable-line @stylistic/curly-newline
class GrandChildClass extends ChildClass {} // eslint-disable-line @stylistic/curly-newline
t.is(ChildClass.nextValue('a'), 'ChildClass-a-1');
t.is(ChildClass.nextValue('a'), 'ChildClass-a-1');
t.is(GrandChildClass.nextValue('a'), 'GrandChildClass-a-2');
t.is(GrandChildClass.nextValue('a'), 'GrandChildClass-a-2');
t.true(memoizeIsCached(ChildClass.nextValue, 'a'));
t.true(memoizeIsCached(GrandChildClass.nextValue, 'a'));
});
test('memoizeDecorator() preserves static overrides that call super', t => {
class BaseClass {
static count = 0;
value = 0;
@memoizeDecorator()
static nextValue() {
this.count ??= 0;
this.count++;
return `${this.name}-${this.count}`;
}
}
class ChildClass extends BaseClass {
static count = 0;
static override nextValue() {
return `child:${super.nextValue()}`;
}
}
class GrandChildClass extends ChildClass {
static count = 0;
}
t.is(ChildClass.nextValue(), 'child:ChildClass-1');
t.is(ChildClass.nextValue(), 'child:ChildClass-1');
t.is(GrandChildClass.nextValue(), 'child:GrandChildClass-1');
t.is(GrandChildClass.nextValue(), 'child:GrandChildClass-1');
t.is(ChildClass.count, 1);
t.is(GrandChildClass.count, 1);
});
test('memoizeDecorator() installs instance methods as non-enumerable own properties', t => {
let value = 0;
class TestClass {
@memoizeDecorator()
nextValue() {
value++;
return value;
}
}
const instance = new TestClass();
t.true(Object.hasOwn(instance, 'nextValue'));
const methodBeforeCall = instance.nextValue;
t.is(instance.nextValue(), 1);
t.is(instance.nextValue(), 1);
t.true(Object.hasOwn(instance, 'nextValue'));
t.is(instance.nextValue, methodBeforeCall);
const descriptor = Object.getOwnPropertyDescriptor(instance, 'nextValue');
t.truthy(descriptor);
t.false(descriptor?.enumerable);
t.true(descriptor?.configurable);
t.true(descriptor?.writable);
});
test('memoizeDecorator() keeps instance method caches isolated per instance', t => {
let value = 0;
class TestClass {
@memoizeDecorator()
nextValue(key: string) {
value++;
return `${key}-${value}`;
}
}
const firstInstance = new TestClass();
const secondInstance = new TestClass();
t.not(firstInstance.nextValue, secondInstance.nextValue);
t.is(firstInstance.nextValue('a'), 'a-1');
t.is(secondInstance.nextValue('a'), 'a-2');
t.true(memoizeIsCached(firstInstance.nextValue, 'a'));
t.true(memoizeIsCached(secondInstance.nextValue, 'a'));
memoizeClear(firstInstance.nextValue);
t.false(memoizeIsCached(firstInstance.nextValue, 'a'));
t.true(memoizeIsCached(secondInstance.nextValue, 'a'));
t.is(firstInstance.nextValue('a'), 'a-3');
t.is(secondInstance.nextValue('a'), 'a-2');
});
test('memoizeDecorator() memoizes inherited methods per instance', t => {
let value = 0;
class BaseClass {
@memoizeDecorator()
nextValue() {
value++;
return `${this.constructor.name}-${value}`;
}
}
class ChildClass extends BaseClass {} // eslint-disable-line @stylistic/curly-newline
const baseInstance = new BaseClass();
const childInstance = new ChildClass();
t.true(Object.hasOwn(baseInstance, 'nextValue'));
t.true(Object.hasOwn(childInstance, 'nextValue'));
t.is(baseInstance.nextValue(), 'BaseClass-1');
t.is(baseInstance.nextValue(), 'BaseClass-1');
t.is(childInstance.nextValue(), 'ChildClass-2');
t.is(childInstance.nextValue(), 'ChildClass-2');
t.true(Object.hasOwn(childInstance, 'nextValue'));
});
test('memoizeDecorator() preserves subclass overrides', t => {
let baseValue = 0;
let childValue = 0;
class BaseClass {
@memoizeDecorator()
value() {
baseValue++;
return `base-${baseValue}`;
}
}
class ChildClass extends BaseClass {
override value() {
childValue++;
return `child-${childValue}`;
}
}
const baseInstance = new BaseClass();
t.is(baseInstance.value(), 'base-1');
t.is(baseInstance.value(), 'base-1');
const childInstance = new ChildClass();
t.is(childInstance.value(), 'child-1');
t.is(childInstance.value(), 'child-2');
});
test('memoizeDecorator() does not touch overridden accessors during super()', t => {
class BaseClass {
@memoizeDecorator()
value() {
return 'base';
}
}
class ChildClass extends BaseClass {
isInitialized = false;
constructor() {
super();
this.isInitialized = true;
}
override get value() {
if (!this.isInitialized) {
throw new Error('constructor has not finished');
}
return () => 'child';
}
}
const instance = new ChildClass();
t.is(instance.value(), 'child');
});
test('memoizeDecorator() keeps extracted methods bound to their instance', t => {
class TestClass {
count = 0;
@memoizeDecorator()
nextValue(key: string) {
this.count++;
return `${this.count}-${key}`;
}
}
const firstInstance = new TestClass();
const secondInstance = new TestClass();
const {nextValue} = firstInstance;
t.is(nextValue('a'), '1-a');
t.is(nextValue('a'), '1-a');
t.is(firstInstance.count, 1);
t.is(secondInstance.count, 0);
});
test('memoizeDecorator() keeps extracted static methods bound to their class', t => {
class BaseClass {
static count = 0;
value = 0;
@memoizeDecorator()
static nextValue(key: string) {
this.count ??= 0;
this.count++;
return `${this.name}-${this.count}-${key}`;
}
}
class ChildClass extends BaseClass {
static count = 0;
}
const {nextValue} = BaseClass;
t.is(nextValue('a'), 'BaseClass-1-a');
t.is(nextValue('a'), 'BaseClass-1-a');
t.is(BaseClass.count, 1);
t.is(ChildClass.count, 0);
});
test('memoizeDecorator() utilities work with extracted instance methods', t => {
class TestClass {
count = 0;
@memoizeDecorator()
nextValue(key: string) {
this.count++;
return `${this.count}-${key}`;
}
}
const firstInstance = new TestClass();
const secondInstance = new TestClass();
const {nextValue} = firstInstance;
t.is(nextValue('a'), '1-a');
t.is(secondInstance.nextValue('a'), '1-a');
t.true(memoizeIsCached(nextValue, 'a'));
t.true(memoizeIsCached(secondInstance.nextValue, 'a'));
memoizeClear(nextValue);
t.false(memoizeIsCached(nextValue, 'a'));
t.true(memoizeIsCached(secondInstance.nextValue, 'a'));
t.is(nextValue('a'), '2-a');
t.is(secondInstance.nextValue('a'), '1-a');
});
test('memoizeDecorator() utilities work with extracted static methods', t => {
class BaseClass {
static count = 0;
value = 0;
@memoizeDecorator()
static nextValue(key: string) {
this.count ??= 0;
this.count++;
return `${this.name}-${this.count}-${key}`;
}
}
class ChildClass extends BaseClass {} // eslint-disable-line @stylistic/curly-newline
const {nextValue} = ChildClass;
t.is(nextValue('a'), 'ChildClass-1-a');
t.is(BaseClass.nextValue('a'), 'BaseClass-1-a');
t.true(memoizeIsCached(nextValue, 'a'));
t.true(memoizeIsCached(BaseClass.nextValue, 'a'));
memoizeClear(nextValue);
t.false(memoizeIsCached(nextValue, 'a'));
t.true(memoizeIsCached(BaseClass.nextValue, 'a'));
t.is(nextValue('a'), 'ChildClass-2-a');
t.is(BaseClass.nextValue('a'), 'BaseClass-1-a');
});
test('memoizeDecorator() rejects private methods', t => {
t.throws(() => {
class TestClass {
value() {
return this.#nextValue();
}
@memoizeDecorator()
#nextValue() {
return Math.random();
}
}
return new TestClass().value();
}, {
message: 'The `memoizeDecorator` method decorator does not support private methods.',
instanceOf: TypeError,
});
});
test('memoizeDecorator() keeps methods off enumerable instance keys', t => {
class TestClass {
value = 1;
@memoizeDecorator()
nextValue() {
return this.value;
}
}
const instance = new TestClass();
t.is(instance.nextValue(), 1);
t.deepEqual(Object.keys(instance), ['value']);
t.deepEqual({...instance}, {value: 1});
t.false(Object.prototype.propertyIsEnumerable.call(instance, 'nextValue'));
});
test('memoizeDecorator() applies cacheKey to methods', t => {
let value = 0;
class TestClass {
@memoizeDecorator({cacheKey: arguments_ => arguments_.join(':')})
nextValue(first: string, second: string) {
value++;
return `${first}-${second}-${value}`;
}
}
const instance = new TestClass();
t.is(instance.nextValue('a', 'b'), 'a-b-1');
t.is(instance.nextValue('a', 'b'), 'a-b-1');
t.true(memoizeIsCached(instance.nextValue, 'a', 'b'));
t.false(memoizeIsCached(instance.nextValue, 'b', 'a'));
t.is(instance.nextValue('b', 'a'), 'b-a-2');
});
test('memoizeDecorator() applies maxAge to methods per instance', async t => {
let value = 0;
class TestClass {
@memoizeDecorator({maxAge: 50})
nextValue(key: string) {
value++;
return `${key}-${value}`;
}
}
const firstInstance = new TestClass();
const secondInstance = new TestClass();
t.is(firstInstance.nextValue('a'), 'a-1');
t.is(firstInstance.nextValue('a'), 'a-1');
t.is(secondInstance.nextValue('a'), 'a-2');
t.is(secondInstance.nextValue('a'), 'a-2');
await delay(70);
t.is(firstInstance.nextValue('a'), 'a-3');
t.is(secondInstance.nextValue('a'), 'a-4');
});
test('memoizeDecorator() supports non-cached getters with maxAge 0', t => {
class TestClass {
value = 0;
@memoizeDecorator({maxAge: 0})
get nextValue() {
this.value++;
return this.value;
}
}
const instance = new TestClass();
t.is(instance.nextValue, 1);
t.is(instance.nextValue, 2);
});
test('memoizeClear() works with memoizeDecorator()', t => {
let value = 0;
class TestClass {
@memoizeDecorator()
nextValue() {
value++;
return value;
}
}
const instance = new TestClass();
t.is(instance.nextValue(), 1);
t.is(instance.nextValue(), 1);
memoizeClear(instance.nextValue);
t.is(instance.nextValue(), 2);
});
test('memoizeIsCached() works with memoizeDecorator()', t => {
let value = 0;
class TestClass {
@memoizeDecorator()
nextValue(key: string) {
value++;
return `${key}-${value}`;
}
}
const instance = new TestClass();
t.false(memoizeIsCached(instance.nextValue, 'a'));
t.is(instance.nextValue('a'), 'a-1');
t.true(memoizeIsCached(instance.nextValue, 'a'));
t.false(memoizeIsCached(instance.nextValue, 'b'));
});
test('memoizeClear() throws when called with a plain function', t => {
t.throws(() => {
memoizeClear(() => {}); // eslint-disable-line @stylistic/curly-newline, @typescript-eslint/no-empty-function
}, {
message: 'Can\'t clear a function that was not memoized!',
instanceOf: TypeError,
});
});
test('memoizeClear() throws when called on an unclearable cache', t => {
const fixture = () => 1;
const memoized = memoize(fixture, {
cache: new WeakMap(),
});
t.throws(() => {
memoizeClear(memoized);
}, {
message: 'The cache Map can\'t be cleared!',
instanceOf: TypeError,
});
});
test('maxAge - cache item expires after specified duration', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized(), 0); // Initial call, cached
t.is(memoized(), 0); // Subsequent call, still cached
await delay(150); // Wait for longer than maxAge
t.is(memoized(), 1); // Cache expired, should compute again
});
test('maxAge - cache expiration timing is accurate', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized(), 0);
await delay(90); // Wait for slightly less than maxAge
t.is(memoized(), 0); // Should still be cached
await delay(20); // Total delay now exceeds maxAge
t.is(memoized(), 1); // Should recompute as cache has expired
});
test('maxAge - expired items are not present in cache', async t => {
let index = 0;
const fixture = () => index++;
const cache = new Map();
const memoized = memoize(fixture, {maxAge: 100, cache});
memoized(); // Call to cache the result
await delay(150); // Wait for cache to expire
memoized(); // Recompute and recache
t.is(cache.size, 1); // Only one item should be in the cache
});
test('maxAge - complex arguments and cache expiration', async t => {
let index = 0;
const fixture = object => index++;
const memoized = memoize(fixture, {maxAge: 100, cacheKey: JSON.stringify});
const argument = {key: 'value'};
t.is(memoized(argument), 0);
await delay(150);
t.is(memoized(argument), 1); // Argument is the same, but should recompute due to expiration
});
test('maxAge - concurrent calls return cached value', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized(), 0);
await delay(50); // Delay less than maxAge
t.is(memoized(), 0); // Should return cached value
});
test('maxAge - different arguments have separate expirations', async t => {
let index = 0;
const fixture = x => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized('a'), 0);
await delay(150); // Expire the cache for 'a'
t.is(memoized('b'), 1); // 'b' should be a separate cache entry
t.is(memoized('a'), 2); // 'a' should be recomputed
});
test('memoizeIsCached() checks if arguments are cached', t => {
let index = 0;
const fixture = (a?: unknown) => index++;
const memoized = memoize(fixture);
t.false(memoizeIsCached(memoized, 1));
memoized(1);
t.true(memoizeIsCached(memoized, 1));
t.false(memoizeIsCached(memoized, 2));
memoized(2);
t.true(memoizeIsCached(memoized, 2));
t.true(memoizeIsCached(memoized, 1));
});
test('memoizeIsCached() works with custom cacheKey', t => {
let index = 0;
const fixture = (a?: unknown, b?: unknown) => index++;
const memoized = memoize(fixture, {cacheKey: JSON.stringify});
t.false(memoizeIsCached(memoized, 1, 2));
memoized(1, 2);
t.true(memoizeIsCached(memoized, 1, 2));
t.false(memoizeIsCached(memoized, 1, 3));
t.false(memoizeIsCached(memoized, 2, 1));
});
test('memoizeIsCached() with maxAge', async t => {
let index = 0;
const fixture = (a?: unknown) => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.false(memoizeIsCached(memoized, 1));
memoized(1);
t.true(memoizeIsCached(memoized, 1));
await delay(150);
t.false(memoizeIsCached(memoized, 1));
});
test('memoizeIsCached() when maxAge is 0', t => {
let index = 0;
const fixture = (a?: unknown) => index++;
const memoized = memoize(fixture, {maxAge: 0});
memoized(1);
t.false(memoizeIsCached(memoized, 1));
memoized(2);
t.false(memoizeIsCached(memoized, 2));
});
test('memoizeIsCached() returns false for non-memoized functions', t => {
const fixture = (a?: unknown) => a;
t.false(memoizeIsCached(fixture, 1));
});
test('memoizeIsCached() handles same function memoized with different options', t => {
let index = 0;
const fixture = (a?: unknown, b?: unknown) => index++;
// Memoize the same function twice with different cache keys
const memoized1 = memoize(fixture);
const memoized2 = memoize(fixture, {cacheKey: JSON.stringify});
memoized1(1, 2);
memoized2(1, 2);
// Default cacheKey only considers first argument
t.true(memoizeIsCached(memoized1, 1, 2));
t.true(memoizeIsCached(memoized1, 1, 3)); // Still cached (same first argument)
// JSON.stringify considers all arguments
t.true(memoizeIsCached(memoized2, 1, 2));
t.false(memoizeIsCached(memoized2, 1, 3)); // Not cached (different arguments)
});
test('maxAge - zero maxAge means no caching', t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 0});
t.is(memoized(), 0);
t.is(memoized(), 1); // No caching, should increment
});
test('maxAge - negative number disables caching', t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: -1});
t.is(memoized(), 0);
t.is(memoized(), 1);
t.false(memoizeIsCached(memoized));
});
test('maxAge - immediate expiration', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 1});
t.is(memoized(), 0);
await delay(10);
t.is(memoized(), 1); // Cache should expire immediately
});
test('maxAge - high concurrency', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 50});
// Simulate concurrent calls
for (let job = 0; job < 10_000; job++) {
memoized();
}
await delay(100);
t.is(memoized(), 1);
});
test('maxAge dependent on function parameters', async t => {
let index = 0;
const fixture = (x: number) => index++;
const memoized = memoize(fixture, {
maxAge: x => x * 100,
});
t.is(memoized(1), 0); // Initial call, cached
await delay(50);
t.is(memoized(1), 0); // Still cached
await delay(60);
t.is(memoized(1), 1); // Cache expired, should compute again
t.is(memoized(2), 2); // Initial call with different parameter, cached
await delay(210);
t.is(memoized(2), 3); // Cache expired, should compute again
});
test('maxAge function returning 0 disables caching', t => {
let index = 0;
const fixture = (value?: unknown) => index++;
const memoized = memoize(fixture, {
maxAge() {
return 0;
},
});
t.is(memoized('a'), 0);