|
| 1 | + |
| 2 | +export interface MapLike<T> { |
| 3 | + get(key: string): T | undefined; |
| 4 | + has(key: string): boolean; |
| 5 | + set(key: string, file: T); |
| 6 | + delete(key: string); |
| 7 | + forEach<R>(cb: (v: T, key: string) => R); |
| 8 | + map<R>(cb: (v: T, key: string) => R): R[]; |
| 9 | +} |
| 10 | + |
| 11 | +export class CaseSensitiveMap<T> implements MapLike<T> { |
| 12 | + private store = new Map<string, T>(); |
| 13 | + get(key: string) { |
| 14 | + return this.store.get(key); |
| 15 | + } |
| 16 | + delete(key: string) { |
| 17 | + return this.store.delete(key); |
| 18 | + } |
| 19 | + has(key: string) { |
| 20 | + return this.store.has(key); |
| 21 | + } |
| 22 | + set(key: string, file: T) { |
| 23 | + return this.store.set(key, file); |
| 24 | + } |
| 25 | + forEach<R>(cb: (v: T, key: string) => R) { |
| 26 | + this.store.forEach(cb); |
| 27 | + } |
| 28 | + map<R>(cb: (v: T, key: string) => R): R[] { |
| 29 | + const res = [] as R[]; |
| 30 | + this.forEach((v, key) => { |
| 31 | + res.push(cb(v, key)); |
| 32 | + }); |
| 33 | + |
| 34 | + return res; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export class CaseInsensitiveMap<T> implements MapLike<T> { |
| 39 | + private store = new Map<string, T>(); |
| 40 | + get(key: string) { |
| 41 | + return this.store.get(key.toLowerCase()); |
| 42 | + } |
| 43 | + delete(key: string) { |
| 44 | + return this.store.delete(key.toLowerCase()); |
| 45 | + } |
| 46 | + has(key: string) { |
| 47 | + return this.store.has(key.toLowerCase()); |
| 48 | + } |
| 49 | + set(key: string, file: T) { |
| 50 | + return this.store.set(key.toLowerCase(), file); |
| 51 | + } |
| 52 | + forEach<R>(cb: (v: T, key: string) => R) { |
| 53 | + this.store.forEach(cb); |
| 54 | + } |
| 55 | + map<R>(cb: (v: T, key: string) => R): R[] { |
| 56 | + const res = [] as R[]; |
| 57 | + this.forEach((v, key) => { |
| 58 | + res.push(cb(v, key)); |
| 59 | + }); |
| 60 | + |
| 61 | + return res; |
| 62 | + } |
| 63 | +} |
0 commit comments