@develia/commons 0.4.15 → 0.4.18
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/dist/cjs/array.js +38 -2
- package/dist/cjs/array.js.map +1 -1
- package/dist/cjs/async-pool-executor.js +26 -11
- package/dist/cjs/async-pool-executor.js.map +1 -1
- package/dist/cjs/browser/form-utilities.js +35 -34
- package/dist/cjs/browser/form-utilities.js.map +1 -1
- package/dist/cjs/browser/geo-location.js +1 -1
- package/dist/cjs/browser/geo-location.js.map +1 -1
- package/dist/cjs/browser/native-bridge.js +3 -1
- package/dist/cjs/browser/native-bridge.js.map +1 -1
- package/dist/cjs/cancellable-promise.js +34 -13
- package/dist/cjs/cancellable-promise.js.map +1 -1
- package/dist/cjs/clean-url-search-params.js +26 -3
- package/dist/cjs/clean-url-search-params.js.map +1 -1
- package/dist/cjs/collections/cache-dictionary.js +3 -3
- package/dist/cjs/collections/cache-dictionary.js.map +1 -1
- package/dist/cjs/collections/default-map.js.map +1 -1
- package/dist/cjs/collections/default-weak-map.js.map +1 -1
- package/dist/cjs/collections/from.js +3 -3
- package/dist/cjs/collections/from.js.map +1 -1
- package/dist/cjs/collections/index.js.map +1 -1
- package/dist/cjs/draft.js +46 -11
- package/dist/cjs/draft.js.map +1 -1
- package/dist/cjs/event-bus.js +1 -1
- package/dist/cjs/event-bus.js.map +1 -1
- package/dist/cjs/functions.js +348 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/key-value-pair.js.map +1 -1
- package/dist/cjs/lazy.js.map +1 -1
- package/dist/cjs/math.js +3 -0
- package/dist/cjs/math.js.map +1 -1
- package/dist/cjs/order.js.map +1 -1
- package/dist/cjs/parse.js +10 -1
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/range.js +154 -0
- package/dist/cjs/range.js.map +1 -0
- package/dist/cjs/stopwatch.js.map +1 -1
- package/dist/cjs/string.js +8 -5
- package/dist/cjs/string.js.map +1 -1
- package/dist/cjs/timer.js +1 -1
- package/dist/cjs/timer.js.map +1 -1
- package/dist/cjs/timespan.js +21 -8
- package/dist/cjs/timespan.js.map +1 -1
- package/dist/cjs/type-code.js.map +1 -1
- package/dist/cjs/type.js +20 -19
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utilities.js +175 -39
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/array.js +36 -2
- package/dist/esm/array.js.map +1 -1
- package/dist/esm/async-pool-executor.js +26 -11
- package/dist/esm/async-pool-executor.js.map +1 -1
- package/dist/esm/browser/form-utilities.js +35 -34
- package/dist/esm/browser/form-utilities.js.map +1 -1
- package/dist/esm/browser/geo-location.js +1 -1
- package/dist/esm/browser/geo-location.js.map +1 -1
- package/dist/esm/browser/native-bridge.js +3 -1
- package/dist/esm/browser/native-bridge.js.map +1 -1
- package/dist/esm/cancellable-promise.js +34 -13
- package/dist/esm/cancellable-promise.js.map +1 -1
- package/dist/esm/clean-url-search-params.js +26 -3
- package/dist/esm/clean-url-search-params.js.map +1 -1
- package/dist/esm/collections/cache-dictionary.js +3 -3
- package/dist/esm/collections/cache-dictionary.js.map +1 -1
- package/dist/esm/collections/default-map.js.map +1 -1
- package/dist/esm/collections/default-weak-map.js.map +1 -1
- package/dist/esm/collections/from.js +3 -3
- package/dist/esm/collections/from.js.map +1 -1
- package/dist/esm/collections/index.js.map +1 -1
- package/dist/esm/draft.js +46 -11
- package/dist/esm/draft.js.map +1 -1
- package/dist/esm/event-bus.js +1 -1
- package/dist/esm/event-bus.js.map +1 -1
- package/dist/esm/functions.js +336 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/key-value-pair.js.map +1 -1
- package/dist/esm/lazy.js.map +1 -1
- package/dist/esm/math.js +3 -0
- package/dist/esm/math.js.map +1 -1
- package/dist/esm/order.js.map +1 -1
- package/dist/esm/parse.js +10 -1
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/range.js +151 -0
- package/dist/esm/range.js.map +1 -0
- package/dist/esm/stopwatch.js.map +1 -1
- package/dist/esm/string.js +8 -5
- package/dist/esm/string.js.map +1 -1
- package/dist/esm/timer.js +1 -1
- package/dist/esm/timer.js.map +1 -1
- package/dist/esm/timespan.js +21 -8
- package/dist/esm/timespan.js.map +1 -1
- package/dist/esm/type-code.js.map +1 -1
- package/dist/esm/type.js +20 -19
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utilities.js +174 -39
- package/dist/esm/utilities.js.map +1 -1
- package/dist/types/array.d.ts +29 -0
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/async-pool-executor.d.ts +2 -2
- package/dist/types/async-pool-executor.d.ts.map +1 -1
- package/dist/types/browser/form-utilities.d.ts.map +1 -1
- package/dist/types/browser/native-bridge.d.ts.map +1 -1
- package/dist/types/cancellable-promise.d.ts.map +1 -1
- package/dist/types/clean-url-search-params.d.ts +3 -1
- package/dist/types/clean-url-search-params.d.ts.map +1 -1
- package/dist/types/collections/from.d.ts +1 -1
- package/dist/types/draft.d.ts.map +1 -1
- package/dist/types/functions.d.ts +231 -0
- package/dist/types/functions.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math.d.ts.map +1 -1
- package/dist/types/parse.d.ts.map +1 -1
- package/dist/types/range.d.ts +72 -0
- package/dist/types/range.d.ts.map +1 -0
- package/dist/types/string.d.ts.map +1 -1
- package/dist/types/timer.d.ts +1 -1
- package/dist/types/timespan.d.ts.map +1 -1
- package/dist/types/type.d.ts +2 -0
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/types.d.ts +1 -1
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utilities.d.ts +21 -3
- package/dist/types/utilities.d.ts.map +1 -1
- package/dist/web/array.js +36 -2
- package/dist/web/array.js.map +1 -1
- package/dist/web/async-pool-executor.js +26 -11
- package/dist/web/async-pool-executor.js.map +1 -1
- package/dist/web/browser/form-utilities.js +35 -34
- package/dist/web/browser/form-utilities.js.map +1 -1
- package/dist/web/browser/geo-location.js +1 -1
- package/dist/web/browser/geo-location.js.map +1 -1
- package/dist/web/browser/native-bridge.js +3 -1
- package/dist/web/browser/native-bridge.js.map +1 -1
- package/dist/web/cancellable-promise.js +34 -13
- package/dist/web/cancellable-promise.js.map +1 -1
- package/dist/web/clean-url-search-params.js +26 -3
- package/dist/web/clean-url-search-params.js.map +1 -1
- package/dist/web/collections/cache-dictionary.js +3 -3
- package/dist/web/collections/cache-dictionary.js.map +1 -1
- package/dist/web/collections/default-map.js.map +1 -1
- package/dist/web/collections/default-weak-map.js.map +1 -1
- package/dist/web/collections/from.js +3 -3
- package/dist/web/collections/from.js.map +1 -1
- package/dist/web/collections/index.js.map +1 -1
- package/dist/web/draft.js +46 -11
- package/dist/web/draft.js.map +1 -1
- package/dist/web/event-bus.js +1 -1
- package/dist/web/event-bus.js.map +1 -1
- package/dist/web/functions.js +336 -0
- package/dist/web/functions.js.map +1 -0
- package/dist/web/index.js +2 -0
- package/dist/web/index.js.map +1 -1
- package/dist/web/key-value-pair.js.map +1 -1
- package/dist/web/lazy.js.map +1 -1
- package/dist/web/math.js +3 -0
- package/dist/web/math.js.map +1 -1
- package/dist/web/order.js.map +1 -1
- package/dist/web/parse.js +10 -1
- package/dist/web/parse.js.map +1 -1
- package/dist/web/patch.js.map +1 -1
- package/dist/web/range.js +151 -0
- package/dist/web/range.js.map +1 -0
- package/dist/web/stopwatch.js.map +1 -1
- package/dist/web/string.js +8 -5
- package/dist/web/string.js.map +1 -1
- package/dist/web/timer.js +1 -1
- package/dist/web/timer.js.map +1 -1
- package/dist/web/timespan.js +21 -8
- package/dist/web/timespan.js.map +1 -1
- package/dist/web/type-code.js.map +1 -1
- package/dist/web/type.js +20 -19
- package/dist/web/type.js.map +1 -1
- package/dist/web/types.js.map +1 -1
- package/dist/web/utilities.js +174 -39
- package/dist/web/utilities.js.map +1 -1
- package/package.json +66 -60
- package/src/array.ts +116 -68
- package/src/async-pool-executor.ts +107 -85
- package/src/browser/form-utilities.ts +32 -31
- package/src/browser/geo-location.ts +113 -113
- package/src/browser/native-bridge.ts +118 -116
- package/src/cancellable-promise.ts +102 -76
- package/src/clean-url-search-params.ts +36 -11
- package/src/collections/cache-dictionary.ts +115 -115
- package/src/collections/default-map.ts +48 -48
- package/src/collections/default-weak-map.ts +51 -51
- package/src/collections/from.ts +859 -859
- package/src/collections/index.ts +3 -3
- package/src/draft.ts +55 -16
- package/src/event-bus.ts +83 -83
- package/src/functions.ts +496 -0
- package/src/index.ts +44 -42
- package/src/key-value-pair.ts +24 -24
- package/src/lazy.ts +32 -32
- package/src/math.ts +33 -30
- package/src/order.ts +5 -5
- package/src/parse.ts +251 -240
- package/src/patch.ts +79 -79
- package/src/range.ts +165 -0
- package/src/stopwatch.ts +43 -43
- package/src/string.ts +8 -5
- package/src/timer.ts +67 -67
- package/src/timespan.ts +412 -398
- package/src/type-code.ts +15 -15
- package/src/type.ts +372 -371
- package/src/types.ts +30 -30
- package/src/utilities.ts +653 -480
package/src/collections/from.ts
CHANGED
|
@@ -1,860 +1,860 @@
|
|
|
1
|
-
import type {Consumer, EqualityComparer, UnaryFunction, Predicate, Supplier, Optional, Nullable} from "../types.js";
|
|
2
|
-
import {shallowEqual, toPairs} from "../utilities.js";
|
|
3
|
-
import KeyValuePair from "../key-value-pair.js";
|
|
4
|
-
import Order from "../order.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Creates a new From instance from an Iterable or a Supplier of an Iterable.
|
|
9
|
-
* @template T - The type of elements in the source.
|
|
10
|
-
* @param {Iterable<T> | Supplier<Iterable<T>>} source - The source data.
|
|
11
|
-
* @returns {From<T> | From<KeyValuePair<any, any>>} A new From instance.
|
|
12
|
-
* @throws {
|
|
13
|
-
*/
|
|
14
|
-
export default function from<T>(source: Iterable<T> | Supplier<Iterable<T>>): From<T> | From<KeyValuePair<any, any>> {
|
|
15
|
-
if (source == null)
|
|
16
|
-
throw "Source is null.";
|
|
17
|
-
if (typeof source === 'function')
|
|
18
|
-
return From.fn<T>(source);
|
|
19
|
-
if (typeof source[Symbol.iterator] === 'function') {
|
|
20
|
-
return From.iterable<T>(source);
|
|
21
|
-
} else if (typeof source === 'object') {
|
|
22
|
-
return From.object(source);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
throw "Invalid source.";
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* A LINQ-like wrapper for iterables that provides various transformation and query methods.
|
|
30
|
-
* @template T - The type of elements in the sequence.
|
|
31
|
-
*/
|
|
32
|
-
export class From<T> implements Iterable<T> {
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Creates a From instance from an object's entries.
|
|
36
|
-
* @param {any} obj - The object to convert to pairs.
|
|
37
|
-
* @returns {From<KeyValuePair<any, any>>} A From instance of key-value pairs.
|
|
38
|
-
*/
|
|
39
|
-
public static object(obj: any): From<KeyValuePair<any, any>> {
|
|
40
|
-
return new From<KeyValuePair<any, any>>(function () {
|
|
41
|
-
return toPairs(obj);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
private readonly _fn: Supplier<Iterable<T>>
|
|
47
|
-
|
|
48
|
-
protected constructor(fn: Supplier<Iterable<T>>) {
|
|
49
|
-
this._fn = fn;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Creates a From instance from a supplier function.
|
|
54
|
-
* @template T
|
|
55
|
-
* @param {Supplier<Iterable<T>>} callable - A function that returns an Iterable.
|
|
56
|
-
* @returns {From<T>} A new From instance.
|
|
57
|
-
*/
|
|
58
|
-
public static fn<T>(callable: Supplier<Iterable<T>>): From<T> {
|
|
59
|
-
return new From<T>(callable);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Collects all elements into an array and returns a new From instance from it.
|
|
64
|
-
* Useful for freezing the sequence and preventing multiple iterations over a generator.
|
|
65
|
-
* @returns {From<T>} A new From instance with cached data.
|
|
66
|
-
*/
|
|
67
|
-
public collect(): From<T> {
|
|
68
|
-
const cache = Array.from(this);
|
|
69
|
-
return From.iterable(cache);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Creates a From instance from an Iterable.
|
|
74
|
-
* @template T
|
|
75
|
-
* @param {Iterable<T>} iterable - The source iterable.
|
|
76
|
-
* @returns {From<T>} A new From instance.
|
|
77
|
-
*/
|
|
78
|
-
public static iterable<T>(iterable: Iterable<T>): From<T> {
|
|
79
|
-
return From.fn<T>(function* () {
|
|
80
|
-
for (const item of iterable) {
|
|
81
|
-
yield item;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Maps each element of the sequence into a new form.
|
|
88
|
-
* @template R
|
|
89
|
-
* @param {UnaryFunction<T, R>} mapper - A transformation function.
|
|
90
|
-
* @returns {From<R>} A new From instance with the mapped elements.
|
|
91
|
-
*/
|
|
92
|
-
public map<R>(mapper: UnaryFunction<T, R>): From<R> {
|
|
93
|
-
const self = this;
|
|
94
|
-
return From.fn<R>(function* () {
|
|
95
|
-
for (let item of self) {
|
|
96
|
-
yield mapper(item);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
* [Symbol.iterator](): Iterator<T> {
|
|
102
|
-
yield* this._fn();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Checks if all elements in the sequence satisfy the given predicate.
|
|
108
|
-
* @param {Predicate<T>} predicate - A function to test each element.
|
|
109
|
-
* @returns {boolean} True if all elements satisfy the predicate.
|
|
110
|
-
*/
|
|
111
|
-
public all(predicate: Predicate<T>): boolean {
|
|
112
|
-
for (let item of this._fn()) {
|
|
113
|
-
if (!predicate(item)) {
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return true;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Checks if any element in the sequence satisfies the given predicate.
|
|
122
|
-
* If no predicate is provided, checks if the sequence is not empty.
|
|
123
|
-
* @param {Optional<Predicate<T>>} predicate - A function to test each element.
|
|
124
|
-
* @returns {boolean} True if any element satisfies the predicate.
|
|
125
|
-
*/
|
|
126
|
-
public any(predicate: Optional<Predicate<T>> = undefined): boolean {
|
|
127
|
-
for (let item of this) {
|
|
128
|
-
if (predicate == null || predicate(item)) {
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Gets the number of elements in the sequence.
|
|
137
|
-
* @returns {number} The count of elements.
|
|
138
|
-
*/
|
|
139
|
-
public get length(): number {
|
|
140
|
-
return this.count();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Counts the number of elements that satisfy the given predicate.
|
|
145
|
-
* If no predicate is provided, counts all elements.
|
|
146
|
-
* @param {Optional<Predicate<T>>} predicate - A function to test each element.
|
|
147
|
-
* @returns {number} The count of elements.
|
|
148
|
-
*/
|
|
149
|
-
public count(predicate: Optional<Predicate<T>> = undefined): number {
|
|
150
|
-
|
|
151
|
-
let output = 0;
|
|
152
|
-
|
|
153
|
-
for (let item of this) {
|
|
154
|
-
if (predicate == null || predicate(item)) {
|
|
155
|
-
output++;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return output;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Filters the sequence based on a predicate.
|
|
164
|
-
* @param {Predicate<T>} predicate - A function to test each element.
|
|
165
|
-
* @returns {From<T>} A new From instance with the filtered elements.
|
|
166
|
-
*/
|
|
167
|
-
public filter(predicate: Predicate<T>): From<T> {
|
|
168
|
-
const self = this;
|
|
169
|
-
return From.fn<T>(function* () {
|
|
170
|
-
for (let item of self) {
|
|
171
|
-
if (predicate(item)) {
|
|
172
|
-
yield item;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Checks if the sequence contains a specific value.
|
|
180
|
-
* @param {T} value - The value to search for.
|
|
181
|
-
* @returns {boolean} True if the value is found.
|
|
182
|
-
*/
|
|
183
|
-
public contains(value: T): boolean {
|
|
184
|
-
for (let item of this) {
|
|
185
|
-
if (item === value) {
|
|
186
|
-
return true;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Returns the first element of the sequence that satisfies a condition.
|
|
195
|
-
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
196
|
-
* @returns {T | undefined} The first element or undefined.
|
|
197
|
-
*/
|
|
198
|
-
public first(predicate?: Predicate<T>): T | undefined {
|
|
199
|
-
|
|
200
|
-
if (predicate) {
|
|
201
|
-
for (let item of this) {
|
|
202
|
-
if (predicate(item)) {
|
|
203
|
-
return item;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
} else {
|
|
207
|
-
// noinspection LoopStatementThatDoesntLoopJS
|
|
208
|
-
for (let item of this) {
|
|
209
|
-
return item;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return undefined;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Returns the only element of the sequence that satisfies a condition, or throws if more than one exists.
|
|
218
|
-
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
219
|
-
* @returns {T | null} The single element or null if not found.
|
|
220
|
-
* @throws {Error} If more than one element is found.
|
|
221
|
-
*/
|
|
222
|
-
public single(predicate?: Predicate<T>): T | null {
|
|
223
|
-
|
|
224
|
-
let found = false;
|
|
225
|
-
let output: T | null = null;
|
|
226
|
-
|
|
227
|
-
if (predicate) {
|
|
228
|
-
for (let item of this) {
|
|
229
|
-
if (predicate(item)) {
|
|
230
|
-
if (found) {
|
|
231
|
-
throw new Error("Sequence contains more than one element.");
|
|
232
|
-
}
|
|
233
|
-
found = true;
|
|
234
|
-
output = item;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
} else {
|
|
238
|
-
// noinspection LoopStatementThatDoesntLoopJS
|
|
239
|
-
for (let item of this) {
|
|
240
|
-
if (found) {
|
|
241
|
-
throw new Error("Sequence contains more than one element.");
|
|
242
|
-
}
|
|
243
|
-
found = true;
|
|
244
|
-
output = item;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return output;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Appends an item to the end of the sequence.
|
|
253
|
-
* @param {T} item - The item to append.
|
|
254
|
-
* @returns {From<T>} A new From instance.
|
|
255
|
-
*/
|
|
256
|
-
public append(item: T): From<T> {
|
|
257
|
-
const self = this;
|
|
258
|
-
return From.fn<T>(function* () {
|
|
259
|
-
for (let element of self) {
|
|
260
|
-
yield element;
|
|
261
|
-
}
|
|
262
|
-
yield item;
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Prepends an item to the beginning of the sequence.
|
|
268
|
-
* @param {T} item - The item to prepend.
|
|
269
|
-
* @returns {From<T>} A new From instance.
|
|
270
|
-
*/
|
|
271
|
-
public prepend(item: T): From<T> {
|
|
272
|
-
const self = this;
|
|
273
|
-
return From.fn<T>(function* () {
|
|
274
|
-
yield item;
|
|
275
|
-
for (let element of self) {
|
|
276
|
-
yield element;
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Gets the element at a specific index.
|
|
283
|
-
* @param {number} index - The zero-based index.
|
|
284
|
-
* @returns {T | undefined} The element at the index or undefined.
|
|
285
|
-
*/
|
|
286
|
-
public at(index: number): T | undefined {
|
|
287
|
-
let idx = 0;
|
|
288
|
-
for (let item of this) {
|
|
289
|
-
if (idx++ === index) {
|
|
290
|
-
return item;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return undefined;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Returns the last element of the sequence that satisfies a condition.
|
|
298
|
-
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
299
|
-
* @returns {T | undefined} The last element or undefined.
|
|
300
|
-
*/
|
|
301
|
-
public last(predicate?: Predicate<T>): T | undefined {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
let last: T | undefined = undefined;
|
|
305
|
-
|
|
306
|
-
if (predicate)
|
|
307
|
-
for (let item of this) {
|
|
308
|
-
if (!predicate || predicate(item)) {
|
|
309
|
-
last = item;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
else {
|
|
313
|
-
for (let item of this) {
|
|
314
|
-
last = item;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
return last;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Projects each element of a sequence to an Iterable and flattens the resulting sequences into one sequence.
|
|
322
|
-
* @template R
|
|
323
|
-
* @param {UnaryFunction<T, Iterable<R>>} mapper - A transformation function.
|
|
324
|
-
* @returns {From<R>} A new From instance.
|
|
325
|
-
*/
|
|
326
|
-
public mapMany<R>(mapper: UnaryFunction<T, Iterable<R>>): From<R> {
|
|
327
|
-
const self = this;
|
|
328
|
-
return From.fn(function* () {
|
|
329
|
-
for (const item of self) {
|
|
330
|
-
const subitems = mapper(item)
|
|
331
|
-
for (const subitem of subitems) {
|
|
332
|
-
yield subitem;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
})
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Flattens a sequence of iterables.
|
|
340
|
-
* @returns {From<any>} A new From instance.
|
|
341
|
-
*/
|
|
342
|
-
public flatten(): From<any> {
|
|
343
|
-
const self = this;
|
|
344
|
-
return From.fn<T>(function* () {
|
|
345
|
-
for (let item of self) {
|
|
346
|
-
let temp = <Iterable<any>>item;
|
|
347
|
-
for (let subitem of temp) {
|
|
348
|
-
yield subitem
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Calculates the sum of the elements in the sequence.
|
|
356
|
-
* @param {UnaryFunction<T, number>} [mapper] - Optional projection function.
|
|
357
|
-
* @returns {number} The sum.
|
|
358
|
-
*/
|
|
359
|
-
public sum(mapper?: UnaryFunction<T, number>): number {
|
|
360
|
-
let total = 0;
|
|
361
|
-
|
|
362
|
-
if (mapper)
|
|
363
|
-
for (let item of this) {
|
|
364
|
-
total += mapper(item);
|
|
365
|
-
}
|
|
366
|
-
else
|
|
367
|
-
for (let item of this) {
|
|
368
|
-
total += item as number;
|
|
369
|
-
}
|
|
370
|
-
return total;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* Calculates the average of the elements in the sequence.
|
|
375
|
-
* @param {UnaryFunction<T, number>} [mapper] - Optional projection function.
|
|
376
|
-
* @returns {number} The average.
|
|
377
|
-
*/
|
|
378
|
-
public avg(mapper?: UnaryFunction<T, number>): number {
|
|
379
|
-
let total = 0;
|
|
380
|
-
let count = 0;
|
|
381
|
-
|
|
382
|
-
if (mapper)
|
|
383
|
-
for (let item of this) {
|
|
384
|
-
total += mapper(item);
|
|
385
|
-
count++;
|
|
386
|
-
}
|
|
387
|
-
else
|
|
388
|
-
for (let item of this) {
|
|
389
|
-
total += item as number;
|
|
390
|
-
count++;
|
|
391
|
-
}
|
|
392
|
-
return count > 0 ? total / count : 0;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* Returns the maximum value in the sequence.
|
|
397
|
-
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
398
|
-
* @returns {number} The maximum value.
|
|
399
|
-
*/
|
|
400
|
-
public max(mapper: UnaryFunction<T, number>): number {
|
|
401
|
-
let max = -Infinity;
|
|
402
|
-
for (let item of this) {
|
|
403
|
-
let value = mapper(item);
|
|
404
|
-
max = value > max ? value : max;
|
|
405
|
-
}
|
|
406
|
-
return max;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* Returns the minimum value in the sequence.
|
|
411
|
-
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
412
|
-
* @returns {number} The minimum value.
|
|
413
|
-
*/
|
|
414
|
-
public min(mapper: UnaryFunction<T, number>): number {
|
|
415
|
-
let min = Infinity;
|
|
416
|
-
for (let item of this) {
|
|
417
|
-
let value = mapper(item);
|
|
418
|
-
min = value < min ? value : min;
|
|
419
|
-
}
|
|
420
|
-
return min;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* Returns the element with the maximum value.
|
|
425
|
-
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
426
|
-
* @returns {T | undefined} The element with the maximum value.
|
|
427
|
-
*/
|
|
428
|
-
public maxBy(mapper: UnaryFunction<T, number>): T | undefined {
|
|
429
|
-
let max = -Infinity;
|
|
430
|
-
let maxItem: T | undefined;
|
|
431
|
-
for (let item of this) {
|
|
432
|
-
let value = mapper(item);
|
|
433
|
-
if (value > max) {
|
|
434
|
-
max = value;
|
|
435
|
-
maxItem = item;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
return maxItem;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* Returns the element with the minimum value.
|
|
443
|
-
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
444
|
-
* @returns {T | undefined} The element with the minimum value.
|
|
445
|
-
*/
|
|
446
|
-
public minBy(mapper: UnaryFunction<T, number>): T | undefined {
|
|
447
|
-
let min = Infinity;
|
|
448
|
-
let minItem: T | undefined;
|
|
449
|
-
for (let item of this) {
|
|
450
|
-
let value = mapper(item);
|
|
451
|
-
if (value < min) {
|
|
452
|
-
min = value;
|
|
453
|
-
minItem = item;
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
return minItem;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
/**
|
|
460
|
-
* Sorts the elements of the sequence.
|
|
461
|
-
* @template R
|
|
462
|
-
* @param {UnaryFunction<T, R>} mapper - Projection function for the key to sort by.
|
|
463
|
-
* @param {Order} [order] - Ascending or Descending.
|
|
464
|
-
* @returns {From<T>} A new From instance.
|
|
465
|
-
*/
|
|
466
|
-
public orderBy<R>(mapper: UnaryFunction<T, R>, order: Order = Order.Ascending): From<T> {
|
|
467
|
-
const array = Array.from(this._fn());
|
|
468
|
-
if (order == Order.Ascending) {
|
|
469
|
-
array.sort((a, b) => {
|
|
470
|
-
const aValue = mapper(a);
|
|
471
|
-
const bValue = mapper(b);
|
|
472
|
-
return aValue > bValue ? 1 : bValue > aValue ? -1 : 0;
|
|
473
|
-
});
|
|
474
|
-
} else if (order == Order.Descending) {
|
|
475
|
-
array.sort((a, b) => {
|
|
476
|
-
const aValue = mapper(a);
|
|
477
|
-
const bValue = mapper(b);
|
|
478
|
-
return aValue > bValue ? 1 : bValue < aValue ? -1 : 0;
|
|
479
|
-
});
|
|
480
|
-
} else {
|
|
481
|
-
throw new Error("invalid order argument");
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
return From.iterable(array);
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
/**
|
|
488
|
-
* Groups the elements of the sequence.
|
|
489
|
-
* @template TKey
|
|
490
|
-
* @param {UnaryFunction<T, TKey>} keySelector - Function to extract the key for each element.
|
|
491
|
-
* @param {EqualityComparer<TKey>} [equalityComparer] - Optional comparer for keys.
|
|
492
|
-
* @returns {From<Grouping<TKey, T>>} A new From instance.
|
|
493
|
-
*/
|
|
494
|
-
public groupBy<TKey>(keySelector: UnaryFunction<T, TKey>, equalityComparer?: EqualityComparer<TKey>): From<Grouping<TKey, T>> {
|
|
495
|
-
|
|
496
|
-
equalityComparer = (equalityComparer == null ? shallowEqual : equalityComparer);
|
|
497
|
-
const self = this;
|
|
498
|
-
|
|
499
|
-
return From.fn<Grouping<TKey, T>>(function* () {
|
|
500
|
-
const groups: Array<[TKey, T[]]> = [];
|
|
501
|
-
for (let item of self) {
|
|
502
|
-
const key = keySelector(item);
|
|
503
|
-
let found = false;
|
|
504
|
-
for (let [entryKey, group] of groups) {
|
|
505
|
-
if (equalityComparer(key, entryKey)) {
|
|
506
|
-
group.push(item);
|
|
507
|
-
found = true;
|
|
508
|
-
break;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
if (!found) {
|
|
512
|
-
groups.push([key, [item]]);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
yield* groups.map(([key, values]) => new Grouping<TKey, T>(key, values));
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* Returns a specified number of contiguous elements from the start of the sequence.
|
|
521
|
-
* @param {number} n - The number of elements to return.
|
|
522
|
-
* @returns {From<T>} A new From instance.
|
|
523
|
-
*/
|
|
524
|
-
public head(n: number): From<T> {
|
|
525
|
-
|
|
526
|
-
const self = this;
|
|
527
|
-
return From.fn<T>(function* () {
|
|
528
|
-
let count = 0;
|
|
529
|
-
for (let item of self) {
|
|
530
|
-
if (count++ < n) {
|
|
531
|
-
yield item;
|
|
532
|
-
} else {
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
});
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
/**
|
|
540
|
-
* Returns a specified number of contiguous elements from the end of the sequence.
|
|
541
|
-
* @param {number} n - The number of elements to return.
|
|
542
|
-
* @returns {From<T>} A new From instance.
|
|
543
|
-
*/
|
|
544
|
-
public tail(n: number): From<T> {
|
|
545
|
-
|
|
546
|
-
const self = this;
|
|
547
|
-
return From.fn<T>(function* () {
|
|
548
|
-
let buffer = [];
|
|
549
|
-
for (let item of self) {
|
|
550
|
-
buffer.push(item);
|
|
551
|
-
if (buffer.length > n) {
|
|
552
|
-
buffer.shift();
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
yield* buffer;
|
|
556
|
-
});
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
/**
|
|
560
|
-
* Performs the specified action on each element of the sequence.
|
|
561
|
-
* @param {Consumer<T>} action - The action to perform.
|
|
562
|
-
*/
|
|
563
|
-
public forEach(action: Consumer<T>): void {
|
|
564
|
-
for (let item of this) {
|
|
565
|
-
action(item);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
/**
|
|
570
|
-
* Converts the sequence to an array.
|
|
571
|
-
* @returns {T[]} The array.
|
|
572
|
-
*/
|
|
573
|
-
public toArray(): T[] {
|
|
574
|
-
return Array.from(this);
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
/**
|
|
578
|
-
* Converts the sequence to a record.
|
|
579
|
-
* @template R
|
|
580
|
-
* @param {UnaryFunction<T, string | symbol | number>} keySelector - Key selector.
|
|
581
|
-
* @param {UnaryFunction<T, R>} valueSelector - Value selector.
|
|
582
|
-
* @returns {Record<string | symbol | number, R>} The record.
|
|
583
|
-
*/
|
|
584
|
-
public toRecord<R>(keySelector: UnaryFunction<T, string | symbol | number>, valueSelector: UnaryFunction<T, R>): Record<string | symbol | number, R> {
|
|
585
|
-
const output: Record<string | symbol | number, R> = {};
|
|
586
|
-
|
|
587
|
-
for (let item of this) {
|
|
588
|
-
output[keySelector(item)] = valueSelector(item);
|
|
589
|
-
}
|
|
590
|
-
return output;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
/**
|
|
594
|
-
* Converts the sequence to a Map.
|
|
595
|
-
* @template RKey, RValue
|
|
596
|
-
* @param {UnaryFunction<T, RKey>} keySelector - Key selector.
|
|
597
|
-
* @param {UnaryFunction<T, RValue>} valueSelector - Value selector.
|
|
598
|
-
* @returns {Map<RKey, RValue>} The map.
|
|
599
|
-
*/
|
|
600
|
-
public toMap<RKey, RValue>(keySelector: UnaryFunction<T, RKey>, valueSelector: UnaryFunction<T, RValue>): Map<RKey, RValue> {
|
|
601
|
-
const map = new Map<any, any>();
|
|
602
|
-
|
|
603
|
-
for (let item of this) {
|
|
604
|
-
const key = keySelector(item);
|
|
605
|
-
const value = valueSelector(item);
|
|
606
|
-
map.set(key, value);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
return map;
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
/**
|
|
614
|
-
* Returns elements that are instances of a specific primitive type.
|
|
615
|
-
* @param {string} type - The type string (e.g., 'string', 'number').
|
|
616
|
-
* @returns {From<T>} A new From instance.
|
|
617
|
-
*/
|
|
618
|
-
public instancesOf(type: string): From<T> {
|
|
619
|
-
return this.filter(item => typeof item === type);
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
/**
|
|
623
|
-
* Checks if all elements are instances of a specific class.
|
|
624
|
-
* @param {new (...args: any[]) => any} type - The class constructor.
|
|
625
|
-
* @returns {boolean} True if all elements are instances of the type.
|
|
626
|
-
*/
|
|
627
|
-
public allInstanceOf(type: new (...args: any[]) => any): boolean {
|
|
628
|
-
for (let item of this) {
|
|
629
|
-
if (!(item instanceof type)) {
|
|
630
|
-
return false;
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
return true;
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
/**
|
|
637
|
-
* Returns distinct elements from the sequence.
|
|
638
|
-
* @param {EqualityComparer<T>} [eq_comparer] - Optional equality comparer.
|
|
639
|
-
* @returns {From<T>} A new From instance.
|
|
640
|
-
*/
|
|
641
|
-
public distinct(eq_comparer?: EqualityComparer<T>): From<T> {
|
|
642
|
-
|
|
643
|
-
if (eq_comparer == null)
|
|
644
|
-
eq_comparer = shallowEqual;
|
|
645
|
-
|
|
646
|
-
const self = this;
|
|
647
|
-
return From.fn<T>(function* () {
|
|
648
|
-
|
|
649
|
-
const included = [];
|
|
650
|
-
|
|
651
|
-
for (let item of self) {
|
|
652
|
-
if (!From.iterable(included).any(x => eq_comparer(x, item))) {
|
|
653
|
-
included.push(item);
|
|
654
|
-
yield item;
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
/**
|
|
661
|
-
* Inserts an item at a specific index.
|
|
662
|
-
* @param {T} item - The item to insert.
|
|
663
|
-
* @param {number} index - The zero-based index.
|
|
664
|
-
* @returns {From<T>} A new From instance.
|
|
665
|
-
*/
|
|
666
|
-
public insert(item: T, index: number): From<T> {
|
|
667
|
-
|
|
668
|
-
const self = this;
|
|
669
|
-
return From.fn<T>(function* () {
|
|
670
|
-
let i = 0;
|
|
671
|
-
let inserted = false;
|
|
672
|
-
for (let existingItem of self) {
|
|
673
|
-
if (i === index) {
|
|
674
|
-
yield item;
|
|
675
|
-
inserted = true;
|
|
676
|
-
}
|
|
677
|
-
yield existingItem;
|
|
678
|
-
i++;
|
|
679
|
-
}
|
|
680
|
-
if (!inserted) {
|
|
681
|
-
yield item;
|
|
682
|
-
}
|
|
683
|
-
});
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
/**
|
|
687
|
-
* Bypasses a specified number of elements in a sequence and then returns the remaining elements.
|
|
688
|
-
* @param {number} n - The number of elements to skip.
|
|
689
|
-
* @returns {From<T>} A new From instance.
|
|
690
|
-
*/
|
|
691
|
-
public skip(n: number): From<T> {
|
|
692
|
-
const self = this;
|
|
693
|
-
return From.fn<T>(function* () {
|
|
694
|
-
let i = 0;
|
|
695
|
-
for (let item of self) {
|
|
696
|
-
if (i >= n) {
|
|
697
|
-
yield item;
|
|
698
|
-
}
|
|
699
|
-
i++;
|
|
700
|
-
}
|
|
701
|
-
});
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
/**
|
|
706
|
-
* Concatenates two sequences.
|
|
707
|
-
* @param {Iterable<T>} other - The other sequence.
|
|
708
|
-
* @returns {From<T>} A new From instance.
|
|
709
|
-
*/
|
|
710
|
-
public union(other: Iterable<T>): From<T> {
|
|
711
|
-
const self = this;
|
|
712
|
-
return From.fn<T>(function* () {
|
|
713
|
-
yield* self;
|
|
714
|
-
yield* other;
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
/**
|
|
719
|
-
* Correlates the elements of two sequences based on matching keys.
|
|
720
|
-
* @template T2, R, R2
|
|
721
|
-
* @param {Iterable<T2>} other - The other sequence.
|
|
722
|
-
* @param {UnaryFunction<T, R>} thisKeySelector - Key selector for this sequence.
|
|
723
|
-
* @param {UnaryFunction<T2, R>} otherKeySelector - Key selector for the other sequence.
|
|
724
|
-
* @param {(thisItem: T, otherItem: T2) => R2} resultSelector - Function to create a result element from two matching elements.
|
|
725
|
-
* @returns {From<R2>} A new From instance.
|
|
726
|
-
*/
|
|
727
|
-
public innerJoin<T2, R, R2>(other: Iterable<T2>, thisKeySelector: UnaryFunction<T, R>, otherKeySelector: UnaryFunction<T2, R>, resultSelector: (thisItem: T, otherItem: T2) => R2): From<R2> {
|
|
728
|
-
|
|
729
|
-
const self = this;
|
|
730
|
-
return From.fn<R2>(() => {
|
|
731
|
-
const otherByKey = new Map<R, T2>();
|
|
732
|
-
for (let item of other) {
|
|
733
|
-
otherByKey.set(otherKeySelector(item), item);
|
|
734
|
-
}
|
|
735
|
-
return Array.from(self)
|
|
736
|
-
.filter(item => otherByKey.has(thisKeySelector(item)))
|
|
737
|
-
.map(item => resultSelector(item, otherByKey.get(thisKeySelector(item))!));
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
/**
|
|
742
|
-
* Performs a left join on two sequences.
|
|
743
|
-
* @template U, K, R
|
|
744
|
-
* @param {Iterable<U>} other - The other sequence.
|
|
745
|
-
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
746
|
-
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
747
|
-
* @param {(thisItem: T, otherItem: U | undefined) => R} resultSelector - Function to create a result element.
|
|
748
|
-
* @returns {From<R>} A new From instance.
|
|
749
|
-
*/
|
|
750
|
-
public leftJoin<U, K, R>(other: Iterable<U>, thisKeySelector: UnaryFunction<T, K>, otherKeySelector: UnaryFunction<U, K>, resultSelector: (thisItem: T, otherItem: U | undefined) => R): From<R> {
|
|
751
|
-
|
|
752
|
-
const self = this;
|
|
753
|
-
return From.fn<R>(() => {
|
|
754
|
-
const otherByKey = new Map<K, U>();
|
|
755
|
-
for (let item of other) {
|
|
756
|
-
otherByKey.set(otherKeySelector(item), item);
|
|
757
|
-
}
|
|
758
|
-
return Array.from(self).map(item => resultSelector(item, otherByKey.get(thisKeySelector(item))));
|
|
759
|
-
});
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
/**
|
|
764
|
-
* Performs a left group join on two sequences.
|
|
765
|
-
* @template U, K, R
|
|
766
|
-
* @param {Iterable<U>} other - The other sequence.
|
|
767
|
-
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
768
|
-
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
769
|
-
* @param {(thisItem: T, otherItems: Array<U>) => R} resultSelector - Function to create a result element.
|
|
770
|
-
* @returns {From<R>} A new From instance.
|
|
771
|
-
*/
|
|
772
|
-
public leftGroupJoin<U, K, R>(other: Iterable<U>, thisKeySelector: UnaryFunction<T, K>, otherKeySelector: UnaryFunction<U, K>, resultSelector: (thisItem: T, otherItems: Array<U>) => R): From<R> {
|
|
773
|
-
|
|
774
|
-
const self = this;
|
|
775
|
-
return From.fn<R>(() => {
|
|
776
|
-
const otherByKeys = new Map<K, Array<U>>();
|
|
777
|
-
for (let item of other) {
|
|
778
|
-
const key = otherKeySelector(item);
|
|
779
|
-
if (!otherByKeys.has(key)) {
|
|
780
|
-
otherByKeys.set(key, []);
|
|
781
|
-
}
|
|
782
|
-
otherByKeys.get(key)!.push(item);
|
|
783
|
-
}
|
|
784
|
-
return Array.from(self).map(item => resultSelector(item, otherByKeys.get(thisKeySelector(item)) || []));
|
|
785
|
-
});
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
/**
|
|
789
|
-
* Performs a right group join on two sequences.
|
|
790
|
-
* @template U, K, R
|
|
791
|
-
* @param {Iterable<U>} other - The other sequence.
|
|
792
|
-
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
793
|
-
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
794
|
-
* @param {(thisItems: Array<T>, otherItem: U) => R} resultSelector - Function to create a result element.
|
|
795
|
-
* @returns {From<R>} A new From instance.
|
|
796
|
-
*/
|
|
797
|
-
public rightGroupJoin<U, K, R>(other: Iterable<U>, thisKeySelector: UnaryFunction<T, K>, otherKeySelector: UnaryFunction<U, K>, resultSelector: (thisItems: Array<T>, otherItem: U) => R): From<R> {
|
|
798
|
-
|
|
799
|
-
const self = this;
|
|
800
|
-
return From.fn<R>(() => {
|
|
801
|
-
const thisByKeys = new Map<K, Array<T>>();
|
|
802
|
-
for (let item of self) {
|
|
803
|
-
const key = thisKeySelector(item);
|
|
804
|
-
if (!thisByKeys.has(key)) {
|
|
805
|
-
thisByKeys.set(key, []);
|
|
806
|
-
}
|
|
807
|
-
thisByKeys.get(key)!.push(item);
|
|
808
|
-
}
|
|
809
|
-
return Array.from(other).map(item => resultSelector(thisByKeys.get(otherKeySelector(item)) || [], item));
|
|
810
|
-
});
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
/**
|
|
815
|
-
* Performs a right join on two sequences.
|
|
816
|
-
* @template U, K, R
|
|
817
|
-
* @param {Iterable<U>} other - The other sequence.
|
|
818
|
-
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
819
|
-
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
820
|
-
* @param {(thisItem: T | undefined, otherItem: U) => R} resultSelector - Function to create a result element.
|
|
821
|
-
* @returns {From<R>} A new From instance.
|
|
822
|
-
*/
|
|
823
|
-
public rightJoin<U, K, R>(other: Iterable<U>, thisKeySelector: UnaryFunction<T, K>, otherKeySelector: UnaryFunction<U, K>, resultSelector: (thisItem: T | undefined, otherItem: U) => R): From<R> {
|
|
824
|
-
|
|
825
|
-
const self = this;
|
|
826
|
-
return From.fn<R>(() => {
|
|
827
|
-
const thisByKey = new Map<K, T>();
|
|
828
|
-
for (let item of self) {
|
|
829
|
-
thisByKey.set(thisKeySelector(item), item);
|
|
830
|
-
}
|
|
831
|
-
return Array.from(other).map(item => resultSelector(thisByKey.get(otherKeySelector(item)), item));
|
|
832
|
-
});
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
/**
|
|
838
|
-
* Represents a group of elements that have a common key.
|
|
839
|
-
* @template TKey, T
|
|
840
|
-
*/
|
|
841
|
-
export class Grouping<TKey, T> extends From<T> {
|
|
842
|
-
|
|
843
|
-
private readonly _key: TKey;
|
|
844
|
-
|
|
845
|
-
/**
|
|
846
|
-
* Gets the key of the group.
|
|
847
|
-
*/
|
|
848
|
-
public get key(): TKey {
|
|
849
|
-
return this._key;
|
|
850
|
-
}
|
|
851
|
-
|
|
852
|
-
/**
|
|
853
|
-
* @param {TKey} key - The group key.
|
|
854
|
-
* @param {Array<T>} values - The values in the group.
|
|
855
|
-
*/
|
|
856
|
-
public constructor(key: TKey, values: Array<T>) {
|
|
857
|
-
super(() => values);
|
|
858
|
-
this._key = key;
|
|
859
|
-
}
|
|
1
|
+
import type {Consumer, EqualityComparer, UnaryFunction, Predicate, Supplier, Optional, Nullable} from "../types.js";
|
|
2
|
+
import {shallowEqual, toPairs} from "../utilities.js";
|
|
3
|
+
import KeyValuePair from "../key-value-pair.js";
|
|
4
|
+
import Order from "../order.js";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new From instance from an Iterable or a Supplier of an Iterable.
|
|
9
|
+
* @template T - The type of elements in the source.
|
|
10
|
+
* @param {Iterable<T> | Supplier<Iterable<T>>} source - The source data.
|
|
11
|
+
* @returns {From<T> | From<KeyValuePair<any, any>>} A new From instance.
|
|
12
|
+
* @throws {Error} If source is null or invalid.
|
|
13
|
+
*/
|
|
14
|
+
export default function from<T>(source: Iterable<T> | Supplier<Iterable<T>>): From<T> | From<KeyValuePair<any, any>> {
|
|
15
|
+
if (source == null)
|
|
16
|
+
throw new Error("Source is null.");
|
|
17
|
+
if (typeof source === 'function')
|
|
18
|
+
return From.fn<T>(source);
|
|
19
|
+
if (typeof source[Symbol.iterator] === 'function') {
|
|
20
|
+
return From.iterable<T>(source);
|
|
21
|
+
} else if (typeof source === 'object') {
|
|
22
|
+
return From.object(source);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
throw new Error("Invalid source.");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A LINQ-like wrapper for iterables that provides various transformation and query methods.
|
|
30
|
+
* @template T - The type of elements in the sequence.
|
|
31
|
+
*/
|
|
32
|
+
export class From<T> implements Iterable<T> {
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Creates a From instance from an object's entries.
|
|
36
|
+
* @param {any} obj - The object to convert to pairs.
|
|
37
|
+
* @returns {From<KeyValuePair<any, any>>} A From instance of key-value pairs.
|
|
38
|
+
*/
|
|
39
|
+
public static object(obj: any): From<KeyValuePair<any, any>> {
|
|
40
|
+
return new From<KeyValuePair<any, any>>(function () {
|
|
41
|
+
return toPairs(obj);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
private readonly _fn: Supplier<Iterable<T>>
|
|
47
|
+
|
|
48
|
+
protected constructor(fn: Supplier<Iterable<T>>) {
|
|
49
|
+
this._fn = fn;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a From instance from a supplier function.
|
|
54
|
+
* @template T
|
|
55
|
+
* @param {Supplier<Iterable<T>>} callable - A function that returns an Iterable.
|
|
56
|
+
* @returns {From<T>} A new From instance.
|
|
57
|
+
*/
|
|
58
|
+
public static fn<T>(callable: Supplier<Iterable<T>>): From<T> {
|
|
59
|
+
return new From<T>(callable);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Collects all elements into an array and returns a new From instance from it.
|
|
64
|
+
* Useful for freezing the sequence and preventing multiple iterations over a generator.
|
|
65
|
+
* @returns {From<T>} A new From instance with cached data.
|
|
66
|
+
*/
|
|
67
|
+
public collect(): From<T> {
|
|
68
|
+
const cache = Array.from(this);
|
|
69
|
+
return From.iterable(cache);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Creates a From instance from an Iterable.
|
|
74
|
+
* @template T
|
|
75
|
+
* @param {Iterable<T>} iterable - The source iterable.
|
|
76
|
+
* @returns {From<T>} A new From instance.
|
|
77
|
+
*/
|
|
78
|
+
public static iterable<T>(iterable: Iterable<T>): From<T> {
|
|
79
|
+
return From.fn<T>(function* () {
|
|
80
|
+
for (const item of iterable) {
|
|
81
|
+
yield item;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Maps each element of the sequence into a new form.
|
|
88
|
+
* @template R
|
|
89
|
+
* @param {UnaryFunction<T, R>} mapper - A transformation function.
|
|
90
|
+
* @returns {From<R>} A new From instance with the mapped elements.
|
|
91
|
+
*/
|
|
92
|
+
public map<R>(mapper: UnaryFunction<T, R>): From<R> {
|
|
93
|
+
const self = this;
|
|
94
|
+
return From.fn<R>(function* () {
|
|
95
|
+
for (let item of self) {
|
|
96
|
+
yield mapper(item);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
* [Symbol.iterator](): Iterator<T> {
|
|
102
|
+
yield* this._fn();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Checks if all elements in the sequence satisfy the given predicate.
|
|
108
|
+
* @param {Predicate<T>} predicate - A function to test each element.
|
|
109
|
+
* @returns {boolean} True if all elements satisfy the predicate.
|
|
110
|
+
*/
|
|
111
|
+
public all(predicate: Predicate<T>): boolean {
|
|
112
|
+
for (let item of this._fn()) {
|
|
113
|
+
if (!predicate(item)) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Checks if any element in the sequence satisfies the given predicate.
|
|
122
|
+
* If no predicate is provided, checks if the sequence is not empty.
|
|
123
|
+
* @param {Optional<Predicate<T>>} predicate - A function to test each element.
|
|
124
|
+
* @returns {boolean} True if any element satisfies the predicate.
|
|
125
|
+
*/
|
|
126
|
+
public any(predicate: Optional<Predicate<T>> = undefined): boolean {
|
|
127
|
+
for (let item of this) {
|
|
128
|
+
if (predicate == null || predicate(item)) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Gets the number of elements in the sequence.
|
|
137
|
+
* @returns {number} The count of elements.
|
|
138
|
+
*/
|
|
139
|
+
public get length(): number {
|
|
140
|
+
return this.count();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Counts the number of elements that satisfy the given predicate.
|
|
145
|
+
* If no predicate is provided, counts all elements.
|
|
146
|
+
* @param {Optional<Predicate<T>>} predicate - A function to test each element.
|
|
147
|
+
* @returns {number} The count of elements.
|
|
148
|
+
*/
|
|
149
|
+
public count(predicate: Optional<Predicate<T>> = undefined): number {
|
|
150
|
+
|
|
151
|
+
let output = 0;
|
|
152
|
+
|
|
153
|
+
for (let item of this) {
|
|
154
|
+
if (predicate == null || predicate(item)) {
|
|
155
|
+
output++;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return output;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Filters the sequence based on a predicate.
|
|
164
|
+
* @param {Predicate<T>} predicate - A function to test each element.
|
|
165
|
+
* @returns {From<T>} A new From instance with the filtered elements.
|
|
166
|
+
*/
|
|
167
|
+
public filter(predicate: Predicate<T>): From<T> {
|
|
168
|
+
const self = this;
|
|
169
|
+
return From.fn<T>(function* () {
|
|
170
|
+
for (let item of self) {
|
|
171
|
+
if (predicate(item)) {
|
|
172
|
+
yield item;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Checks if the sequence contains a specific value.
|
|
180
|
+
* @param {T} value - The value to search for.
|
|
181
|
+
* @returns {boolean} True if the value is found.
|
|
182
|
+
*/
|
|
183
|
+
public contains(value: T): boolean {
|
|
184
|
+
for (let item of this) {
|
|
185
|
+
if (item === value) {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Returns the first element of the sequence that satisfies a condition.
|
|
195
|
+
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
196
|
+
* @returns {T | undefined} The first element or undefined.
|
|
197
|
+
*/
|
|
198
|
+
public first(predicate?: Predicate<T>): T | undefined {
|
|
199
|
+
|
|
200
|
+
if (predicate) {
|
|
201
|
+
for (let item of this) {
|
|
202
|
+
if (predicate(item)) {
|
|
203
|
+
return item;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
// noinspection LoopStatementThatDoesntLoopJS
|
|
208
|
+
for (let item of this) {
|
|
209
|
+
return item;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Returns the only element of the sequence that satisfies a condition, or throws if more than one exists.
|
|
218
|
+
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
219
|
+
* @returns {T | null} The single element or null if not found.
|
|
220
|
+
* @throws {Error} If more than one element is found.
|
|
221
|
+
*/
|
|
222
|
+
public single(predicate?: Predicate<T>): T | null {
|
|
223
|
+
|
|
224
|
+
let found = false;
|
|
225
|
+
let output: T | null = null;
|
|
226
|
+
|
|
227
|
+
if (predicate) {
|
|
228
|
+
for (let item of this) {
|
|
229
|
+
if (predicate(item)) {
|
|
230
|
+
if (found) {
|
|
231
|
+
throw new Error("Sequence contains more than one element.");
|
|
232
|
+
}
|
|
233
|
+
found = true;
|
|
234
|
+
output = item;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
// noinspection LoopStatementThatDoesntLoopJS
|
|
239
|
+
for (let item of this) {
|
|
240
|
+
if (found) {
|
|
241
|
+
throw new Error("Sequence contains more than one element.");
|
|
242
|
+
}
|
|
243
|
+
found = true;
|
|
244
|
+
output = item;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return output;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Appends an item to the end of the sequence.
|
|
253
|
+
* @param {T} item - The item to append.
|
|
254
|
+
* @returns {From<T>} A new From instance.
|
|
255
|
+
*/
|
|
256
|
+
public append(item: T): From<T> {
|
|
257
|
+
const self = this;
|
|
258
|
+
return From.fn<T>(function* () {
|
|
259
|
+
for (let element of self) {
|
|
260
|
+
yield element;
|
|
261
|
+
}
|
|
262
|
+
yield item;
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Prepends an item to the beginning of the sequence.
|
|
268
|
+
* @param {T} item - The item to prepend.
|
|
269
|
+
* @returns {From<T>} A new From instance.
|
|
270
|
+
*/
|
|
271
|
+
public prepend(item: T): From<T> {
|
|
272
|
+
const self = this;
|
|
273
|
+
return From.fn<T>(function* () {
|
|
274
|
+
yield item;
|
|
275
|
+
for (let element of self) {
|
|
276
|
+
yield element;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Gets the element at a specific index.
|
|
283
|
+
* @param {number} index - The zero-based index.
|
|
284
|
+
* @returns {T | undefined} The element at the index or undefined.
|
|
285
|
+
*/
|
|
286
|
+
public at(index: number): T | undefined {
|
|
287
|
+
let idx = 0;
|
|
288
|
+
for (let item of this) {
|
|
289
|
+
if (idx++ === index) {
|
|
290
|
+
return item;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return undefined;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Returns the last element of the sequence that satisfies a condition.
|
|
298
|
+
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
299
|
+
* @returns {T | undefined} The last element or undefined.
|
|
300
|
+
*/
|
|
301
|
+
public last(predicate?: Predicate<T>): T | undefined {
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
let last: T | undefined = undefined;
|
|
305
|
+
|
|
306
|
+
if (predicate)
|
|
307
|
+
for (let item of this) {
|
|
308
|
+
if (!predicate || predicate(item)) {
|
|
309
|
+
last = item;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
for (let item of this) {
|
|
314
|
+
last = item;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return last;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Projects each element of a sequence to an Iterable and flattens the resulting sequences into one sequence.
|
|
322
|
+
* @template R
|
|
323
|
+
* @param {UnaryFunction<T, Iterable<R>>} mapper - A transformation function.
|
|
324
|
+
* @returns {From<R>} A new From instance.
|
|
325
|
+
*/
|
|
326
|
+
public mapMany<R>(mapper: UnaryFunction<T, Iterable<R>>): From<R> {
|
|
327
|
+
const self = this;
|
|
328
|
+
return From.fn(function* () {
|
|
329
|
+
for (const item of self) {
|
|
330
|
+
const subitems = mapper(item)
|
|
331
|
+
for (const subitem of subitems) {
|
|
332
|
+
yield subitem;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
})
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Flattens a sequence of iterables.
|
|
340
|
+
* @returns {From<any>} A new From instance.
|
|
341
|
+
*/
|
|
342
|
+
public flatten(): From<any> {
|
|
343
|
+
const self = this;
|
|
344
|
+
return From.fn<T>(function* () {
|
|
345
|
+
for (let item of self) {
|
|
346
|
+
let temp = <Iterable<any>>item;
|
|
347
|
+
for (let subitem of temp) {
|
|
348
|
+
yield subitem
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Calculates the sum of the elements in the sequence.
|
|
356
|
+
* @param {UnaryFunction<T, number>} [mapper] - Optional projection function.
|
|
357
|
+
* @returns {number} The sum.
|
|
358
|
+
*/
|
|
359
|
+
public sum(mapper?: UnaryFunction<T, number>): number {
|
|
360
|
+
let total = 0;
|
|
361
|
+
|
|
362
|
+
if (mapper)
|
|
363
|
+
for (let item of this) {
|
|
364
|
+
total += mapper(item);
|
|
365
|
+
}
|
|
366
|
+
else
|
|
367
|
+
for (let item of this) {
|
|
368
|
+
total += item as number;
|
|
369
|
+
}
|
|
370
|
+
return total;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Calculates the average of the elements in the sequence.
|
|
375
|
+
* @param {UnaryFunction<T, number>} [mapper] - Optional projection function.
|
|
376
|
+
* @returns {number} The average.
|
|
377
|
+
*/
|
|
378
|
+
public avg(mapper?: UnaryFunction<T, number>): number {
|
|
379
|
+
let total = 0;
|
|
380
|
+
let count = 0;
|
|
381
|
+
|
|
382
|
+
if (mapper)
|
|
383
|
+
for (let item of this) {
|
|
384
|
+
total += mapper(item);
|
|
385
|
+
count++;
|
|
386
|
+
}
|
|
387
|
+
else
|
|
388
|
+
for (let item of this) {
|
|
389
|
+
total += item as number;
|
|
390
|
+
count++;
|
|
391
|
+
}
|
|
392
|
+
return count > 0 ? total / count : 0;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Returns the maximum value in the sequence.
|
|
397
|
+
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
398
|
+
* @returns {number} The maximum value.
|
|
399
|
+
*/
|
|
400
|
+
public max(mapper: UnaryFunction<T, number>): number {
|
|
401
|
+
let max = -Infinity;
|
|
402
|
+
for (let item of this) {
|
|
403
|
+
let value = mapper(item);
|
|
404
|
+
max = value > max ? value : max;
|
|
405
|
+
}
|
|
406
|
+
return max;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Returns the minimum value in the sequence.
|
|
411
|
+
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
412
|
+
* @returns {number} The minimum value.
|
|
413
|
+
*/
|
|
414
|
+
public min(mapper: UnaryFunction<T, number>): number {
|
|
415
|
+
let min = Infinity;
|
|
416
|
+
for (let item of this) {
|
|
417
|
+
let value = mapper(item);
|
|
418
|
+
min = value < min ? value : min;
|
|
419
|
+
}
|
|
420
|
+
return min;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Returns the element with the maximum value.
|
|
425
|
+
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
426
|
+
* @returns {T | undefined} The element with the maximum value.
|
|
427
|
+
*/
|
|
428
|
+
public maxBy(mapper: UnaryFunction<T, number>): T | undefined {
|
|
429
|
+
let max = -Infinity;
|
|
430
|
+
let maxItem: T | undefined;
|
|
431
|
+
for (let item of this) {
|
|
432
|
+
let value = mapper(item);
|
|
433
|
+
if (value > max) {
|
|
434
|
+
max = value;
|
|
435
|
+
maxItem = item;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return maxItem;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Returns the element with the minimum value.
|
|
443
|
+
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
444
|
+
* @returns {T | undefined} The element with the minimum value.
|
|
445
|
+
*/
|
|
446
|
+
public minBy(mapper: UnaryFunction<T, number>): T | undefined {
|
|
447
|
+
let min = Infinity;
|
|
448
|
+
let minItem: T | undefined;
|
|
449
|
+
for (let item of this) {
|
|
450
|
+
let value = mapper(item);
|
|
451
|
+
if (value < min) {
|
|
452
|
+
min = value;
|
|
453
|
+
minItem = item;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return minItem;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Sorts the elements of the sequence.
|
|
461
|
+
* @template R
|
|
462
|
+
* @param {UnaryFunction<T, R>} mapper - Projection function for the key to sort by.
|
|
463
|
+
* @param {Order} [order] - Ascending or Descending.
|
|
464
|
+
* @returns {From<T>} A new From instance.
|
|
465
|
+
*/
|
|
466
|
+
public orderBy<R>(mapper: UnaryFunction<T, R>, order: Order = Order.Ascending): From<T> {
|
|
467
|
+
const array = Array.from(this._fn());
|
|
468
|
+
if (order == Order.Ascending) {
|
|
469
|
+
array.sort((a, b) => {
|
|
470
|
+
const aValue = mapper(a);
|
|
471
|
+
const bValue = mapper(b);
|
|
472
|
+
return aValue > bValue ? 1 : bValue > aValue ? -1 : 0;
|
|
473
|
+
});
|
|
474
|
+
} else if (order == Order.Descending) {
|
|
475
|
+
array.sort((a, b) => {
|
|
476
|
+
const aValue = mapper(a);
|
|
477
|
+
const bValue = mapper(b);
|
|
478
|
+
return aValue > bValue ? 1 : bValue < aValue ? -1 : 0;
|
|
479
|
+
});
|
|
480
|
+
} else {
|
|
481
|
+
throw new Error("invalid order argument");
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return From.iterable(array);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Groups the elements of the sequence.
|
|
489
|
+
* @template TKey
|
|
490
|
+
* @param {UnaryFunction<T, TKey>} keySelector - Function to extract the key for each element.
|
|
491
|
+
* @param {EqualityComparer<TKey>} [equalityComparer] - Optional comparer for keys.
|
|
492
|
+
* @returns {From<Grouping<TKey, T>>} A new From instance.
|
|
493
|
+
*/
|
|
494
|
+
public groupBy<TKey>(keySelector: UnaryFunction<T, TKey>, equalityComparer?: EqualityComparer<TKey>): From<Grouping<TKey, T>> {
|
|
495
|
+
|
|
496
|
+
equalityComparer = (equalityComparer == null ? shallowEqual : equalityComparer);
|
|
497
|
+
const self = this;
|
|
498
|
+
|
|
499
|
+
return From.fn<Grouping<TKey, T>>(function* () {
|
|
500
|
+
const groups: Array<[TKey, T[]]> = [];
|
|
501
|
+
for (let item of self) {
|
|
502
|
+
const key = keySelector(item);
|
|
503
|
+
let found = false;
|
|
504
|
+
for (let [entryKey, group] of groups) {
|
|
505
|
+
if (equalityComparer(key, entryKey)) {
|
|
506
|
+
group.push(item);
|
|
507
|
+
found = true;
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (!found) {
|
|
512
|
+
groups.push([key, [item]]);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
yield* groups.map(([key, values]) => new Grouping<TKey, T>(key, values));
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Returns a specified number of contiguous elements from the start of the sequence.
|
|
521
|
+
* @param {number} n - The number of elements to return.
|
|
522
|
+
* @returns {From<T>} A new From instance.
|
|
523
|
+
*/
|
|
524
|
+
public head(n: number): From<T> {
|
|
525
|
+
|
|
526
|
+
const self = this;
|
|
527
|
+
return From.fn<T>(function* () {
|
|
528
|
+
let count = 0;
|
|
529
|
+
for (let item of self) {
|
|
530
|
+
if (count++ < n) {
|
|
531
|
+
yield item;
|
|
532
|
+
} else {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Returns a specified number of contiguous elements from the end of the sequence.
|
|
541
|
+
* @param {number} n - The number of elements to return.
|
|
542
|
+
* @returns {From<T>} A new From instance.
|
|
543
|
+
*/
|
|
544
|
+
public tail(n: number): From<T> {
|
|
545
|
+
|
|
546
|
+
const self = this;
|
|
547
|
+
return From.fn<T>(function* () {
|
|
548
|
+
let buffer = [];
|
|
549
|
+
for (let item of self) {
|
|
550
|
+
buffer.push(item);
|
|
551
|
+
if (buffer.length > n) {
|
|
552
|
+
buffer.shift();
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
yield* buffer;
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Performs the specified action on each element of the sequence.
|
|
561
|
+
* @param {Consumer<T>} action - The action to perform.
|
|
562
|
+
*/
|
|
563
|
+
public forEach(action: Consumer<T>): void {
|
|
564
|
+
for (let item of this) {
|
|
565
|
+
action(item);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Converts the sequence to an array.
|
|
571
|
+
* @returns {T[]} The array.
|
|
572
|
+
*/
|
|
573
|
+
public toArray(): T[] {
|
|
574
|
+
return Array.from(this);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Converts the sequence to a record.
|
|
579
|
+
* @template R
|
|
580
|
+
* @param {UnaryFunction<T, string | symbol | number>} keySelector - Key selector.
|
|
581
|
+
* @param {UnaryFunction<T, R>} valueSelector - Value selector.
|
|
582
|
+
* @returns {Record<string | symbol | number, R>} The record.
|
|
583
|
+
*/
|
|
584
|
+
public toRecord<R>(keySelector: UnaryFunction<T, string | symbol | number>, valueSelector: UnaryFunction<T, R>): Record<string | symbol | number, R> {
|
|
585
|
+
const output: Record<string | symbol | number, R> = {};
|
|
586
|
+
|
|
587
|
+
for (let item of this) {
|
|
588
|
+
output[keySelector(item)] = valueSelector(item);
|
|
589
|
+
}
|
|
590
|
+
return output;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Converts the sequence to a Map.
|
|
595
|
+
* @template RKey, RValue
|
|
596
|
+
* @param {UnaryFunction<T, RKey>} keySelector - Key selector.
|
|
597
|
+
* @param {UnaryFunction<T, RValue>} valueSelector - Value selector.
|
|
598
|
+
* @returns {Map<RKey, RValue>} The map.
|
|
599
|
+
*/
|
|
600
|
+
public toMap<RKey, RValue>(keySelector: UnaryFunction<T, RKey>, valueSelector: UnaryFunction<T, RValue>): Map<RKey, RValue> {
|
|
601
|
+
const map = new Map<any, any>();
|
|
602
|
+
|
|
603
|
+
for (let item of this) {
|
|
604
|
+
const key = keySelector(item);
|
|
605
|
+
const value = valueSelector(item);
|
|
606
|
+
map.set(key, value);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
return map;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Returns elements that are instances of a specific primitive type.
|
|
615
|
+
* @param {string} type - The type string (e.g., 'string', 'number').
|
|
616
|
+
* @returns {From<T>} A new From instance.
|
|
617
|
+
*/
|
|
618
|
+
public instancesOf(type: string): From<T> {
|
|
619
|
+
return this.filter(item => typeof item === type);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Checks if all elements are instances of a specific class.
|
|
624
|
+
* @param {new (...args: any[]) => any} type - The class constructor.
|
|
625
|
+
* @returns {boolean} True if all elements are instances of the type.
|
|
626
|
+
*/
|
|
627
|
+
public allInstanceOf(type: new (...args: any[]) => any): boolean {
|
|
628
|
+
for (let item of this) {
|
|
629
|
+
if (!(item instanceof type)) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Returns distinct elements from the sequence.
|
|
638
|
+
* @param {EqualityComparer<T>} [eq_comparer] - Optional equality comparer.
|
|
639
|
+
* @returns {From<T>} A new From instance.
|
|
640
|
+
*/
|
|
641
|
+
public distinct(eq_comparer?: EqualityComparer<T>): From<T> {
|
|
642
|
+
|
|
643
|
+
if (eq_comparer == null)
|
|
644
|
+
eq_comparer = shallowEqual;
|
|
645
|
+
|
|
646
|
+
const self = this;
|
|
647
|
+
return From.fn<T>(function* () {
|
|
648
|
+
|
|
649
|
+
const included = [];
|
|
650
|
+
|
|
651
|
+
for (let item of self) {
|
|
652
|
+
if (!From.iterable(included).any(x => eq_comparer(x, item))) {
|
|
653
|
+
included.push(item);
|
|
654
|
+
yield item;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Inserts an item at a specific index.
|
|
662
|
+
* @param {T} item - The item to insert.
|
|
663
|
+
* @param {number} index - The zero-based index.
|
|
664
|
+
* @returns {From<T>} A new From instance.
|
|
665
|
+
*/
|
|
666
|
+
public insert(item: T, index: number): From<T> {
|
|
667
|
+
|
|
668
|
+
const self = this;
|
|
669
|
+
return From.fn<T>(function* () {
|
|
670
|
+
let i = 0;
|
|
671
|
+
let inserted = false;
|
|
672
|
+
for (let existingItem of self) {
|
|
673
|
+
if (i === index) {
|
|
674
|
+
yield item;
|
|
675
|
+
inserted = true;
|
|
676
|
+
}
|
|
677
|
+
yield existingItem;
|
|
678
|
+
i++;
|
|
679
|
+
}
|
|
680
|
+
if (!inserted) {
|
|
681
|
+
yield item;
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Bypasses a specified number of elements in a sequence and then returns the remaining elements.
|
|
688
|
+
* @param {number} n - The number of elements to skip.
|
|
689
|
+
* @returns {From<T>} A new From instance.
|
|
690
|
+
*/
|
|
691
|
+
public skip(n: number): From<T> {
|
|
692
|
+
const self = this;
|
|
693
|
+
return From.fn<T>(function* () {
|
|
694
|
+
let i = 0;
|
|
695
|
+
for (let item of self) {
|
|
696
|
+
if (i >= n) {
|
|
697
|
+
yield item;
|
|
698
|
+
}
|
|
699
|
+
i++;
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Concatenates two sequences.
|
|
707
|
+
* @param {Iterable<T>} other - The other sequence.
|
|
708
|
+
* @returns {From<T>} A new From instance.
|
|
709
|
+
*/
|
|
710
|
+
public union(other: Iterable<T>): From<T> {
|
|
711
|
+
const self = this;
|
|
712
|
+
return From.fn<T>(function* () {
|
|
713
|
+
yield* self;
|
|
714
|
+
yield* other;
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Correlates the elements of two sequences based on matching keys.
|
|
720
|
+
* @template T2, R, R2
|
|
721
|
+
* @param {Iterable<T2>} other - The other sequence.
|
|
722
|
+
* @param {UnaryFunction<T, R>} thisKeySelector - Key selector for this sequence.
|
|
723
|
+
* @param {UnaryFunction<T2, R>} otherKeySelector - Key selector for the other sequence.
|
|
724
|
+
* @param {(thisItem: T, otherItem: T2) => R2} resultSelector - Function to create a result element from two matching elements.
|
|
725
|
+
* @returns {From<R2>} A new From instance.
|
|
726
|
+
*/
|
|
727
|
+
public innerJoin<T2, R, R2>(other: Iterable<T2>, thisKeySelector: UnaryFunction<T, R>, otherKeySelector: UnaryFunction<T2, R>, resultSelector: (thisItem: T, otherItem: T2) => R2): From<R2> {
|
|
728
|
+
|
|
729
|
+
const self = this;
|
|
730
|
+
return From.fn<R2>(() => {
|
|
731
|
+
const otherByKey = new Map<R, T2>();
|
|
732
|
+
for (let item of other) {
|
|
733
|
+
otherByKey.set(otherKeySelector(item), item);
|
|
734
|
+
}
|
|
735
|
+
return Array.from(self)
|
|
736
|
+
.filter(item => otherByKey.has(thisKeySelector(item)))
|
|
737
|
+
.map(item => resultSelector(item, otherByKey.get(thisKeySelector(item))!));
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Performs a left join on two sequences.
|
|
743
|
+
* @template U, K, R
|
|
744
|
+
* @param {Iterable<U>} other - The other sequence.
|
|
745
|
+
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
746
|
+
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
747
|
+
* @param {(thisItem: T, otherItem: U | undefined) => R} resultSelector - Function to create a result element.
|
|
748
|
+
* @returns {From<R>} A new From instance.
|
|
749
|
+
*/
|
|
750
|
+
public leftJoin<U, K, R>(other: Iterable<U>, thisKeySelector: UnaryFunction<T, K>, otherKeySelector: UnaryFunction<U, K>, resultSelector: (thisItem: T, otherItem: U | undefined) => R): From<R> {
|
|
751
|
+
|
|
752
|
+
const self = this;
|
|
753
|
+
return From.fn<R>(() => {
|
|
754
|
+
const otherByKey = new Map<K, U>();
|
|
755
|
+
for (let item of other) {
|
|
756
|
+
otherByKey.set(otherKeySelector(item), item);
|
|
757
|
+
}
|
|
758
|
+
return Array.from(self).map(item => resultSelector(item, otherByKey.get(thisKeySelector(item))));
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Performs a left group join on two sequences.
|
|
765
|
+
* @template U, K, R
|
|
766
|
+
* @param {Iterable<U>} other - The other sequence.
|
|
767
|
+
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
768
|
+
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
769
|
+
* @param {(thisItem: T, otherItems: Array<U>) => R} resultSelector - Function to create a result element.
|
|
770
|
+
* @returns {From<R>} A new From instance.
|
|
771
|
+
*/
|
|
772
|
+
public leftGroupJoin<U, K, R>(other: Iterable<U>, thisKeySelector: UnaryFunction<T, K>, otherKeySelector: UnaryFunction<U, K>, resultSelector: (thisItem: T, otherItems: Array<U>) => R): From<R> {
|
|
773
|
+
|
|
774
|
+
const self = this;
|
|
775
|
+
return From.fn<R>(() => {
|
|
776
|
+
const otherByKeys = new Map<K, Array<U>>();
|
|
777
|
+
for (let item of other) {
|
|
778
|
+
const key = otherKeySelector(item);
|
|
779
|
+
if (!otherByKeys.has(key)) {
|
|
780
|
+
otherByKeys.set(key, []);
|
|
781
|
+
}
|
|
782
|
+
otherByKeys.get(key)!.push(item);
|
|
783
|
+
}
|
|
784
|
+
return Array.from(self).map(item => resultSelector(item, otherByKeys.get(thisKeySelector(item)) || []));
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* Performs a right group join on two sequences.
|
|
790
|
+
* @template U, K, R
|
|
791
|
+
* @param {Iterable<U>} other - The other sequence.
|
|
792
|
+
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
793
|
+
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
794
|
+
* @param {(thisItems: Array<T>, otherItem: U) => R} resultSelector - Function to create a result element.
|
|
795
|
+
* @returns {From<R>} A new From instance.
|
|
796
|
+
*/
|
|
797
|
+
public rightGroupJoin<U, K, R>(other: Iterable<U>, thisKeySelector: UnaryFunction<T, K>, otherKeySelector: UnaryFunction<U, K>, resultSelector: (thisItems: Array<T>, otherItem: U) => R): From<R> {
|
|
798
|
+
|
|
799
|
+
const self = this;
|
|
800
|
+
return From.fn<R>(() => {
|
|
801
|
+
const thisByKeys = new Map<K, Array<T>>();
|
|
802
|
+
for (let item of self) {
|
|
803
|
+
const key = thisKeySelector(item);
|
|
804
|
+
if (!thisByKeys.has(key)) {
|
|
805
|
+
thisByKeys.set(key, []);
|
|
806
|
+
}
|
|
807
|
+
thisByKeys.get(key)!.push(item);
|
|
808
|
+
}
|
|
809
|
+
return Array.from(other).map(item => resultSelector(thisByKeys.get(otherKeySelector(item)) || [], item));
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Performs a right join on two sequences.
|
|
816
|
+
* @template U, K, R
|
|
817
|
+
* @param {Iterable<U>} other - The other sequence.
|
|
818
|
+
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
819
|
+
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
820
|
+
* @param {(thisItem: T | undefined, otherItem: U) => R} resultSelector - Function to create a result element.
|
|
821
|
+
* @returns {From<R>} A new From instance.
|
|
822
|
+
*/
|
|
823
|
+
public rightJoin<U, K, R>(other: Iterable<U>, thisKeySelector: UnaryFunction<T, K>, otherKeySelector: UnaryFunction<U, K>, resultSelector: (thisItem: T | undefined, otherItem: U) => R): From<R> {
|
|
824
|
+
|
|
825
|
+
const self = this;
|
|
826
|
+
return From.fn<R>(() => {
|
|
827
|
+
const thisByKey = new Map<K, T>();
|
|
828
|
+
for (let item of self) {
|
|
829
|
+
thisByKey.set(thisKeySelector(item), item);
|
|
830
|
+
}
|
|
831
|
+
return Array.from(other).map(item => resultSelector(thisByKey.get(otherKeySelector(item)), item));
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* Represents a group of elements that have a common key.
|
|
839
|
+
* @template TKey, T
|
|
840
|
+
*/
|
|
841
|
+
export class Grouping<TKey, T> extends From<T> {
|
|
842
|
+
|
|
843
|
+
private readonly _key: TKey;
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Gets the key of the group.
|
|
847
|
+
*/
|
|
848
|
+
public get key(): TKey {
|
|
849
|
+
return this._key;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* @param {TKey} key - The group key.
|
|
854
|
+
* @param {Array<T>} values - The values in the group.
|
|
855
|
+
*/
|
|
856
|
+
public constructor(key: TKey, values: Array<T>) {
|
|
857
|
+
super(() => values);
|
|
858
|
+
this._key = key;
|
|
859
|
+
}
|
|
860
860
|
}
|