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