@bemoje/map 1.0.1 → 1.2.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 (66) hide show
  1. package/lib/ExtMap.cjs +354 -0
  2. package/lib/ExtMap.d.cts +110 -0
  3. package/lib/ExtMap.d.ts +110 -0
  4. package/lib/ExtMap.js +352 -0
  5. package/lib/TimeoutWeakMap.cjs +122 -0
  6. package/lib/TimeoutWeakMap.d.cts +75 -0
  7. package/lib/TimeoutWeakMap.d.ts +75 -0
  8. package/lib/TimeoutWeakMap.js +120 -0
  9. package/lib/countUniques.cjs +363 -0
  10. package/lib/countUniques.d.cts +9 -0
  11. package/lib/countUniques.d.ts +9 -0
  12. package/lib/countUniques.js +361 -0
  13. package/lib/entriesArray.cjs +12 -0
  14. package/lib/entriesArray.d.cts +19 -0
  15. package/lib/entriesArray.d.ts +19 -0
  16. package/lib/entriesArray.js +10 -0
  17. package/lib/index.cjs +505 -0
  18. package/lib/index.d.cts +17 -0
  19. package/lib/index.d.ts +17 -0
  20. package/{index.mjs → lib/index.js} +104 -90
  21. package/lib/isGenericMap.cjs +18 -0
  22. package/lib/isGenericMap.d.cts +6 -0
  23. package/lib/isGenericMap.d.ts +6 -0
  24. package/lib/isGenericMap.js +16 -0
  25. package/lib/keysArray.cjs +12 -0
  26. package/lib/keysArray.d.cts +19 -0
  27. package/lib/keysArray.d.ts +19 -0
  28. package/lib/keysArray.js +10 -0
  29. package/lib/mapGetOrDefault.cjs +18 -0
  30. package/lib/mapGetOrDefault.d.cts +8 -0
  31. package/lib/mapGetOrDefault.d.ts +8 -0
  32. package/lib/mapGetOrDefault.js +16 -0
  33. package/lib/mapLoad.cjs +15 -0
  34. package/lib/mapLoad.d.cts +11 -0
  35. package/lib/mapLoad.d.ts +11 -0
  36. package/lib/mapLoad.js +13 -0
  37. package/lib/mapReverse.cjs +24 -0
  38. package/lib/mapReverse.d.cts +15 -0
  39. package/lib/mapReverse.d.ts +15 -0
  40. package/lib/mapReverse.js +22 -0
  41. package/lib/mapUpdate.cjs +14 -0
  42. package/lib/mapUpdate.d.cts +13 -0
  43. package/lib/mapUpdate.d.ts +13 -0
  44. package/lib/mapUpdate.js +12 -0
  45. package/lib/sort.cjs +17 -0
  46. package/lib/sort.d.cts +25 -0
  47. package/lib/sort.d.ts +25 -0
  48. package/lib/sort.js +15 -0
  49. package/lib/sortByKeys.cjs +19 -0
  50. package/lib/sortByKeys.d.cts +24 -0
  51. package/lib/sortByKeys.d.ts +24 -0
  52. package/lib/sortByKeys.js +17 -0
  53. package/lib/sortByValues.cjs +19 -0
  54. package/lib/sortByValues.d.cts +25 -0
  55. package/lib/sortByValues.d.ts +25 -0
  56. package/lib/sortByValues.js +17 -0
  57. package/lib/toMap.cjs +12 -0
  58. package/lib/toMap.d.cts +20 -0
  59. package/lib/toMap.d.ts +20 -0
  60. package/lib/toMap.js +10 -0
  61. package/lib/valuesArray.cjs +12 -0
  62. package/lib/valuesArray.d.cts +19 -0
  63. package/lib/valuesArray.d.ts +19 -0
  64. package/lib/valuesArray.js +10 -0
  65. package/package.json +248 -18
  66. package/index.d.ts +0 -364
package/lib/index.cjs ADDED
@@ -0,0 +1,505 @@
1
+ 'use strict';
2
+
3
+ var iterTools = require('iter-tools');
4
+ var util = require('util');
5
+ var mnemonist = require('mnemonist');
6
+
7
+ var __defProp = Object.defineProperty;
8
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
+
10
+ // ../object/src/defineMethod.ts
11
+ function defineMethod(obj, key, value, des = {}) {
12
+ return Object.defineProperty(obj, key, {
13
+ value,
14
+ configurable: true,
15
+ writable: true,
16
+ enumerable: false,
17
+ ...des
18
+ });
19
+ }
20
+ __name(defineMethod, "defineMethod");
21
+
22
+ // ../object/src/entriesOf.ts
23
+ function entriesOf(obj) {
24
+ return Object.entries(obj);
25
+ }
26
+ __name(entriesOf, "entriesOf");
27
+
28
+ // ../composition/src/View.ts
29
+ var TARGET_SYMBOL = /* @__PURE__ */ Symbol("target");
30
+ var View = class {
31
+ static {
32
+ __name(this, "View");
33
+ }
34
+ [TARGET_SYMBOL];
35
+ constructor(target) {
36
+ this[TARGET_SYMBOL] = target;
37
+ }
38
+ get [Symbol.toStringTag]() {
39
+ return this.constructor.name;
40
+ }
41
+ get target() {
42
+ return this[TARGET_SYMBOL];
43
+ }
44
+ };
45
+
46
+ // ../composition/src/inheritProxifiedPrototypeProperty.ts
47
+ function inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, key) {
48
+ const des = Reflect.getOwnPropertyDescriptor(TargetClass.prototype, key);
49
+ if (!des) {
50
+ return;
51
+ }
52
+ if (des.value && typeof des.value === "function") {
53
+ return Object.defineProperty(ViewerClass.prototype, key, {
54
+ value(...args) {
55
+ return this.target[key](...args);
56
+ },
57
+ writable: true,
58
+ configurable: true,
59
+ enumerable: false
60
+ });
61
+ } else if (des && !des.get && !des.set) {
62
+ return Object.defineProperty(ViewerClass.prototype, key, {
63
+ get() {
64
+ return this.target[key];
65
+ },
66
+ set: des.writable ? /* @__PURE__ */ __name(function set(value) {
67
+ this.target[key] = value;
68
+ }, "set") : void 0,
69
+ configurable: true,
70
+ enumerable: false
71
+ });
72
+ } else {
73
+ Object.defineProperty(ViewerClass.prototype, key, {
74
+ get: des.get ? function() {
75
+ return this.target[key];
76
+ } : void 0,
77
+ set: des.set ? function(value) {
78
+ this.target[key] = value;
79
+ } : void 0,
80
+ configurable: true,
81
+ enumerable: false
82
+ });
83
+ }
84
+ }
85
+ __name(inheritProxifiedPrototypeProperty, "inheritProxifiedPrototypeProperty");
86
+
87
+ // ../composition/src/inheritProxifiedPrototype.ts
88
+ function inheritProxifiedPrototype(ViewerClass, TargetClass, omitKeys) {
89
+ Reflect.ownKeys(TargetClass.prototype).filter((k) => {
90
+ return k !== "constructor" && k !== Symbol.toStringTag;
91
+ }).filter((k) => {
92
+ return omitKeys ? !omitKeys.includes(k) : true;
93
+ }).forEach((k) => {
94
+ return inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, k);
95
+ });
96
+ return ViewerClass;
97
+ }
98
+ __name(inheritProxifiedPrototype, "inheritProxifiedPrototype");
99
+
100
+ // src/entriesArray.ts
101
+ function entriesArray(ins) {
102
+ return Array.from(ins);
103
+ }
104
+ __name(entriesArray, "entriesArray");
105
+ function filterIterableValues(mapLike, predicate) {
106
+ return iterTools.filter(([k, v]) => {
107
+ return predicate(v, k);
108
+ }, mapLike);
109
+ }
110
+ __name(filterIterableValues, "filterIterableValues");
111
+
112
+ // src/keysArray.ts
113
+ function keysArray(ins) {
114
+ return Array.from(ins.keys());
115
+ }
116
+ __name(keysArray, "keysArray");
117
+
118
+ // src/mapGetOrDefault.ts
119
+ function mapGetOrDefault(map3, key, factory) {
120
+ let value = map3.get(key);
121
+ if (value !== void 0 || map3.has(key)) {
122
+ return value;
123
+ }
124
+ value = factory(key, map3);
125
+ map3.set(key, value);
126
+ return value;
127
+ }
128
+ __name(mapGetOrDefault, "mapGetOrDefault");
129
+ function mapIterableKeys(mapLike, mapper) {
130
+ return iterTools.map(([k, v]) => {
131
+ return [
132
+ mapper(k, v),
133
+ v
134
+ ];
135
+ }, mapLike);
136
+ }
137
+ __name(mapIterableKeys, "mapIterableKeys");
138
+ function mapIterableValues(mapLike, mapper) {
139
+ return iterTools.map(([k, v]) => {
140
+ return [
141
+ k,
142
+ mapper(v, k)
143
+ ];
144
+ }, mapLike);
145
+ }
146
+ __name(mapIterableValues, "mapIterableValues");
147
+
148
+ // src/mapLoad.ts
149
+ function mapLoad(ins, entries) {
150
+ for (const [key, value] of entries) {
151
+ ins.set(key, value);
152
+ }
153
+ return ins;
154
+ }
155
+ __name(mapLoad, "mapLoad");
156
+
157
+ // src/mapReverse.ts
158
+ function mapReverse(ins) {
159
+ const entries = Array.from(ins).reverse();
160
+ ins.clear();
161
+ mapLoad(ins, entries);
162
+ return ins;
163
+ }
164
+ __name(mapReverse, "mapReverse");
165
+
166
+ // src/mapUpdate.ts
167
+ function mapUpdate(ins, key, update) {
168
+ const newValue = update(ins.get(key), key, ins);
169
+ ins.set(key, newValue);
170
+ return ins;
171
+ }
172
+ __name(mapUpdate, "mapUpdate");
173
+
174
+ // ../iter/src/reduceIterableValues.ts
175
+ function reduceIterableValues(mapLike, reducer, initial) {
176
+ let accumulator = initial;
177
+ for (const [k, v] of mapLike) {
178
+ accumulator = reducer(accumulator, v, k);
179
+ }
180
+ return accumulator;
181
+ }
182
+ __name(reduceIterableValues, "reduceIterableValues");
183
+
184
+ // src/sort.ts
185
+ function sort(ins, compare) {
186
+ const entries = Array.from(ins).sort(compare);
187
+ ins.clear();
188
+ for (const [k, v] of entries) {
189
+ ins.set(k, v);
190
+ }
191
+ return ins;
192
+ }
193
+ __name(sort, "sort");
194
+
195
+ // src/sortByKeys.ts
196
+ function sortByKeys(ins, compare) {
197
+ const entries = Array.from(ins).sort((a, b) => {
198
+ return compare(a[0], b[0]);
199
+ });
200
+ ins.clear();
201
+ for (const [k, v] of entries) {
202
+ ins.set(k, v);
203
+ }
204
+ return ins;
205
+ }
206
+ __name(sortByKeys, "sortByKeys");
207
+
208
+ // src/sortByValues.ts
209
+ function sortByValues(ins, compare) {
210
+ const entries = Array.from(ins).sort((a, b) => {
211
+ return compare(a[1], b[1]);
212
+ });
213
+ ins.clear();
214
+ for (const [k, v] of entries) {
215
+ ins.set(k, v);
216
+ }
217
+ return ins;
218
+ }
219
+ __name(sortByValues, "sortByValues");
220
+
221
+ // ../fn/src/setNameAndLength.ts
222
+ function setNameAndLength(source, target, lengthDelta = 0) {
223
+ return Object.defineProperties(target, {
224
+ name: {
225
+ configurable: true,
226
+ value: source.name
227
+ },
228
+ length: {
229
+ configurable: true,
230
+ value: source.length + lengthDelta
231
+ }
232
+ });
233
+ }
234
+ __name(setNameAndLength, "setNameAndLength");
235
+
236
+ // ../fn/src/thisify.ts
237
+ function thisify(fn) {
238
+ return setNameAndLength(fn, /* @__PURE__ */ __name(function f(...args) {
239
+ return fn(this, ...args);
240
+ }, "f"), -1);
241
+ }
242
+ __name(thisify, "thisify");
243
+
244
+ // src/toMap.ts
245
+ function toMap(ins) {
246
+ return new Map(ins);
247
+ }
248
+ __name(toMap, "toMap");
249
+
250
+ // ../iter/src/toObjectIterable.ts
251
+ function toObjectIterable(mapLike) {
252
+ const object = {};
253
+ for (const [key, value] of mapLike) {
254
+ if (typeof key !== "string" && typeof key !== "number" && typeof key !== "symbol") {
255
+ object[String(key)] = value;
256
+ } else {
257
+ object[key] = value;
258
+ }
259
+ }
260
+ return object;
261
+ }
262
+ __name(toObjectIterable, "toObjectIterable");
263
+
264
+ // ../fn/src/transformReturnValue.ts
265
+ function transformReturnValue(fn, transform) {
266
+ return setNameAndLength(fn, function(...args) {
267
+ const result = fn.apply(this, args);
268
+ return transform(result);
269
+ });
270
+ }
271
+ __name(transformReturnValue, "transformReturnValue");
272
+
273
+ // src/valuesArray.ts
274
+ function valuesArray(ins) {
275
+ return Array.from(ins.values());
276
+ }
277
+ __name(valuesArray, "valuesArray");
278
+
279
+ // src/ExtMap.ts
280
+ var ExtMap = class _ExtMap extends View {
281
+ static {
282
+ __name(this, "ExtMap");
283
+ }
284
+ constructor(arg) {
285
+ super(new Map(iterTools.isIterable(arg) ? arg : arg ? entriesOf(arg) : void 0));
286
+ }
287
+ /**
288
+ * Creates a new ExtMap instance from a regular object.
289
+ *
290
+ * @param obj - An object whose key-value pairs will be used to initialize the map
291
+ * @returns A new ExtMap instance containing the entries from the object
292
+ */
293
+ static fromObject(obj) {
294
+ return new _ExtMap(Object.entries(obj));
295
+ }
296
+ /**
297
+ * Creates a new ExtMap instance from an iterable of key-value pairs.
298
+ *
299
+ * @param entries - An iterable of key-value pairs to initialize the map
300
+ * @returns A new ExtMap instance containing the provided entries
301
+ */
302
+ static fromIterable(entries) {
303
+ return new _ExtMap(entries);
304
+ }
305
+ /**
306
+ * Creates a shallow copy of the map.
307
+ *
308
+ * @returns A new ExtMap instance with the same entries
309
+ */
310
+ clone() {
311
+ return new _ExtMap(this);
312
+ }
313
+ /**
314
+ * Converts the map to a JSON-serializable format.
315
+ *
316
+ * @returns Array of key-value pairs suitable for JSON serialization
317
+ */
318
+ toJSON() {
319
+ return [
320
+ ...this.entries()
321
+ ];
322
+ }
323
+ /**
324
+ * Custom inspection method for better debugging output.
325
+ *
326
+ * @param _depth - The depth of inspection
327
+ * @param _options - Options for inspection
328
+ * @returns A string representation of the map
329
+ */
330
+ [util.inspect.custom](_depth, _options) {
331
+ return util.inspect(this.target, {
332
+ breakLength: 50
333
+ });
334
+ }
335
+ };
336
+ inheritProxifiedPrototype(ExtMap, Map, []);
337
+ defineMethod(ExtMap.prototype, "load", thisify(mapLoad));
338
+ defineMethod(ExtMap.prototype, "sort", thisify(sort));
339
+ defineMethod(ExtMap.prototype, "sortByKeys", thisify(sortByKeys));
340
+ defineMethod(ExtMap.prototype, "sortByValues", thisify(sortByValues));
341
+ defineMethod(ExtMap.prototype, "reverse", thisify(mapReverse));
342
+ defineMethod(ExtMap.prototype, "update", thisify(mapUpdate));
343
+ defineMethod(ExtMap.prototype, "getOrDefault", thisify(mapGetOrDefault));
344
+ defineMethod(ExtMap.prototype, "keysArray", thisify(keysArray));
345
+ defineMethod(ExtMap.prototype, "valuesArray", thisify(valuesArray));
346
+ defineMethod(ExtMap.prototype, "entriesArray", thisify(entriesArray));
347
+ defineMethod(ExtMap.prototype, "toObject", thisify(toObjectIterable));
348
+ defineMethod(ExtMap.prototype, "toMap", thisify(toMap));
349
+ defineMethod(ExtMap.prototype, "reduce", thisify(reduceIterableValues));
350
+ defineMethod(ExtMap.prototype, "map", thisify(transformReturnValue(mapIterableValues, ExtMap.fromIterable)));
351
+ defineMethod(ExtMap.prototype, "mapKeys", thisify(transformReturnValue(mapIterableKeys, ExtMap.fromIterable)));
352
+ defineMethod(ExtMap.prototype, "mapValues", thisify(transformReturnValue(mapIterableValues, ExtMap.fromIterable)));
353
+ defineMethod(ExtMap.prototype, "filter", thisify(transformReturnValue(filterIterableValues, ExtMap.fromIterable)));
354
+
355
+ // src/TimeoutWeakMap.ts
356
+ var TimeoutWeakMap = class {
357
+ static {
358
+ __name(this, "TimeoutWeakMap");
359
+ }
360
+ timeoutMs;
361
+ wmap = /* @__PURE__ */ new WeakMap();
362
+ /**
363
+ * @param timeoutMs Default timeout in milliseconds for entries
364
+ */
365
+ constructor(timeoutMs) {
366
+ this.timeoutMs = timeoutMs;
367
+ }
368
+ /**
369
+ * Retrieves the value for the given key and refreshes its timeout.
370
+ *
371
+ * @param key The key to retrieve
372
+ * @returns The value if found, undefined otherwise
373
+ */
374
+ get(key) {
375
+ const vt = this.wmap.get(key);
376
+ if (!vt) {
377
+ return void 0;
378
+ }
379
+ const [value, timeout] = vt;
380
+ timeout.refresh();
381
+ return value;
382
+ }
383
+ /**
384
+ * Sets a key-value pair with an optional custom timeout.
385
+ *
386
+ * @param key The key to set
387
+ * @param value The value to associate with the key
388
+ * @param timeoutMs Optional timeout override for this entry
389
+ * @returns This instance for chaining
390
+ */
391
+ set(key, value, timeoutMs) {
392
+ this.wmap.set(key, [
393
+ value,
394
+ setTimeout(() => {
395
+ this.delete(key);
396
+ }, timeoutMs ?? this.timeoutMs).unref()
397
+ ]);
398
+ return this;
399
+ }
400
+ /**
401
+ * Removes a key-value pair and clears its timeout.
402
+ *
403
+ * @param key The key to delete
404
+ * @returns True if the key existed and was deleted, false otherwise
405
+ */
406
+ delete(key) {
407
+ const vt = this.wmap.get(key);
408
+ if (!vt) {
409
+ return false;
410
+ }
411
+ const timeout = vt[1];
412
+ clearTimeout(timeout);
413
+ return this.wmap.delete(key);
414
+ }
415
+ /**
416
+ * Checks if a key exists in the map.
417
+ * Does not refresh the timeout.
418
+ *
419
+ * @param key The key to check
420
+ * @returns True if the key exists, false otherwise
421
+ */
422
+ has(key) {
423
+ return this.wmap.has(key);
424
+ }
425
+ /**
426
+ * Loads multiple key-value pairs into the map.
427
+ *
428
+ * @param entries Iterable of key-value pairs to load
429
+ * @param timeoutMs Optional timeout override for all entries
430
+ * @returns This instance for chaining
431
+ */
432
+ load(entries, timeoutMs) {
433
+ for (const [key, value] of entries) {
434
+ this.set(key, value, timeoutMs);
435
+ }
436
+ return this;
437
+ }
438
+ /**
439
+ * Updates an existing value or creates a new one using an update function.
440
+ *
441
+ * @param key The key to update
442
+ * @param update Function that receives the current value and returns the new value
443
+ * @returns This instance for chaining
444
+ */
445
+ update(key, update) {
446
+ return this.set(key, update(this.get(key), key, this));
447
+ }
448
+ /**
449
+ * Gets a value or creates it using a factory function if it doesn't exist.
450
+ * Refreshes timeout if the value exists.
451
+ *
452
+ * @param key The key to get or create
453
+ * @param factory Function to create the value if it doesn't exist
454
+ * @param timeoutMs Optional timeout override for new entries
455
+ * @returns The existing or newly created value
456
+ */
457
+ getOrDefault(key, factory, timeoutMs) {
458
+ const vt = this.wmap.get(key);
459
+ if (vt) {
460
+ const [value, timeout] = vt;
461
+ timeout.refresh();
462
+ return value;
463
+ } else {
464
+ const value = factory(key, this);
465
+ this.set(key, value, timeoutMs);
466
+ return value;
467
+ }
468
+ }
469
+ };
470
+ function countUniques(input) {
471
+ return new ExtMap(iterTools.reduce(new mnemonist.MultiSet(), (acc, imp) => {
472
+ return acc.add(imp);
473
+ }, input).multiplicities()).sortByValues((a, b) => {
474
+ return b - a;
475
+ });
476
+ }
477
+ __name(countUniques, "countUniques");
478
+
479
+ // src/isGenericMap.ts
480
+ function isGenericMap(target, requiredProps = [
481
+ "get",
482
+ "set",
483
+ "has"
484
+ ]) {
485
+ return !!target && requiredProps.every((prop) => {
486
+ return typeof target[prop] === typeof Map.prototype[prop];
487
+ });
488
+ }
489
+ __name(isGenericMap, "isGenericMap");
490
+
491
+ exports.ExtMap = ExtMap;
492
+ exports.TimeoutWeakMap = TimeoutWeakMap;
493
+ exports.countUniques = countUniques;
494
+ exports.entriesArray = entriesArray;
495
+ exports.isGenericMap = isGenericMap;
496
+ exports.keysArray = keysArray;
497
+ exports.mapGetOrDefault = mapGetOrDefault;
498
+ exports.mapLoad = mapLoad;
499
+ exports.mapReverse = mapReverse;
500
+ exports.mapUpdate = mapUpdate;
501
+ exports.sort = sort;
502
+ exports.sortByKeys = sortByKeys;
503
+ exports.sortByValues = sortByValues;
504
+ exports.toMap = toMap;
505
+ exports.valuesArray = valuesArray;
@@ -0,0 +1,17 @@
1
+ export { ExtMap } from './ExtMap.cjs';
2
+ export { TimeoutWeakMap } from './TimeoutWeakMap.cjs';
3
+ export { countUniques } from './countUniques.cjs';
4
+ export { entriesArray } from './entriesArray.cjs';
5
+ export { isGenericMap } from './isGenericMap.cjs';
6
+ export { keysArray } from './keysArray.cjs';
7
+ export { mapGetOrDefault } from './mapGetOrDefault.cjs';
8
+ export { mapLoad } from './mapLoad.cjs';
9
+ export { mapReverse } from './mapReverse.cjs';
10
+ export { mapUpdate } from './mapUpdate.cjs';
11
+ export { sort } from './sort.cjs';
12
+ export { sortByKeys } from './sortByKeys.cjs';
13
+ export { sortByValues } from './sortByValues.cjs';
14
+ export { toMap } from './toMap.cjs';
15
+ export { valuesArray } from './valuesArray.cjs';
16
+ import 'util';
17
+ import './GenericMap-B4OM3vGu.cjs';
package/lib/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ export { ExtMap } from './ExtMap.js';
2
+ export { TimeoutWeakMap } from './TimeoutWeakMap.js';
3
+ export { countUniques } from './countUniques.js';
4
+ export { entriesArray } from './entriesArray.js';
5
+ export { isGenericMap } from './isGenericMap.js';
6
+ export { keysArray } from './keysArray.js';
7
+ export { mapGetOrDefault } from './mapGetOrDefault.js';
8
+ export { mapLoad } from './mapLoad.js';
9
+ export { mapReverse } from './mapReverse.js';
10
+ export { mapUpdate } from './mapUpdate.js';
11
+ export { sort } from './sort.js';
12
+ export { sortByKeys } from './sortByKeys.js';
13
+ export { sortByValues } from './sortByValues.js';
14
+ export { toMap } from './toMap.js';
15
+ export { valuesArray } from './valuesArray.js';
16
+ import 'util';
17
+ import './GenericMap-B4OM3vGu.js';