add memoize function and use it for makeEnvironment

This commit is contained in:
Géry Debongnie
2019-01-27 09:23:48 +01:00
parent d68198ad34
commit 0dfa47e405
4 changed files with 60 additions and 5 deletions
+26 -1
View File
@@ -1,4 +1,9 @@
import { escape, htmlTrim, idGenerator } from "../../src/ts/core/utils";
import {
escape,
htmlTrim,
idGenerator,
memoize
} from "../../src/ts/core/utils";
describe("escape", () => {
test("normal strings", () => {
@@ -34,3 +39,23 @@ describe("idGenerator", () => {
expect(gen()).toBe(3);
});
});
describe("memoize", () => {
test("return correct value", () => {
const f = memoize((a, b) => a + b);
expect(f(1, 3)).toBe(4);
});
test("does not recompute if not needed", () => {
let nCalls = 0;
function origFunction(a: number, b: number): number {
nCalls++;
return a + b;
}
const memoized = memoize(origFunction);
expect(memoized(1, 3)).toBe(4);
expect(memoized(1, 3)).toBe(4);
expect(nCalls).toBe(1);
});
});