@bemoje/map 1.0.0 → 1.2.0

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