@bemoje/iter 1.0.0 → 1.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.
- package/README.md +96 -0
- package/index.d.ts +67 -95
- package/index.mjs +182 -288
- package/package.json +2 -3
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# @bemoje/iter
|
|
2
|
+
|
|
3
|
+
Utility functions for working with iterables, especially map-like key-value pair structures.
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/api/esm.html)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @bemoje/iter
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### Transform Iterables
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { mapIterable, mapIterableKeys, mapIterableValues } from '@bemoje/iter'
|
|
20
|
+
|
|
21
|
+
const entries: Iterable<[string, number]> = new Map([
|
|
22
|
+
['a', 1],
|
|
23
|
+
['b', 2],
|
|
24
|
+
])
|
|
25
|
+
|
|
26
|
+
// Transform both keys and values
|
|
27
|
+
const mapped = mapIterable(entries, (value, key) => [key.toUpperCase(), value * 10])
|
|
28
|
+
// [['A', 10], ['B', 20]]
|
|
29
|
+
|
|
30
|
+
// Transform only keys
|
|
31
|
+
const newKeys = mapIterableKeys(entries, (key) => key.toUpperCase())
|
|
32
|
+
// [['A', 1], ['B', 2]]
|
|
33
|
+
|
|
34
|
+
// Transform only values
|
|
35
|
+
const doubled = mapIterableValues(entries, (value) => value * 2)
|
|
36
|
+
// [['a', 2], ['b', 4]]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Filter and Reduce
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { filterIterable, reduceIterable, forEachIterable } from '@bemoje/iter'
|
|
43
|
+
|
|
44
|
+
const entries: Iterable<[string, number]> = new Map([
|
|
45
|
+
['a', 1],
|
|
46
|
+
['b', 2],
|
|
47
|
+
['c', 3],
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
// Filter entries by value
|
|
51
|
+
const filtered = filterIterable(entries, (value) => value > 1)
|
|
52
|
+
// [['b', 2], ['c', 3]]
|
|
53
|
+
|
|
54
|
+
// Reduce to a single value
|
|
55
|
+
const sum = reduceIterable(entries, (acc, value) => acc + value, 0)
|
|
56
|
+
// 6
|
|
57
|
+
|
|
58
|
+
// Side effects
|
|
59
|
+
forEachIterable(entries, (value, key) => console.log(`${key}: ${value}`))
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Count Unique Values
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { countUniques } from '@bemoje/iter'
|
|
66
|
+
|
|
67
|
+
const words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
|
|
68
|
+
const counts = countUniques(words)
|
|
69
|
+
// ExtMap { 'apple' => 3, 'banana' => 2, 'cherry' => 1 } (sorted by count desc)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Convert to Object
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { toObjectIterable } from '@bemoje/iter'
|
|
76
|
+
|
|
77
|
+
const entries: Iterable<[string, number]> = [
|
|
78
|
+
['a', 1],
|
|
79
|
+
['b', 2],
|
|
80
|
+
]
|
|
81
|
+
const obj = toObjectIterable(entries)
|
|
82
|
+
// { a: 1, b: 2 }
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## API Reference
|
|
86
|
+
|
|
87
|
+
| Export | Description |
|
|
88
|
+
| ------------------- | ------------------------------------------------------- |
|
|
89
|
+
| `mapIterable` | Transform both keys and values of map-like entries |
|
|
90
|
+
| `mapIterableKeys` | Transform keys while preserving values |
|
|
91
|
+
| `mapIterableValues` | Transform values while preserving keys |
|
|
92
|
+
| `filterIterable` | Filter map entries by predicate |
|
|
93
|
+
| `reduceIterable` | Reduce a map-like iterable to a single value |
|
|
94
|
+
| `forEachIterable` | Execute a callback for each entry |
|
|
95
|
+
| `countUniques` | Count unique values in an iterable, sorted by frequency |
|
|
96
|
+
| `toObjectIterable` | Convert key-value iterable to a plain object |
|
package/index.d.ts
CHANGED
|
@@ -1,118 +1,71 @@
|
|
|
1
|
-
import { inspect, InspectOptions } from 'util';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
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}
|
|
2
|
+
* Filter a `Map` (or `ReadonlyMap`). Predicate receives `(value, key)`. Returns a new `Map`.
|
|
9
3
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
declare function filter<K, V>(map: ReadonlyMap<K, V>, predicate: (value: V, key: K) => boolean): Map<K, V>;
|
|
5
|
+
/** Filter a `Set` (or `ReadonlySet`). Predicate receives `(value)`. Returns a new `Set`. */
|
|
6
|
+
declare function filter<V>(set: ReadonlySet<V>, predicate: (value: V) => boolean): Set<V>;
|
|
7
|
+
/** Filter an array. Predicate receives `(value, index)`. Returns a new array. */
|
|
8
|
+
declare function filter<V>(array: readonly V[], predicate: (value: V, index: number) => boolean): V[];
|
|
9
|
+
/** Filter a plain object. Predicate receives `(value, key)`. Returns a new partial object. */
|
|
10
|
+
declare function filter<K extends string, V>(object: Record<K, V>, predicate: (value: V, key: K) => boolean): Partial<Record<K, V>>;
|
|
11
|
+
/** Filter any `Iterable`. Predicate receives `(value)`. Returns a new array. */
|
|
12
|
+
declare function filter<V>(iterable: Iterable<V>, predicate: (value: V) => boolean): V[];
|
|
13
13
|
|
|
14
|
-
declare const TARGET_SYMBOL: unique symbol;
|
|
15
14
|
/**
|
|
16
|
-
*
|
|
15
|
+
* Transform values of an iterable.
|
|
17
16
|
*/
|
|
18
|
-
declare
|
|
19
|
-
private [TARGET_SYMBOL];
|
|
20
|
-
constructor(target: Target);
|
|
21
|
-
get [Symbol.toStringTag](): string;
|
|
22
|
-
get target(): Target;
|
|
23
|
-
}
|
|
17
|
+
declare function filterIterable<T>(set: Iterable<T>, predicate: (value: T) => boolean): Iterable<T>;
|
|
24
18
|
|
|
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
19
|
/**
|
|
47
|
-
*
|
|
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
|
-
* ```
|
|
20
|
+
* Filter map entries based on a predicate function.
|
|
69
21
|
*/
|
|
70
|
-
declare
|
|
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
|
-
}
|
|
22
|
+
declare function filterIterableEntries<K, V>(mapLike: Iterable<[K, V]>, predicate: (value: V, key: K) => boolean): Iterable<[K, V]>;
|
|
96
23
|
|
|
97
24
|
/**
|
|
98
|
-
*
|
|
25
|
+
* Iterate a `Map` (or `ReadonlyMap`). Callback receives `(value, key)`.
|
|
99
26
|
*/
|
|
100
|
-
declare function
|
|
27
|
+
declare function forEach<K, V>(map: ReadonlyMap<K, V>, callback: (value: V, key: K) => void): void;
|
|
28
|
+
/** Iterate a `Set` (or `ReadonlySet`). Callback receives `(value)`. */
|
|
29
|
+
declare function forEach<V>(set: ReadonlySet<V>, callback: (value: V) => void): void;
|
|
30
|
+
/** Iterate an array. Callback receives `(value, index)`. */
|
|
31
|
+
declare function forEach<V>(array: readonly V[], callback: (value: V, index: number) => void): void;
|
|
32
|
+
/** Iterate a plain object. Callback receives `(value, key)`. */
|
|
33
|
+
declare function forEach<K extends string, V>(object: Record<K, V>, callback: (value: V, key: K) => void): void;
|
|
34
|
+
/** Iterate any `Iterable`. Callback receives `(value)`. */
|
|
35
|
+
declare function forEach<V>(iterable: Iterable<V>, callback: (value: V) => void): void;
|
|
101
36
|
|
|
102
37
|
/**
|
|
103
|
-
*
|
|
38
|
+
* Iterate over values of an iterable, executing a callback for each.
|
|
104
39
|
*/
|
|
105
|
-
declare function
|
|
40
|
+
declare function forEachIterable<T>(set: Iterable<T>, callback: (value: T) => unknown): void;
|
|
106
41
|
|
|
107
42
|
/**
|
|
108
43
|
* Execute a callback function for each entry in a map-like iterable.
|
|
109
44
|
*/
|
|
110
|
-
declare function
|
|
45
|
+
declare function forEachIterableEntries<K, V>(mapLike: Iterable<[K, V]>, callback: (value: V, key: K) => void): void;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Map a `Map` (or `ReadonlyMap`). Callback receives `(value, key)`. Returns a new `Map`.
|
|
49
|
+
*/
|
|
50
|
+
declare function map<K, V, R>(map: ReadonlyMap<K, V>, transform: (value: V, key: K) => R): Map<K, R>;
|
|
51
|
+
/** Map a `Set` (or `ReadonlySet`). Callback receives `(value)`. Returns a new `Set`. */
|
|
52
|
+
declare function map<V, R>(set: ReadonlySet<V>, transform: (value: V) => R): Set<R>;
|
|
53
|
+
/** Map an array. Callback receives `(value, index)`. Returns a new array. */
|
|
54
|
+
declare function map<V, R>(array: readonly V[], transform: (value: V, index: number) => R): R[];
|
|
55
|
+
/** Map a plain object. Callback receives `(value, key)`. Returns a new object. */
|
|
56
|
+
declare function map<K extends string, V, R>(object: Record<K, V>, transform: (value: V, key: K) => R): Record<K, R>;
|
|
57
|
+
/** Map any `Iterable`. Callback receives `(value)`. Returns a new array. */
|
|
58
|
+
declare function map<V, R>(iterable: Iterable<V>, transform: (value: V) => R): R[];
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Transform values of an iterable.
|
|
62
|
+
*/
|
|
63
|
+
declare function mapIterable<T, R>(set: Iterable<T>, transform: (value: T) => R): Iterable<R>;
|
|
111
64
|
|
|
112
65
|
/**
|
|
113
66
|
* Transform both keys and values of map entries.
|
|
114
67
|
*/
|
|
115
|
-
declare function
|
|
68
|
+
declare function mapIterableEntries<K, V, U, W>(mapLike: Iterable<[K, V]>, transform: (value: V, key: K) => [U, W]): Iterable<[U, W]>;
|
|
116
69
|
|
|
117
70
|
/**
|
|
118
71
|
* Transform map keys while preserving values.
|
|
@@ -124,14 +77,33 @@ declare function mapIterableKeys<K, V, U>(mapLike: Iterable<[K, V]>, transform:
|
|
|
124
77
|
*/
|
|
125
78
|
declare function mapIterableValues<K, V, U>(mapLike: Iterable<[K, V]>, transform: (value: V, key: K) => U): Iterable<[K, U]>;
|
|
126
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Reduce a `Map` (or `ReadonlyMap`). Callback receives `(accumulator, value, key)`.
|
|
82
|
+
*/
|
|
83
|
+
declare function reduce<K, V, T>(map: ReadonlyMap<K, V>, reducer: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
|
|
84
|
+
/** Reduce a `Set` (or `ReadonlySet`). Callback receives `(accumulator, value)`. */
|
|
85
|
+
declare function reduce<V, T>(set: ReadonlySet<V>, reducer: (accumulator: T, value: V) => T, initialValue: T): T;
|
|
86
|
+
/** Reduce an array. Callback receives `(accumulator, value, index)`. */
|
|
87
|
+
declare function reduce<V, T>(array: readonly V[], reducer: (accumulator: T, value: V, index: number) => T, initialValue: T): T;
|
|
88
|
+
/** Reduce a plain object. Callback receives `(accumulator, value, key)`. */
|
|
89
|
+
declare function reduce<K extends string, V, T>(object: Record<K, V>, reducer: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
|
|
90
|
+
/** Reduce any `Iterable`. Callback receives `(accumulator, value)`. */
|
|
91
|
+
declare function reduce<V, T>(iterable: Iterable<V>, reducer: (accumulator: T, value: V) => T, initialValue: T): T;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Reduce any iterable to a single value.
|
|
95
|
+
*/
|
|
96
|
+
declare function reduceIterable<V, T>(iterable: Iterable<V>, reducer: (accumulator: T, value: V) => T, initialValue: T): T;
|
|
97
|
+
|
|
127
98
|
/**
|
|
128
99
|
* Reduce a map-like iterable to a single value.
|
|
129
100
|
*/
|
|
130
|
-
declare function
|
|
101
|
+
declare function reduceIterableEntries<K, V, T>(mapLike: Iterable<[K, V]>, reducer: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
|
|
131
102
|
|
|
132
103
|
/**
|
|
133
104
|
* Convert a map-like iterable to a regular object.
|
|
134
105
|
*/
|
|
135
106
|
declare function toObjectIterable<K extends string, V>(mapLike: Iterable<[K, V]>): Record<K, V>;
|
|
107
|
+
declare function toObjectIterable<K, V>(mapLike: Iterable<[K, V]>): Record<Extract<K, string> | string, V>;
|
|
136
108
|
|
|
137
|
-
export {
|
|
109
|
+
export { filter, filterIterable, filterIterableEntries, forEach, forEachIterable, forEachIterableEntries, map, mapIterable, mapIterableEntries, mapIterableKeys, mapIterableValues, reduce, reduceIterable, reduceIterableEntries, toObjectIterable };
|
package/index.mjs
CHANGED
|
@@ -1,303 +1,142 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isIterable, filter, map, reduce, forEach } from 'iter-tools';
|
|
3
|
-
import { MultiSet } from 'mnemonist';
|
|
1
|
+
import { filter as filter$1, forEach, map as map$1 } from 'iter-tools';
|
|
4
2
|
|
|
5
3
|
var __defProp = Object.defineProperty;
|
|
6
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
5
|
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
value
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
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;
|
|
6
|
+
// src/filter.ts
|
|
7
|
+
function filter(target, predicate) {
|
|
8
|
+
if (Symbol.iterator in target) {
|
|
9
|
+
if (target instanceof Map) {
|
|
10
|
+
const result3 = /* @__PURE__ */ new Map();
|
|
11
|
+
for (const [key, value] of target) {
|
|
12
|
+
if (predicate(value, key)) {
|
|
13
|
+
result3.set(key, value);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return result3;
|
|
17
|
+
}
|
|
18
|
+
if (target instanceof Set) {
|
|
19
|
+
const result3 = /* @__PURE__ */ new Set();
|
|
20
|
+
for (const value of target) {
|
|
21
|
+
if (predicate(value)) {
|
|
22
|
+
result3.add(value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return result3;
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(target)) {
|
|
28
|
+
return target.filter(predicate);
|
|
29
|
+
}
|
|
30
|
+
const result2 = [];
|
|
31
|
+
for (const value of target) {
|
|
32
|
+
if (predicate(value)) {
|
|
33
|
+
result2.push(value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result2;
|
|
37
|
+
}
|
|
38
|
+
const result = {};
|
|
39
|
+
for (const [key, value] of Object.entries(target)) {
|
|
40
|
+
if (predicate(value, key)) {
|
|
41
|
+
result[key] = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
__name(filter, "filter");
|
|
47
|
+
function filterIterable(set, predicate) {
|
|
48
|
+
return filter$1((value) => {
|
|
49
|
+
return predicate(value);
|
|
50
|
+
}, set);
|
|
182
51
|
}
|
|
183
|
-
__name(
|
|
184
|
-
|
|
185
|
-
|
|
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]) => {
|
|
52
|
+
__name(filterIterable, "filterIterable");
|
|
53
|
+
function filterIterableEntries(mapLike, predicate) {
|
|
54
|
+
return filter$1(([k, v]) => {
|
|
283
55
|
return predicate(v, k);
|
|
284
56
|
}, mapLike);
|
|
285
57
|
}
|
|
286
|
-
__name(
|
|
287
|
-
function
|
|
58
|
+
__name(filterIterableEntries, "filterIterableEntries");
|
|
59
|
+
function forEachIterableEntries(mapLike, callback) {
|
|
288
60
|
return forEach(([k, v]) => {
|
|
289
61
|
return callback(v, k);
|
|
290
62
|
}, mapLike);
|
|
291
63
|
}
|
|
64
|
+
__name(forEachIterableEntries, "forEachIterableEntries");
|
|
65
|
+
|
|
66
|
+
// src/forEach.ts
|
|
67
|
+
function forEach2(target, callback) {
|
|
68
|
+
if (Symbol.iterator in target) {
|
|
69
|
+
if (target instanceof Map) {
|
|
70
|
+
return target.forEach(callback);
|
|
71
|
+
}
|
|
72
|
+
if (target instanceof Set) {
|
|
73
|
+
return target.forEach(callback);
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(target)) {
|
|
76
|
+
return target.forEach(callback);
|
|
77
|
+
}
|
|
78
|
+
for (const value of target) {
|
|
79
|
+
callback(value);
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
return forEachIterableEntries(Object.entries(target), callback);
|
|
84
|
+
}
|
|
85
|
+
__name(forEach2, "forEach");
|
|
86
|
+
function forEachIterable(set, callback) {
|
|
87
|
+
return forEach((value) => {
|
|
88
|
+
return callback(value);
|
|
89
|
+
}, set);
|
|
90
|
+
}
|
|
292
91
|
__name(forEachIterable, "forEachIterable");
|
|
293
|
-
|
|
294
|
-
|
|
92
|
+
|
|
93
|
+
// src/map.ts
|
|
94
|
+
function map(target, transform) {
|
|
95
|
+
if (target instanceof Map) {
|
|
96
|
+
const result2 = /* @__PURE__ */ new Map();
|
|
97
|
+
for (const [key, value] of target) {
|
|
98
|
+
result2.set(key, transform(value, key));
|
|
99
|
+
}
|
|
100
|
+
return result2;
|
|
101
|
+
}
|
|
102
|
+
if (target instanceof Set) {
|
|
103
|
+
const result2 = /* @__PURE__ */ new Set();
|
|
104
|
+
for (const value of target) {
|
|
105
|
+
result2.add(transform(value));
|
|
106
|
+
}
|
|
107
|
+
return result2;
|
|
108
|
+
}
|
|
109
|
+
if (Array.isArray(target)) {
|
|
110
|
+
return target.map(transform);
|
|
111
|
+
}
|
|
112
|
+
if (Symbol.iterator in target) {
|
|
113
|
+
const result2 = [];
|
|
114
|
+
for (const value of target) {
|
|
115
|
+
result2.push(transform(value));
|
|
116
|
+
}
|
|
117
|
+
return result2;
|
|
118
|
+
}
|
|
119
|
+
const result = {};
|
|
120
|
+
for (const [key, value] of Object.entries(target)) {
|
|
121
|
+
result[key] = transform(value, key);
|
|
122
|
+
}
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
__name(map, "map");
|
|
126
|
+
function mapIterable(set, transform) {
|
|
127
|
+
return map$1((value) => {
|
|
128
|
+
return transform(value);
|
|
129
|
+
}, set);
|
|
130
|
+
}
|
|
131
|
+
__name(mapIterable, "mapIterable");
|
|
132
|
+
function mapIterableEntries(mapLike, transform) {
|
|
133
|
+
return map$1(([k, v]) => {
|
|
295
134
|
return transform(v, k);
|
|
296
135
|
}, mapLike);
|
|
297
136
|
}
|
|
298
|
-
__name(
|
|
137
|
+
__name(mapIterableEntries, "mapIterableEntries");
|
|
299
138
|
function mapIterableKeys(mapLike, transform) {
|
|
300
|
-
return map(([k, v]) => {
|
|
139
|
+
return map$1(([k, v]) => {
|
|
301
140
|
return [
|
|
302
141
|
transform(k, v),
|
|
303
142
|
v
|
|
@@ -306,7 +145,7 @@ function mapIterableKeys(mapLike, transform) {
|
|
|
306
145
|
}
|
|
307
146
|
__name(mapIterableKeys, "mapIterableKeys");
|
|
308
147
|
function mapIterableValues(mapLike, transform) {
|
|
309
|
-
return map(([k, v]) => {
|
|
148
|
+
return map$1(([k, v]) => {
|
|
310
149
|
return [
|
|
311
150
|
k,
|
|
312
151
|
transform(v, k)
|
|
@@ -315,19 +154,74 @@ function mapIterableValues(mapLike, transform) {
|
|
|
315
154
|
}
|
|
316
155
|
__name(mapIterableValues, "mapIterableValues");
|
|
317
156
|
|
|
157
|
+
// src/reduce.ts
|
|
158
|
+
function reduce(target, reducer, initialValue) {
|
|
159
|
+
if (Symbol.iterator in target) {
|
|
160
|
+
if (target instanceof Map) {
|
|
161
|
+
let acc3 = initialValue;
|
|
162
|
+
for (const [key, value] of target.entries()) {
|
|
163
|
+
acc3 = reducer(acc3, value, key);
|
|
164
|
+
}
|
|
165
|
+
return acc3;
|
|
166
|
+
}
|
|
167
|
+
if (target instanceof Set) {
|
|
168
|
+
let acc3 = initialValue;
|
|
169
|
+
for (const value of target.values()) {
|
|
170
|
+
acc3 = reducer(acc3, value);
|
|
171
|
+
}
|
|
172
|
+
return acc3;
|
|
173
|
+
}
|
|
174
|
+
if (Array.isArray(target)) {
|
|
175
|
+
let acc3 = initialValue;
|
|
176
|
+
for (let i = 0; i < target.length; i++) {
|
|
177
|
+
acc3 = reducer(acc3, target[i], i);
|
|
178
|
+
}
|
|
179
|
+
return acc3;
|
|
180
|
+
}
|
|
181
|
+
let acc2 = initialValue;
|
|
182
|
+
for (const value of target) {
|
|
183
|
+
acc2 = reducer(acc2, value);
|
|
184
|
+
}
|
|
185
|
+
return acc2;
|
|
186
|
+
}
|
|
187
|
+
let acc = initialValue;
|
|
188
|
+
for (const [key, value] of Object.entries(target)) {
|
|
189
|
+
acc = reducer(acc, value, key);
|
|
190
|
+
}
|
|
191
|
+
return acc;
|
|
192
|
+
}
|
|
193
|
+
__name(reduce, "reduce");
|
|
194
|
+
|
|
318
195
|
// src/reduceIterable.ts
|
|
319
|
-
function reduceIterable(
|
|
196
|
+
function reduceIterable(iterable, reducer, initialValue) {
|
|
197
|
+
for (const value of iterable) {
|
|
198
|
+
initialValue = reducer(initialValue, value);
|
|
199
|
+
}
|
|
200
|
+
return initialValue;
|
|
201
|
+
}
|
|
202
|
+
__name(reduceIterable, "reduceIterable");
|
|
203
|
+
|
|
204
|
+
// src/reduceIterableEntries.ts
|
|
205
|
+
function reduceIterableEntries(mapLike, reducer, initialValue) {
|
|
320
206
|
for (const [key, value] of mapLike) {
|
|
321
207
|
initialValue = reducer(initialValue, value, key);
|
|
322
208
|
}
|
|
323
209
|
return initialValue;
|
|
324
210
|
}
|
|
325
|
-
__name(
|
|
211
|
+
__name(reduceIterableEntries, "reduceIterableEntries");
|
|
326
212
|
|
|
327
213
|
// src/toObjectIterable.ts
|
|
328
214
|
function toObjectIterable(mapLike) {
|
|
329
|
-
|
|
215
|
+
const object = {};
|
|
216
|
+
for (const [key, value] of mapLike) {
|
|
217
|
+
if (typeof key !== "string" && typeof key !== "number" && typeof key !== "symbol") {
|
|
218
|
+
object[String(key)] = value;
|
|
219
|
+
} else {
|
|
220
|
+
object[key] = value;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return object;
|
|
330
224
|
}
|
|
331
225
|
__name(toObjectIterable, "toObjectIterable");
|
|
332
226
|
|
|
333
|
-
export {
|
|
227
|
+
export { filter, filterIterable, filterIterableEntries, forEach2 as forEach, forEachIterable, forEachIterableEntries, map, mapIterable, mapIterableEntries, mapIterableKeys, mapIterableValues, reduce, reduceIterable, reduceIterableEntries, toObjectIterable };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/iter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "iter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -15,8 +15,7 @@
|
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"iter-tools": "^7.5.4"
|
|
19
|
-
"mnemonist": "^0.40.3"
|
|
18
|
+
"iter-tools": "^7.5.4"
|
|
20
19
|
},
|
|
21
20
|
"publishConfig": {
|
|
22
21
|
"access": "public"
|