@jiovan-labs/ts-multi-map 0.0.0-stage → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +46 -2
  3. package/README.zh-CN.md +47 -0
  4. package/README.zh-TW.md +47 -0
  5. package/dist/cjs/index.d.ts +7 -0
  6. package/dist/cjs/index.js +47 -0
  7. package/dist/cjs/type/declaration.d.ts +10 -0
  8. package/dist/cjs/type/declaration.js +17 -0
  9. package/dist/cjs/util/MultiKeyMap.d.ts +34 -0
  10. package/dist/cjs/util/MultiKeyMap.js +530 -0
  11. package/dist/cjs/util/MultiValueMap.d.ts +35 -0
  12. package/dist/cjs/util/MultiValueMap.js +527 -0
  13. package/dist/cjs/util/RangeMap.d.ts +36 -0
  14. package/dist/cjs/util/RangeMap.js +584 -0
  15. package/dist/cjs/util/ReadonlyMultiKeyMap.d.ts +27 -0
  16. package/dist/cjs/util/ReadonlyMultiKeyMap.js +364 -0
  17. package/dist/cjs/util/ReadonlyMultiValueMap.d.ts +27 -0
  18. package/dist/cjs/util/ReadonlyMultiValueMap.js +365 -0
  19. package/dist/cjs/util/ReadonlyRangeMap.d.ts +27 -0
  20. package/dist/cjs/util/ReadonlyRangeMap.js +376 -0
  21. package/dist/esm/index.d.ts +7 -0
  22. package/dist/esm/index.js +7 -0
  23. package/dist/esm/type/declaration.d.ts +10 -0
  24. package/dist/esm/type/declaration.js +1 -0
  25. package/dist/esm/util/MultiKeyMap.d.ts +34 -0
  26. package/dist/esm/util/MultiKeyMap.js +318 -0
  27. package/dist/esm/util/MultiValueMap.d.ts +35 -0
  28. package/dist/esm/util/MultiValueMap.js +328 -0
  29. package/dist/esm/util/RangeMap.d.ts +36 -0
  30. package/dist/esm/util/RangeMap.js +358 -0
  31. package/dist/esm/util/ReadonlyMultiKeyMap.d.ts +27 -0
  32. package/dist/esm/util/ReadonlyMultiKeyMap.js +136 -0
  33. package/dist/esm/util/ReadonlyMultiValueMap.d.ts +27 -0
  34. package/dist/esm/util/ReadonlyMultiValueMap.js +136 -0
  35. package/dist/esm/util/ReadonlyRangeMap.d.ts +27 -0
  36. package/dist/esm/util/ReadonlyRangeMap.js +135 -0
  37. package/dist/umd/ts-multi-map.min.js +1 -0
  38. package/package.json +111 -5
@@ -0,0 +1,530 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/util/MultiKeyMap.ts
30
+ var MultiKeyMap_exports = {};
31
+ __export(MultiKeyMap_exports, {
32
+ MultiKeyMap: () => MultiKeyMap
33
+ });
34
+ module.exports = __toCommonJS(MultiKeyMap_exports);
35
+ var import_object_hash = __toESM(require("object-hash"));
36
+ var import__ = require("..");
37
+ var MultiKeyMap = class {
38
+ /**
39
+ * Construct a multi key map instance
40
+ *
41
+ * @param entries the map entries that represented as [K[], V][]
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const map = new MultiKeyMap([
46
+ * [['row1', 'col1'], 'foo']
47
+ * ]);
48
+ * ```
49
+ */
50
+ constructor(entries) {
51
+ this.keyMap = new import__.MultiValueMap();
52
+ this.valueMap = /* @__PURE__ */ new Map();
53
+ entries == null ? void 0 : entries.forEach((entry) => {
54
+ const [ks, v] = entry;
55
+ this.set(ks, v);
56
+ });
57
+ }
58
+ /**
59
+ * Construct a multi key map instance
60
+ *
61
+ * @param entries the map entries that represented as [K[], V][]
62
+ *
63
+ * @returns a multi key map instance
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * const map = MultiKeyMap.of([
68
+ * [['row1', 'col1'], 'foo']
69
+ * ]);
70
+ * ```
71
+ */
72
+ static of(entries) {
73
+ return new MultiKeyMap(entries);
74
+ }
75
+ /**
76
+ * Returns the value of the given keys
77
+ *
78
+ * @param keys the keys to retrieve
79
+ * @param defaults the default value if not found
80
+ *
81
+ * @returns the value of the given keys
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * const map = MultiKeyMap.of([
86
+ * [['row1', 'col1'], 'foo']
87
+ * ]);
88
+ * map.get(['row1', 'col1']); // 'foo'
89
+ * map.get(['row2', 'col2'], 'bar'); // 'bar'
90
+ * ```
91
+ */
92
+ get(keys, defaults) {
93
+ if (keys.length == 0 || this.isEmpty()) {
94
+ return defaults;
95
+ }
96
+ const hash = (0, import_object_hash.default)(keys);
97
+ return this.valueMap.get(hash) ?? defaults;
98
+ }
99
+ /**
100
+ * Sets the value of the given keys
101
+ *
102
+ * @param keys the keys to set
103
+ * @param value the value to set
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * map.set(['row1', 'col1'], 'bar');
108
+ * ```
109
+ */
110
+ set(keys, value) {
111
+ if (keys.length == 0) {
112
+ return;
113
+ }
114
+ const hash = (0, import_object_hash.default)(keys);
115
+ this.keyMap.set(hash, [...keys]);
116
+ this.valueMap.set(hash, value);
117
+ }
118
+ /**
119
+ * Clears the map
120
+ */
121
+ clear() {
122
+ this.keyMap.clear();
123
+ this.valueMap.clear();
124
+ }
125
+ /**
126
+ * Returns the keys array of the map
127
+ *
128
+ * @returns the keys array of the map
129
+ */
130
+ keys() {
131
+ return [...this.keyMap.values()];
132
+ }
133
+ /**
134
+ * Returns the values of the map
135
+ *
136
+ * @returns the values of the map
137
+ */
138
+ values() {
139
+ return [...this.valueMap.values()];
140
+ }
141
+ /**
142
+ * Returns the keys/value entries of the map
143
+ *
144
+ * @returns the keys/value entries of the map
145
+ */
146
+ entries() {
147
+ if (this.isEmpty()) {
148
+ return [];
149
+ }
150
+ if (this.keyMap.size !== this.valueMap.size) {
151
+ throw EvalError(`Internal maps size mismatch!`);
152
+ }
153
+ const result = [];
154
+ for (const [k, v] of this.valueMap) {
155
+ result.push([this.keyMap.get(k) ?? [], v]);
156
+ }
157
+ return result;
158
+ }
159
+ /**
160
+ * Deletes the entry with the given key
161
+ *
162
+ * @param key the key to delete
163
+ *
164
+ * @returns whether the entry has been deleted
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * map.deleteByKey(['row1', 'col1']);
169
+ * ```
170
+ */
171
+ deleteByKey(key) {
172
+ if (!key.length || this.isEmpty()) {
173
+ return false;
174
+ }
175
+ const hash = (0, import_object_hash.default)(key);
176
+ return this.keyMap.deleteByKey(hash) && this.valueMap.delete(hash);
177
+ }
178
+ /**
179
+ * Deletes all the entries with the given keys
180
+ *
181
+ * @param keys the keys to delete
182
+ *
183
+ * @returns whether any of the entries has been deleted
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * map.deleteByKey([['row1', 'col1'], ['row2', 'col2']]);
188
+ * ```
189
+ */
190
+ deleteByKeys(keys) {
191
+ if (!keys.length || this.isEmpty()) {
192
+ return false;
193
+ }
194
+ let result = false;
195
+ for (const key of keys) {
196
+ if (this.deleteByKey(key)) {
197
+ result = true;
198
+ }
199
+ }
200
+ return result;
201
+ }
202
+ /**
203
+ * Deletes the entry/entries with the given value
204
+ *
205
+ * @param value the value to delete
206
+ *
207
+ * @returns whether the entry/entries has been deleted
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * map.deleteByValue('foo');
212
+ * ```
213
+ */
214
+ deleteByValue(value) {
215
+ if (this.isEmpty()) {
216
+ return false;
217
+ }
218
+ let result = false;
219
+ for (const [k, v] of this.valueMap.entries()) {
220
+ if (v === value && this.keyMap.deleteByKey(k) && this.valueMap.delete(k)) {
221
+ result = true;
222
+ }
223
+ }
224
+ return result;
225
+ }
226
+ /**
227
+ * Deletes all the entries with any of the given values
228
+ *
229
+ * @param values the values to delete
230
+ *
231
+ * @returns whether any of the entries has been deleted
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * map.deleteByValues(['foo', 'bar']);
236
+ * ```
237
+ */
238
+ deleteByValues(values) {
239
+ if (!values.length || this.isEmpty()) {
240
+ return false;
241
+ }
242
+ let result = false;
243
+ for (const value of values) {
244
+ if (this.deleteByValue(value)) {
245
+ result = true;
246
+ }
247
+ }
248
+ return result;
249
+ }
250
+ /**
251
+ * Processes each entry in the map
252
+ *
253
+ * @param callback a callback function that processes each entry
254
+ * @param thisArg any instance to retrieve 'this' reference in the callback function
255
+ *
256
+ * @example
257
+ * ```ts
258
+ * map.forEach((value, keys) => {
259
+ * console.log(value);
260
+ * });
261
+ * ```
262
+ */
263
+ forEach(callback, thisArg) {
264
+ this.entries().forEach((entry) => {
265
+ const [ks, v] = entry;
266
+ callback(v, ks);
267
+ }, thisArg);
268
+ }
269
+ /**
270
+ * Processes each entry in the map with index capability
271
+ *
272
+ * @param callback a callback function that processes each entry
273
+ * @param thisArg any instance to retrieve 'this' reference in the callback function
274
+ *
275
+ * @example
276
+ * ```ts
277
+ * map.forEachIndexing((value, keys, index) => {
278
+ * console.log(index);
279
+ * });
280
+ * ```
281
+ */
282
+ forEachIndexing(callback, thisArg) {
283
+ let index = 0;
284
+ this.entries().forEach((entry) => {
285
+ const [ks, v] = entry;
286
+ callback(v, ks, index++);
287
+ }, thisArg);
288
+ }
289
+ /**
290
+ * Processes each entry in the map with breakable capability
291
+ *
292
+ * @param callback a callback function that processes each entry. Returning false indicates to break the map iteration
293
+ * @param thisArg any instance to retrieve 'this' reference in the callback function
294
+ *
295
+ * @example
296
+ * ```ts
297
+ * map.forEachBreakable((value, keys) => {
298
+ * return true;
299
+ * });
300
+ * ```
301
+ */
302
+ forEachBreakable(callback, thisArg) {
303
+ this.entries().forEach((entry) => {
304
+ const [ks, v] = entry;
305
+ if (!callback(v, ks)) {
306
+ return;
307
+ }
308
+ }, thisArg);
309
+ }
310
+ /**
311
+ * Returns whether the map contains the given keys
312
+ *
313
+ * @param keys the keys to check
314
+ * @param exact whether matching entry values exactly
315
+ *
316
+ * @returns whether the map contains the given key
317
+ *
318
+ * @example
319
+ * ```ts
320
+ * map.hasKey(['row1', 'col1']);
321
+ * ```
322
+ */
323
+ hasKey(keys, exact = true) {
324
+ return this.isNotEmpty() && keys.length > 0 && this.keyMap.hasValue(keys, exact);
325
+ }
326
+ /**
327
+ * Returns whether the map contains the given keys/value pair
328
+ *
329
+ * @param keys the keys to check
330
+ * @param value the value to check
331
+ *
332
+ * @returns whether the map contains the given keys/value pair
333
+ *
334
+ * @example
335
+ * ```ts
336
+ * const map = MultiKeyMap.of([
337
+ * [['row1', 'col1'], 'foo']
338
+ * ]);
339
+ * map.hasKeyValue(['row1', 'col1'], 'foo'); // true
340
+ * map.hasKeyValue(['row1', 'col1'], 'bar'); // false
341
+ * ```
342
+ */
343
+ hasKeyValue(keys, value) {
344
+ return this.isNotEmpty() && keys.length > 0 && this.get(keys) === value;
345
+ }
346
+ /**
347
+ * Returns whether the map contains any of the given keys
348
+ *
349
+ * @param keys the keys to check
350
+ * @param exact whether matching entry values exactly
351
+ *
352
+ * @returns whether the map contains any of the given keys
353
+ *
354
+ * @example
355
+ * ```ts
356
+ * const map = MultiKeyMap.of([
357
+ * [['row1', 'col1'], 'foo']
358
+ * ]);
359
+ * map.hasAnyKeys([['row1', 'col1'], ['row2', 'col2']]); // true
360
+ * ```
361
+ */
362
+ hasAnyKeys(keys, exact = true) {
363
+ return this.isNotEmpty() && keys.length > 0 && keys.some((item) => this.hasKey(item, exact));
364
+ }
365
+ /**
366
+ * Returns whether the map contains all the given keys
367
+ *
368
+ * @param keys the keys to check
369
+ * @param exact whether matching entry values exactly
370
+ *
371
+ * @returns whether the map contains all the given keys
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * const map = MultiKeyMap.of([
376
+ * [['row1', 'col1'], 'foo']
377
+ * ]);
378
+ * map.hasAllKeys([['row1', 'col1'], ['row2', 'col2']]); // false
379
+ * ```
380
+ */
381
+ hasAllKeys(keys, exact = true) {
382
+ return this.isNotEmpty() && keys.length > 0 && keys.every((item) => this.hasKey(item, exact));
383
+ }
384
+ /**
385
+ * Returns whether the map contains the given value
386
+ *
387
+ * @param value the value to check
388
+ *
389
+ * @returns whether the map contains the given value
390
+ *
391
+ * @example
392
+ * ```ts
393
+ * const map = MultiKeyMap.of([
394
+ * [['row1', 'col1'], 'foo']
395
+ * ]);
396
+ * map.hasValue('foo'); // true
397
+ * map.hasValue('bar'); // false
398
+ * ```
399
+ */
400
+ hasValue(value) {
401
+ return this.values().includes(value);
402
+ }
403
+ /**
404
+ * Returns whether the map contains any of the given values
405
+ *
406
+ * @param values the values to check
407
+ *
408
+ * @returns whether the map contains any of the given values
409
+ *
410
+ * @example
411
+ * ```ts
412
+ * const map = MultiKeyMap.of([
413
+ * [['row1', 'col1'], 'foo']
414
+ * ]);
415
+ * map.hasAnyValues(['foo', 'bar']); // true
416
+ * ```
417
+ */
418
+ hasAnyValues(values) {
419
+ return this.isNotEmpty() && values.length > 0 && values.some((item) => this.hasValue(item));
420
+ }
421
+ /**
422
+ * Returns whether the map contains all the given values
423
+ *
424
+ * @param values the values to check
425
+ *
426
+ * @returns whether the map contains all the given values
427
+ *
428
+ * @example
429
+ * ```ts
430
+ * map.hasAllValues(['foo']);
431
+ * ```
432
+ */
433
+ hasAllValues(values) {
434
+ return this.isNotEmpty() && values.length > 0 && values.every((item) => this.hasValue(item));
435
+ }
436
+ /**
437
+ * Returns whether the map is empty
438
+ *
439
+ * @returns whether the map is empty
440
+ */
441
+ isEmpty() {
442
+ return !this.valueMap.size;
443
+ }
444
+ /**
445
+ * Returns whether the map is not empty
446
+ *
447
+ * @returns whether the map is not empty
448
+ */
449
+ isNotEmpty() {
450
+ return this.valueMap.size > 0;
451
+ }
452
+ /**
453
+ * Returns the keys with the given value
454
+ *
455
+ * @param value the value to inspect
456
+ * @param defaults the default keys if nothing matches the given value
457
+ *
458
+ * @returns the keys with the given value
459
+ *
460
+ * @example
461
+ * ```ts
462
+ * map.getKey('bar');
463
+ * ```
464
+ */
465
+ getKey(value, defaults) {
466
+ if (this.isEmpty()) {
467
+ return defaults;
468
+ }
469
+ for (const [k, v] of this.valueMap.entries()) {
470
+ if (v === value) {
471
+ return this.keyMap.get(k, defaults);
472
+ }
473
+ }
474
+ return defaults;
475
+ }
476
+ /**
477
+ * Response for returning the list of keys/value to iterate
478
+ *
479
+ * @example
480
+ * ```ts
481
+ * for (const [keys, value] of map) {
482
+ * console.log(value);
483
+ * }
484
+ * ```
485
+ */
486
+ // @ts-ignore
487
+ [Symbol.iterator]() {
488
+ return this.entries()[Symbol.iterator]();
489
+ }
490
+ /**
491
+ * Returns the size of map
492
+ *
493
+ * @returns the size of map
494
+ */
495
+ get size() {
496
+ return this.valueMap.size;
497
+ }
498
+ /**
499
+ * Returns the string representation of the map identifier ('MultiKeyMap')
500
+ *
501
+ * @returns the string representation of the map identifier
502
+ */
503
+ get [Symbol.toStringTag]() {
504
+ return "MultiKeyMap";
505
+ }
506
+ /**
507
+ * Returns the string representation of the map elements
508
+ *
509
+ * @returns the string representation of the map elements
510
+ *
511
+ * @example
512
+ * ```ts
513
+ * const map = MultiKeyMap.of([
514
+ * [['row1', 'col1'], 'foo'],
515
+ * [['row2', 'col2'], 'bar']
516
+ * ]);
517
+ * console.log(map.toString()); // '[row1,col1]:foo;[row2,col2]:bar'
518
+ * ```
519
+ */
520
+ toString() {
521
+ return [...this].map((entry) => {
522
+ const [ks, v] = entry.length <= 1 ? [[], entry[0]] : [entry.slice(0, -1), entry[entry.length - 1]];
523
+ return `[${ks.join()}]:${v}`;
524
+ }).join(";");
525
+ }
526
+ };
527
+ // Annotate the CommonJS export names for ESM import in node:
528
+ 0 && (module.exports = {
529
+ MultiKeyMap
530
+ });
@@ -0,0 +1,35 @@
1
+ import { type MultiValueMapEntries } from "..";
2
+ export declare class MultiValueMap<K, V> implements Omit<Map<K, V[]>, 'delete' | 'forEach' | 'get' | 'has' | 'set' | 'entries' | 'keys' | 'values' | 'push'> {
3
+ private readonly map;
4
+ static of<K, V>(entries?: MultiValueMapEntries<K, V>): MultiValueMap<K, V>;
5
+ constructor(entries?: MultiValueMapEntries<K, V>);
6
+ get(key: K, defaults?: V[]): V[] | undefined;
7
+ set(key: K, values: V[]): void;
8
+ push(key: K, values: V[]): void;
9
+ clear(): void;
10
+ keys(): K[];
11
+ values(): V[][];
12
+ entries(): [K, V[]][];
13
+ deleteByKey(key: K): boolean;
14
+ deleteByKeys(keys: K[]): boolean;
15
+ deleteByValue(value: V): boolean;
16
+ deleteByValues(values: V[]): boolean;
17
+ deleteValueOfKey(key: K, value: V): boolean;
18
+ forEach(callback: (values?: V[], key?: K) => void, thisArg?: any): void;
19
+ forEachIndexing(callback: (values?: V[], key?: K, index?: number) => void, thisArg?: any): void;
20
+ forEachBreakable(callback: (values?: V[], key?: K) => boolean, thisArg?: any): void;
21
+ hasKey(key: K): boolean;
22
+ hasKeyValue(key: K, value: V): boolean;
23
+ hasAnyKeys(keys: K[]): boolean;
24
+ hasAllKeys(keys: K[]): boolean;
25
+ hasValue(values: V[], exact?: boolean): boolean;
26
+ hasAnyValues(values: V[][], exact?: boolean): boolean;
27
+ hasAllValues(values: V[][], exact?: boolean): boolean;
28
+ isEmpty(): boolean;
29
+ isNotEmpty(): boolean;
30
+ getKey(values: V[], defaults?: K): K | undefined;
31
+ [Symbol.iterator](): IterableIterator<[K, V[]]>;
32
+ get size(): number;
33
+ get [Symbol.toStringTag](): string;
34
+ toString(): string;
35
+ }