@bemoje/map 1.2.0 → 1.2.2

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