|
| 1 | +// Class. |
| 2 | +import { Data } from '../lib/data.class'; |
| 3 | +// Abstract. |
| 4 | +import { DataCore } from '../lib/data-core.abstract'; |
| 5 | +// Type. |
| 6 | +import { DataConstructor, MapTypeConstructor } from '../interface'; |
| 7 | +/** |
| 8 | + * @description The abstract core class for building customizable `Map` and `DataCore` related classes. |
| 9 | + * @export |
| 10 | + * @abstract |
| 11 | + * @class CoreMap |
| 12 | + * @template Key |
| 13 | + * @template Value |
| 14 | + * @template {Map<Key, Value>} [MapType=Map<Key, Value>] The type of `Map`. |
| 15 | + * @template {DataCore<MapType>} [DataType=Data<MapType>] The `Data` storage type of `Map` type. |
| 16 | + */ |
| 17 | +export abstract class CoreMap< |
| 18 | + Key, |
| 19 | + Value, |
| 20 | + MapType extends Map<Key, Value> = Map<Key, Value>, |
| 21 | + DataType extends DataCore<MapType> = Data<MapType>, |
| 22 | +> { |
| 23 | + /** |
| 24 | + * @description Returns the `string` tag representation of the `DataMap` class when used in `Object.prototype.toString.call(instance)`. |
| 25 | + * @public |
| 26 | + * @readonly |
| 27 | + */ |
| 28 | + public get [Symbol.toStringTag](): string { |
| 29 | + return CoreMap.name; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @description Returns the privately stored data class. |
| 34 | + * @public |
| 35 | + * @readonly |
| 36 | + * @type {DataType} |
| 37 | + */ |
| 38 | + public get data() { |
| 39 | + return this.#data; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + * @public |
| 45 | + * @readonly |
| 46 | + * @type {number} |
| 47 | + */ |
| 48 | + public get size() { |
| 49 | + return this.#data.value.size; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @description A privately stored data holder of generic type variable `DataType` for the `Map`. |
| 54 | + * @type {DataType} |
| 55 | + */ |
| 56 | + #data: DataType; |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates an instance of `DataMap`. |
| 60 | + * @constructor |
| 61 | + * @param {?[Key, Value][]} [entries] |
| 62 | + * @param {?DataType} [data] |
| 63 | + */ |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates an instance of `CoreMap`. |
| 67 | + * @constructor |
| 68 | + * @param {?[Key, Value][]} [entries] Initial value for `Map`. |
| 69 | + * @param {?MapTypeConstructor<Key, Value, MapType>} [map] The map of generic type variable `MapType` for `Map` value. |
| 70 | + * @param {?DataConstructor<MapType, DataType>} [data] The data store of generic type variable `DataType` for `Map` value. |
| 71 | + */ |
| 72 | + constructor( |
| 73 | + entries?: [Key, Value][], |
| 74 | + map?: MapTypeConstructor<Key, Value, MapType>, |
| 75 | + data?: DataConstructor<MapType, DataType> |
| 76 | + ) { |
| 77 | + this.#data = new (data ?? Data)(new (map ?? Map<Key, Value> as unknown as MapTypeConstructor<Key, Value, MapType>)(entries)) as unknown as DataType; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Clears all entries. |
| 82 | + * @inheritdoc |
| 83 | + * @public |
| 84 | + * @returns {this} |
| 85 | + */ |
| 86 | + public clear(): this { |
| 87 | + this.onClear?.(this.#data); |
| 88 | + this.#data.value.clear(); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Deletes a value from the `key`. |
| 94 | + * @inheritdoc |
| 95 | + * @public |
| 96 | + * @param {Key} key The key to delete. |
| 97 | + * @returns {boolean} |
| 98 | + */ |
| 99 | + public delete(key: Key): boolean { |
| 100 | + return this.#data.value.delete(key); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @inheritdoc |
| 105 | + */ |
| 106 | + public entries(): IterableIterator<[Key, Value]> { |
| 107 | + return this.#data.value.entries(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @inheritdoc |
| 112 | + * @public |
| 113 | + * @param {(value: Value, key: Key, map: Map<Key, Value>) => void} callbackfn |
| 114 | + * @param {?*} [thisArg] |
| 115 | + * @returns {this} |
| 116 | + */ |
| 117 | + public forEach(callbackfn: (value: Value, key: Key, map: Map<Key, Value>) => void, thisArg?: any): this { |
| 118 | + this.#data.value.forEach(callbackfn, thisArg); |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @inheritdoc |
| 124 | + * @public |
| 125 | + * @param {Key} key The key to get the value. |
| 126 | + */ |
| 127 | + public get(key: Key): Value | undefined { |
| 128 | + return this.onGet?.(key, this.#data), this.#data.value.get(key); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @inheritdoc |
| 133 | + * @public |
| 134 | + * @param {Key} key The key to check. |
| 135 | + * @returns {boolean} |
| 136 | + */ |
| 137 | + public has(key: Key): boolean { |
| 138 | + return this.onGet?.(key, this.#data), this.#data.value.has(key); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @inheritdoc |
| 143 | + */ |
| 144 | + public keys(): MapIterator<Key> { |
| 145 | + return this.#data.value.keys(); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @inheritdoc |
| 150 | + * @public |
| 151 | + * @param {Key} key The key under which the `value` set. |
| 152 | + * @param {Value} value The value of `Value` type. |
| 153 | + * @returns {this} The `this` current instance for chaining. |
| 154 | + */ |
| 155 | + public set(key: Key, value: Value): this { |
| 156 | + this.onSet?.(key, value, this.get(key)!, this.#data); |
| 157 | + this.#data.value.set(key, value); |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @inheritdoc |
| 163 | + */ |
| 164 | + public values(): MapIterator<Value> { |
| 165 | + return this.#data.value.values(); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @description Hook called when the `Map` is cleared. |
| 170 | + * @protected |
| 171 | + * @param {DataType} data The data holder. |
| 172 | + */ |
| 173 | + protected onClear(data: DataType): void { } |
| 174 | + |
| 175 | + /** |
| 176 | + * @description Hook called when a value is deleted. |
| 177 | + * @protected |
| 178 | + * @param {DataType} data The data holder. |
| 179 | + */ |
| 180 | + protected onDelete(key: Key, data: DataType): void { } |
| 181 | + |
| 182 | + /** |
| 183 | + * @description Hook called before the `get` being invoked. |
| 184 | + * @protected |
| 185 | + * @param {Key} key The key to get the value. |
| 186 | + * @param {DataType} data The data holder. |
| 187 | + */ |
| 188 | + protected onGet(key: Key, data: DataType): void { } |
| 189 | + |
| 190 | + /** |
| 191 | + * @description Hook called when a value is added. |
| 192 | + * @protected |
| 193 | + * @param {key} key The key under which set the `value`. |
| 194 | + * @param {Type} value The value to set. |
| 195 | + * @param {Type} previousValue The previous value. |
| 196 | + * @param {DataType} data The data holder. |
| 197 | + */ |
| 198 | + protected onSet(key: Key, value: Value, previousValue: Value, data: DataType): void { } |
| 199 | +} |
0 commit comments