Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 15x 15x 7x 1x 1x 15x 1x 3x 2x 3x 2x 2x 2x 2x | import type { ITypeBuilder } from '../typeBuilder';
import { entries, keys, mapRecord, values } from '../utils';
import { Flattable } from './Flattable';
type Entry<Key extends string | Flattable<string>, T> = {
key: Key;
value: T;
};
export class Dictionary<Key extends string | Flattable<string>, T> implements Flattable<Dictionary.Flatten<T>> {
public constructor(
private readonly keyBuilder: ITypeBuilder<string, Key>,
private readonly dictionary: Record<string, T> = {}
) {}
public get(key: Key): T {
return this.dictionary[Flattable.flatten(key)];
}
public getOptional(key: Key): T | null {
if (!this.has(key))
return null;
return this.get(key);
}
public set(key: Key, value: T): void {
this.dictionary[Flattable.flatten(key)] = value;
}
public delete(key: Key): void {
delete this.dictionary[Flattable.flatten(key)];
}
public has(key: Key): boolean {
return Flattable.flatten(key) in this.dictionary;
}
public get keys(): Key[] {
return keys(this.dictionary).map(key => this.keyBuilder.build(key));
}
public get values(): T[] {
return values(this.dictionary);
}
public get entries(): Entry<Key, T>[] {
return entries(this.dictionary).map(({ key, value }) => ({
key: this.keyBuilder.build(key),
value: value
}));
}
public get isEmpty(): boolean {
return this.values.length === 0;
}
public map<U>(callbackFn: (value: T, key: Key) => U): Dictionary<Key, U> {
return new Dictionary(this.keyBuilder, mapRecord(this.dictionary, (value, key) => callbackFn(value, this.keyBuilder.build(key))));
}
public get flatten(): Dictionary.Flatten<T> {
return mapRecord(this.dictionary, value => Flattable.flatten(value));
}
}
// istanbul ignore next
export namespace Dictionary {
export type Flatten<T> = Record<string, Flattable.Flatten<T>>;
export class TypeBuilder<Key extends string | Flattable<string>, T> implements ITypeBuilder<Dictionary.Flatten<T>, Dictionary<Key, T>> {
public constructor(
private readonly keyBuilder: ITypeBuilder<string, Key>,
private readonly builder: ITypeBuilder<Flattable.Flatten<T>, T>
) {}
public build(value: Dictionary.Flatten<T>): Dictionary<Key, T> {
return new Dictionary(this.keyBuilder, mapRecord(value, value => this.builder.build(value)));
}
}
export function builder<Key extends string | Flattable<string>, T>(keyBuilder: ITypeBuilder<string, Key>, builder: ITypeBuilder<Flattable.Flatten<T>, T>): TypeBuilder<Key, T> {
return new TypeBuilder(keyBuilder, builder);
}
}
|