-
Notifications
You must be signed in to change notification settings - Fork 281
/
19-generic-interfaces-with-functions.solution.ts
55 lines (42 loc) · 1.33 KB
/
19-generic-interfaces-with-functions.solution.ts
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
import { expect, it } from "vitest";
import { Equal, Expect } from "../helpers/type-utils";
export interface Cache<T> {
get: (key: string) => T | undefined;
set: (key: string, value: T) => void;
// You can fix this by only changing the line below!
clone: <U>(transform: (elem: T) => U) => Cache<U>;
}
const createCache = <T>(initialCache?: Record<string, T>): Cache<T> => {
const cache: Record<string, T> = initialCache || {};
return {
get: (key) => cache[key],
set: (key, value) => {
cache[key] = value;
},
clone: (transform) => {
const newCache: Record<string, any> = {};
for (const key in cache) {
newCache[key] = transform(cache[key]);
}
return createCache(newCache);
},
};
};
it("Should let you get and set to/from the cache", () => {
const cache = createCache<number>();
cache.set("a", 1);
cache.set("b", 2);
expect(cache.get("a")).toEqual(1);
expect(cache.get("b")).toEqual(2);
});
it("Should let you clone the cache using a transform function", () => {
const numberCache = createCache<number>();
numberCache.set("a", 1);
numberCache.set("b", 2);
const stringCache = numberCache.clone((elem) => {
return String(elem);
});
const a = stringCache.get("a");
expect(a).toEqual("1");
type tests = [Expect<Equal<typeof a, string | undefined>>];
});