@bemoje/iter 0.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 (3) hide show
  1. package/index.d.ts +137 -0
  2. package/index.mjs +333 -0
  3. package/package.json +35 -0
package/index.d.ts ADDED
@@ -0,0 +1,137 @@
1
+ import { inspect, InspectOptions } from 'util';
2
+
3
+ /**
4
+ * Interface for a View that wraps a target object.
5
+ * View methods are redirected to the wrapped object which acts as
6
+ * a surrogate 'this'.
7
+ *
8
+ * To create a method that redirects to 'target', @see {@link defineProxifiedPrototypeProperty}
9
+ */
10
+ interface IView<T extends object> {
11
+ readonly target: T;
12
+ }
13
+
14
+ declare const TARGET_SYMBOL: unique symbol;
15
+ /**
16
+ * Base class providing view functionality over a target object using the Composition pattern.
17
+ */
18
+ declare class View<Target extends object> implements IView<Target> {
19
+ private [TARGET_SYMBOL];
20
+ constructor(target: Target);
21
+ get [Symbol.toStringTag](): string;
22
+ get target(): Target;
23
+ }
24
+
25
+ declare module './ExtMap' {
26
+ interface ExtMap<K, V> extends Map<K, V> {
27
+ load(entries: Iterable<[K, V]>): this;
28
+ sort(compare: (a: [K, V], b: [K, V]) => number): this;
29
+ sortByKeys(compare: (a: K, b: K) => number): this;
30
+ sortByValues(compare: (a: V, b: V) => number): this;
31
+ reverse(): this;
32
+ update(key: K, update: (value: V | undefined, key: K, map: this) => V): this;
33
+ getOrDefault(key: K, factory: (key: K, map: this) => V): V;
34
+ keysArray(): K[];
35
+ valuesArray(): V[];
36
+ entriesArray(): [K, V][];
37
+ toMap(): Map<K, V>;
38
+ toObject<K extends string, V>(): Record<K, V>;
39
+ reduce<R>(reducer: (accumulator: R, value: V, key: K) => R, initialValue: R): R;
40
+ map<NewK, NewV>(mapper: (value: V, key: K) => [NewK, NewV]): ExtMap<NewK, NewV>;
41
+ mapKeys<NewK>(mapper: (key: K, value: V) => NewK): ExtMap<NewK, V>;
42
+ mapValues<NewV>(mapper: (value: V, key: K) => NewV): ExtMap<K, NewV>;
43
+ filter(predicate: (value: V, key: K) => boolean): ExtMap<K, V>;
44
+ }
45
+ }
46
+ /**
47
+ * Minimal Extended Map class focused only on Map-specific utilities.
48
+ *
49
+ * @template K - The type of keys in the map
50
+ * @template V - The type of values in the map
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * import { map as iterMap, filter } from 'iter-tools'
55
+ *
56
+ * const extMap = new ExtMap<string, number>()
57
+ * .load([['a', 1], ['b', 2], ['c', 3]])
58
+ * .update('a', v => (v ?? 0) + 10)
59
+ * .sortByValues((a, b) => a - b)
60
+ *
61
+ * // Use iter-tools for transformations
62
+ * const doubled = new ExtMap<string, number>()
63
+ * .load(iterMap(([k, v]) => [k, v * 2], extMap))
64
+ *
65
+ * // Use iter-tools for filtering
66
+ * const filtered = new ExtMap<string, number>()
67
+ * .load(filter(([k, v]) => v > 5, extMap))
68
+ * ```
69
+ */
70
+ declare class ExtMap<K = any, V = any> extends View<Map<K, V>> implements Map<K, V> {
71
+ constructor(iterable?: Iterable<readonly [K, V]> | null | undefined);
72
+ constructor(obj?: Record<Extract<K, string>, V>);
73
+ static fromObject<K extends string, V>(obj: Record<K, V>): ExtMap<K, V>;
74
+ static fromIterable<K, V>(entries: Iterable<readonly [K, V]>): ExtMap<K, V>;
75
+ /**
76
+ * Creates a shallow copy of the map.
77
+ *
78
+ * @returns A new ExtMap instance with the same entries
79
+ */
80
+ clone(): ExtMap<K, V>;
81
+ /**
82
+ * Converts the map to a JSON-serializable format.
83
+ *
84
+ * @returns Array of key-value pairs suitable for JSON serialization
85
+ */
86
+ toJSON(): [K, V][];
87
+ /**
88
+ * Custom inspection method for better debugging output.
89
+ *
90
+ * @param _depth - The depth of inspection
91
+ * @param _options - Options for inspection
92
+ * @returns A string representation of the map
93
+ */
94
+ [inspect.custom](_depth: number, _options: InspectOptions): string;
95
+ }
96
+
97
+ /**
98
+ * Count unique occurrences of values in an iterable, returning a sorted map by count descending.
99
+ */
100
+ declare function countUniques<V>(input: Iterable<V>): ExtMap<V, number>;
101
+
102
+ /**
103
+ * Filter map entries based on a predicate function.
104
+ */
105
+ declare function filterIterable<K, V>(mapLike: Iterable<[K, V]>, predicate: (value: V, key: K) => boolean): Iterable<[K, V]>;
106
+
107
+ /**
108
+ * Execute a callback function for each entry in a map-like iterable.
109
+ */
110
+ declare function forEachIterable<K, V>(mapLike: Iterable<[K, V]>, callback: (value: V, key: K) => void): void;
111
+
112
+ /**
113
+ * Transform both keys and values of map entries.
114
+ */
115
+ declare function mapIterable<K, V, U, W>(mapLike: Iterable<[K, V]>, transform: (value: V, key: K) => [U, W]): Iterable<[U, W]>;
116
+
117
+ /**
118
+ * Transform map keys while preserving values.
119
+ */
120
+ declare function mapIterableKeys<K, V, U>(mapLike: Iterable<[K, V]>, transform: (key: K, value: V) => U): Iterable<[U, V]>;
121
+
122
+ /**
123
+ * Transform map values while preserving keys.
124
+ */
125
+ declare function mapIterableValues<K, V, U>(mapLike: Iterable<[K, V]>, transform: (value: V, key: K) => U): Iterable<[K, U]>;
126
+
127
+ /**
128
+ * Reduce a map-like iterable to a single value.
129
+ */
130
+ declare function reduceIterable<K, V, T>(mapLike: Iterable<[K, V]>, reducer: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
131
+
132
+ /**
133
+ * Convert a map-like iterable to a regular object.
134
+ */
135
+ declare function toObjectIterable<K extends string, V>(mapLike: Iterable<[K, V]>): Record<K, V>;
136
+
137
+ export { countUniques, filterIterable, forEachIterable, mapIterable, mapIterableKeys, mapIterableValues, reduceIterable, toObjectIterable };
package/index.mjs ADDED
@@ -0,0 +1,333 @@
1
+ import { inspect } from 'util';
2
+ import { isIterable, filter, map, reduce, forEach } from 'iter-tools';
3
+ import { MultiSet } from 'mnemonist';
4
+
5
+ var __defProp = Object.defineProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+
8
+ // ../composition/src/View.ts
9
+ var TARGET_SYMBOL = /* @__PURE__ */ Symbol("target");
10
+ var View = class {
11
+ static {
12
+ __name(this, "View");
13
+ }
14
+ [TARGET_SYMBOL];
15
+ constructor(target) {
16
+ this[TARGET_SYMBOL] = target;
17
+ }
18
+ get [Symbol.toStringTag]() {
19
+ return this.constructor.name;
20
+ }
21
+ get target() {
22
+ return this[TARGET_SYMBOL];
23
+ }
24
+ };
25
+
26
+ // ../composition/src/inheritProxifiedPrototypeProperty.ts
27
+ function inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, key) {
28
+ const des = Reflect.getOwnPropertyDescriptor(TargetClass.prototype, key);
29
+ if (!des) {
30
+ return;
31
+ }
32
+ if (des.value && typeof des.value === "function") {
33
+ return Object.defineProperty(ViewerClass.prototype, key, {
34
+ value(...args) {
35
+ return this.target[key](...args);
36
+ },
37
+ writable: true,
38
+ configurable: true,
39
+ enumerable: false
40
+ });
41
+ } else if (des && !des.get && !des.set) {
42
+ return Object.defineProperty(ViewerClass.prototype, key, {
43
+ get() {
44
+ return this.target[key];
45
+ },
46
+ set: des.writable ? /* @__PURE__ */ __name(function set(value) {
47
+ this.target[key] = value;
48
+ }, "set") : void 0,
49
+ configurable: true,
50
+ enumerable: false
51
+ });
52
+ } else {
53
+ Object.defineProperty(ViewerClass.prototype, key, {
54
+ get: des.get ? function() {
55
+ return this.target[key];
56
+ } : void 0,
57
+ set: des.set ? function(value) {
58
+ this.target[key] = value;
59
+ } : void 0,
60
+ configurable: true,
61
+ enumerable: false
62
+ });
63
+ }
64
+ }
65
+ __name(inheritProxifiedPrototypeProperty, "inheritProxifiedPrototypeProperty");
66
+
67
+ // ../composition/src/inheritProxifiedPrototype.ts
68
+ function inheritProxifiedPrototype(ViewerClass, TargetClass, omitKeys) {
69
+ Reflect.ownKeys(TargetClass.prototype).filter((k) => {
70
+ return k !== "constructor" && k !== Symbol.toStringTag;
71
+ }).filter((k) => {
72
+ return omitKeys ? !omitKeys.includes(k) : true;
73
+ }).forEach((k) => {
74
+ return inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, k);
75
+ });
76
+ return ViewerClass;
77
+ }
78
+ __name(inheritProxifiedPrototype, "inheritProxifiedPrototype");
79
+
80
+ // ../object/src/entriesOf.ts
81
+ function entriesOf(obj) {
82
+ return Object.entries(obj);
83
+ }
84
+ __name(entriesOf, "entriesOf");
85
+
86
+ // ../object/src/defineMethod.ts
87
+ function defineMethod(obj, key, value, des = {}) {
88
+ return Object.defineProperty(obj, key, {
89
+ value,
90
+ configurable: true,
91
+ writable: true,
92
+ enumerable: false,
93
+ ...des
94
+ });
95
+ }
96
+ __name(defineMethod, "defineMethod");
97
+
98
+ // ../map/src/entriesArray.ts
99
+ function entriesArray(ins) {
100
+ return Array.from(ins);
101
+ }
102
+ __name(entriesArray, "entriesArray");
103
+
104
+ // ../map/src/keysArray.ts
105
+ function keysArray(ins) {
106
+ return Array.from(ins.keys());
107
+ }
108
+ __name(keysArray, "keysArray");
109
+
110
+ // ../map/src/mapGetOrDefault.ts
111
+ function mapGetOrDefault(map4, key, factory) {
112
+ let value = map4.get(key);
113
+ if (value !== void 0 || map4.has(key)) {
114
+ return value;
115
+ }
116
+ value = factory(key, map4);
117
+ map4.set(key, value);
118
+ return value;
119
+ }
120
+ __name(mapGetOrDefault, "mapGetOrDefault");
121
+
122
+ // ../map/src/mapLoad.ts
123
+ function mapLoad(ins, entries) {
124
+ for (const [key, value] of entries) {
125
+ ins.set(key, value);
126
+ }
127
+ return ins;
128
+ }
129
+ __name(mapLoad, "mapLoad");
130
+
131
+ // ../map/src/mapReverse.ts
132
+ function mapReverse(ins) {
133
+ const entries = Array.from(ins).reverse();
134
+ ins.clear();
135
+ mapLoad(ins, entries);
136
+ return ins;
137
+ }
138
+ __name(mapReverse, "mapReverse");
139
+
140
+ // ../map/src/mapUpdate.ts
141
+ function mapUpdate(ins, key, update) {
142
+ const newValue = update(ins.get(key), key, ins);
143
+ ins.set(key, newValue);
144
+ return ins;
145
+ }
146
+ __name(mapUpdate, "mapUpdate");
147
+
148
+ // ../map/src/sort.ts
149
+ function sort(ins, compare) {
150
+ const entries = Array.from(ins).sort(compare);
151
+ ins.clear();
152
+ for (const [k, v] of entries) {
153
+ ins.set(k, v);
154
+ }
155
+ return ins;
156
+ }
157
+ __name(sort, "sort");
158
+
159
+ // ../map/src/sortByKeys.ts
160
+ function sortByKeys(ins, compare) {
161
+ const entries = Array.from(ins).sort((a, b) => {
162
+ return compare(a[0], b[0]);
163
+ });
164
+ ins.clear();
165
+ for (const [k, v] of entries) {
166
+ ins.set(k, v);
167
+ }
168
+ return ins;
169
+ }
170
+ __name(sortByKeys, "sortByKeys");
171
+
172
+ // ../map/src/sortByValues.ts
173
+ function sortByValues(ins, compare) {
174
+ const entries = Array.from(ins).sort((a, b) => {
175
+ return compare(a[1], b[1]);
176
+ });
177
+ ins.clear();
178
+ for (const [k, v] of entries) {
179
+ ins.set(k, v);
180
+ }
181
+ return ins;
182
+ }
183
+ __name(sortByValues, "sortByValues");
184
+
185
+ // ../map/src/toMap.ts
186
+ function toMap(ins) {
187
+ return new Map(ins);
188
+ }
189
+ __name(toMap, "toMap");
190
+
191
+ // ../map/src/valuesArray.ts
192
+ function valuesArray(ins) {
193
+ return Array.from(ins.values());
194
+ }
195
+ __name(valuesArray, "valuesArray");
196
+
197
+ // ../map/src/ExtMap.ts
198
+ var ExtMap = class _ExtMap extends View {
199
+ static {
200
+ __name(this, "ExtMap");
201
+ }
202
+ constructor(arg) {
203
+ super(new Map(isIterable(arg) ? arg : arg ? entriesOf(arg) : void 0));
204
+ }
205
+ static fromObject(obj) {
206
+ return new _ExtMap(entriesOf(obj));
207
+ }
208
+ static fromIterable(entries) {
209
+ return new _ExtMap(entries);
210
+ }
211
+ /**
212
+ * Creates a shallow copy of the map.
213
+ *
214
+ * @returns A new ExtMap instance with the same entries
215
+ */
216
+ clone() {
217
+ return new _ExtMap(this);
218
+ }
219
+ /**
220
+ * Converts the map to a JSON-serializable format.
221
+ *
222
+ * @returns Array of key-value pairs suitable for JSON serialization
223
+ */
224
+ toJSON() {
225
+ return [
226
+ ...this.entries()
227
+ ];
228
+ }
229
+ /**
230
+ * Custom inspection method for better debugging output.
231
+ *
232
+ * @param _depth - The depth of inspection
233
+ * @param _options - Options for inspection
234
+ * @returns A string representation of the map
235
+ */
236
+ [inspect.custom](_depth, _options) {
237
+ return inspect(this.target, {
238
+ breakLength: 50
239
+ });
240
+ }
241
+ };
242
+ function thisify(fn) {
243
+ return function(...args) {
244
+ return fn(this, ...args);
245
+ };
246
+ }
247
+ __name(thisify, "thisify");
248
+ function transformReturnValue(fn, transform) {
249
+ return function(...args) {
250
+ const result = fn.apply(this, args);
251
+ return transform(result);
252
+ };
253
+ }
254
+ __name(transformReturnValue, "transformReturnValue");
255
+ inheritProxifiedPrototype(ExtMap, Map, []);
256
+ defineMethod(ExtMap.prototype, "load", thisify(mapLoad));
257
+ defineMethod(ExtMap.prototype, "sort", thisify(sort));
258
+ defineMethod(ExtMap.prototype, "sortByKeys", thisify(sortByKeys));
259
+ defineMethod(ExtMap.prototype, "sortByValues", thisify(sortByValues));
260
+ defineMethod(ExtMap.prototype, "reverse", thisify(mapReverse));
261
+ defineMethod(ExtMap.prototype, "update", thisify(mapUpdate));
262
+ defineMethod(ExtMap.prototype, "getOrDefault", thisify(mapGetOrDefault));
263
+ defineMethod(ExtMap.prototype, "keysArray", thisify(keysArray));
264
+ defineMethod(ExtMap.prototype, "valuesArray", thisify(valuesArray));
265
+ defineMethod(ExtMap.prototype, "entriesArray", thisify(entriesArray));
266
+ defineMethod(ExtMap.prototype, "toMap", thisify(toMap));
267
+ defineMethod(ExtMap.prototype, "toObject", thisify(toObjectIterable));
268
+ defineMethod(ExtMap.prototype, "reduce", thisify(reduceIterable));
269
+ defineMethod(ExtMap.prototype, "map", thisify(transformReturnValue(mapIterable, ExtMap.fromIterable)));
270
+ defineMethod(ExtMap.prototype, "mapKeys", thisify(transformReturnValue(mapIterableKeys, ExtMap.fromIterable)));
271
+ defineMethod(ExtMap.prototype, "mapValues", thisify(transformReturnValue(mapIterableValues, ExtMap.fromIterable)));
272
+ defineMethod(ExtMap.prototype, "filter", thisify(transformReturnValue(filterIterable, ExtMap.fromIterable)));
273
+ function countUniques(input) {
274
+ return new ExtMap(reduce(new MultiSet(), (acc, imp) => {
275
+ return acc.add(imp);
276
+ }, input).multiplicities()).sortByValues((a, b) => {
277
+ return b - a;
278
+ });
279
+ }
280
+ __name(countUniques, "countUniques");
281
+ function filterIterable(mapLike, predicate) {
282
+ return filter(([k, v]) => {
283
+ return predicate(v, k);
284
+ }, mapLike);
285
+ }
286
+ __name(filterIterable, "filterIterable");
287
+ function forEachIterable(mapLike, callback) {
288
+ return forEach(([k, v]) => {
289
+ return callback(v, k);
290
+ }, mapLike);
291
+ }
292
+ __name(forEachIterable, "forEachIterable");
293
+ function mapIterable(mapLike, transform) {
294
+ return map(([k, v]) => {
295
+ return transform(v, k);
296
+ }, mapLike);
297
+ }
298
+ __name(mapIterable, "mapIterable");
299
+ function mapIterableKeys(mapLike, transform) {
300
+ return map(([k, v]) => {
301
+ return [
302
+ transform(k, v),
303
+ v
304
+ ];
305
+ }, mapLike);
306
+ }
307
+ __name(mapIterableKeys, "mapIterableKeys");
308
+ function mapIterableValues(mapLike, transform) {
309
+ return map(([k, v]) => {
310
+ return [
311
+ k,
312
+ transform(v, k)
313
+ ];
314
+ }, mapLike);
315
+ }
316
+ __name(mapIterableValues, "mapIterableValues");
317
+
318
+ // src/reduceIterable.ts
319
+ function reduceIterable(mapLike, reducer, initialValue) {
320
+ for (const [key, value] of mapLike) {
321
+ initialValue = reducer(initialValue, value, key);
322
+ }
323
+ return initialValue;
324
+ }
325
+ __name(reduceIterable, "reduceIterable");
326
+
327
+ // src/toObjectIterable.ts
328
+ function toObjectIterable(mapLike) {
329
+ return Object.fromEntries(mapLike);
330
+ }
331
+ __name(toObjectIterable, "toObjectIterable");
332
+
333
+ export { countUniques, filterIterable, forEachIterable, mapIterable, mapIterableKeys, mapIterableValues, reduceIterable, toObjectIterable };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@bemoje/iter",
3
+ "version": "0.0.1",
4
+ "description": "iter",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "keywords": [
8
+ "iter"
9
+ ],
10
+ "exports": {
11
+ ".": {
12
+ "types": "./index.d.ts",
13
+ "import": "./index.mjs",
14
+ "default": "./index.mjs"
15
+ }
16
+ },
17
+ "dependencies": {
18
+ "iter-tools": "^7.5.4",
19
+ "mnemonist": "^0.40.3"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "license": "MIT",
25
+ "author": {
26
+ "name": "Benjamin Møller Jensen",
27
+ "email": "bemoje@bemoje.net",
28
+ "url": "https://github.com/bemoje/"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/bemoje/mono.git",
33
+ "directory": "libs/iter"
34
+ }
35
+ }