summaryrefslogtreecommitdiff
path: root/tst/memoize.test.ts
blob: e3d905042cf3c5afcd2891f751a08f0092779c06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { memoize } from '@emprespresso/pengueno';

interface RefCounter {
    inc(by: number): number;
}

test('memoizes', () => {
    let _c = 0;
    const c: RefCounter = {
        inc(by: number): number {
            _c += by;
            return _c;
        },
    };

    const incBy = memoize((n: number) => c.inc(n));

    expect(incBy(1)).toBe(1);
    expect(incBy(2)).toBe(3);
    expect(incBy(20)).toBe(23);
    expect(incBy(20)).toBe(23);
    expect(incBy(1)).toBe(1);
    expect(incBy(2)).toBe(3);
});