@develia/commons 0.4.14 → 0.4.16
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 +43 -1
- package/dist/cjs/array.js.map +1 -1
- package/dist/cjs/async-pool-executor.js +15 -0
- package/dist/cjs/async-pool-executor.js.map +1 -1
- package/dist/cjs/browser/form-utilities.js +26 -0
- package/dist/cjs/browser/form-utilities.js.map +1 -1
- package/dist/cjs/browser/geo-location.js +5 -0
- package/dist/cjs/browser/geo-location.js.map +1 -1
- package/dist/cjs/browser/index.js.map +1 -1
- package/dist/cjs/browser/native-bridge.js +9 -0
- package/dist/cjs/browser/native-bridge.js.map +1 -1
- package/dist/cjs/cancellable-promise.js.map +1 -1
- package/dist/cjs/clean-url-search-params.js +1 -0
- package/dist/cjs/clean-url-search-params.js.map +1 -1
- package/dist/cjs/collections/cache-dictionary.js +39 -0
- package/dist/cjs/collections/cache-dictionary.js.map +1 -1
- package/dist/cjs/collections/default-map.js +25 -1
- package/dist/cjs/collections/default-map.js.map +1 -1
- package/dist/cjs/collections/default-weak-map.js +23 -3
- package/dist/cjs/collections/default-weak-map.js.map +1 -1
- package/dist/cjs/{from.js → collections/from.js} +278 -4
- package/dist/cjs/collections/from.js.map +1 -0
- package/dist/cjs/collections/index.js +3 -1
- package/dist/cjs/collections/index.js.map +1 -1
- package/dist/cjs/draft.js +37 -0
- package/dist/cjs/draft.js.map +1 -1
- package/dist/cjs/event-bus.js +1 -0
- package/dist/cjs/event-bus.js.map +1 -1
- package/dist/cjs/index.js +16 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/key-value-pair.js +6 -0
- package/dist/cjs/key-value-pair.js.map +1 -1
- package/dist/cjs/lazy.js +4 -0
- package/dist/cjs/lazy.js.map +1 -1
- package/dist/cjs/math.js +19 -0
- package/dist/cjs/math.js.map +1 -1
- package/dist/cjs/order.js.map +1 -1
- package/dist/cjs/parse.js +96 -0
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/patch.js +10 -2
- 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 +10 -0
- package/dist/cjs/stopwatch.js.map +1 -1
- package/dist/cjs/string.js +62 -0
- package/dist/cjs/string.js.map +1 -1
- package/dist/cjs/timer.js +7 -0
- package/dist/cjs/timer.js.map +1 -1
- package/dist/cjs/timespan.js +202 -0
- package/dist/cjs/timespan.js.map +1 -1
- package/dist/cjs/type-code.js.map +1 -1
- package/dist/cjs/type.js +80 -1
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utilities.js +183 -0
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/array.js +42 -1
- package/dist/esm/array.js.map +1 -1
- package/dist/esm/async-pool-executor.js +15 -0
- package/dist/esm/async-pool-executor.js.map +1 -1
- package/dist/esm/browser/form-utilities.js +26 -0
- package/dist/esm/browser/form-utilities.js.map +1 -1
- package/dist/esm/browser/geo-location.js +5 -0
- package/dist/esm/browser/geo-location.js.map +1 -1
- package/dist/esm/browser/index.js.map +1 -1
- package/dist/esm/browser/native-bridge.js +9 -0
- package/dist/esm/browser/native-bridge.js.map +1 -1
- package/dist/esm/cancellable-promise.js.map +1 -1
- package/dist/esm/clean-url-search-params.js +1 -0
- package/dist/esm/clean-url-search-params.js.map +1 -1
- package/dist/esm/collections/cache-dictionary.js +39 -0
- package/dist/esm/collections/cache-dictionary.js.map +1 -1
- package/dist/esm/collections/default-map.js +25 -1
- package/dist/esm/collections/default-map.js.map +1 -1
- package/dist/esm/collections/default-weak-map.js +22 -2
- package/dist/esm/collections/default-weak-map.js.map +1 -1
- package/dist/esm/{from.js → collections/from.js} +278 -4
- package/dist/esm/collections/from.js.map +1 -0
- package/dist/esm/collections/index.js +1 -0
- package/dist/esm/collections/index.js.map +1 -1
- package/dist/esm/draft.js +37 -0
- package/dist/esm/draft.js.map +1 -1
- package/dist/esm/event-bus.js +1 -0
- package/dist/esm/event-bus.js.map +1 -1
- package/dist/esm/index.js +14 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/key-value-pair.js +6 -0
- package/dist/esm/key-value-pair.js.map +1 -1
- package/dist/esm/lazy.js +4 -0
- package/dist/esm/lazy.js.map +1 -1
- package/dist/esm/math.js +19 -0
- package/dist/esm/math.js.map +1 -1
- package/dist/esm/order.js.map +1 -1
- package/dist/esm/parse.js +96 -0
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/patch.js +10 -2
- 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 +10 -0
- package/dist/esm/stopwatch.js.map +1 -1
- package/dist/esm/string.js +62 -0
- package/dist/esm/string.js.map +1 -1
- package/dist/esm/timer.js +7 -0
- package/dist/esm/timer.js.map +1 -1
- package/dist/esm/timespan.js +202 -0
- package/dist/esm/timespan.js.map +1 -1
- package/dist/esm/type-code.js.map +1 -1
- package/dist/esm/type.js +80 -1
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utilities.js +183 -0
- package/dist/esm/utilities.js.map +1 -1
- package/dist/types/array.d.ts +35 -0
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/async-pool-executor.d.ts +15 -0
- package/dist/types/async-pool-executor.d.ts.map +1 -1
- package/dist/types/browser/form-utilities.d.ts +13 -0
- package/dist/types/browser/form-utilities.d.ts.map +1 -1
- package/dist/types/browser/index.d.ts.map +1 -1
- package/dist/types/browser/native-bridge.d.ts +6 -0
- package/dist/types/browser/native-bridge.d.ts.map +1 -1
- package/dist/types/collections/cache-dictionary.d.ts +39 -0
- package/dist/types/collections/cache-dictionary.d.ts.map +1 -1
- package/dist/types/collections/default-map.d.ts +23 -0
- package/dist/types/collections/default-map.d.ts.map +1 -1
- package/dist/types/collections/default-weak-map.d.ts +20 -1
- package/dist/types/collections/default-weak-map.d.ts.map +1 -1
- package/dist/types/collections/from.d.ts +333 -0
- package/dist/types/collections/from.d.ts.map +1 -0
- package/dist/types/collections/index.d.ts +1 -0
- package/dist/types/collections/index.d.ts.map +1 -1
- package/dist/types/draft.d.ts +68 -0
- package/dist/types/draft.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/key-value-pair.d.ts +6 -0
- package/dist/types/key-value-pair.d.ts.map +1 -1
- package/dist/types/lazy.d.ts +4 -0
- package/dist/types/lazy.d.ts.map +1 -1
- package/dist/types/math.d.ts +19 -0
- package/dist/types/math.d.ts.map +1 -1
- package/dist/types/parse.d.ts +121 -0
- 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 +51 -0
- package/dist/types/string.d.ts.map +1 -1
- package/dist/types/timer.d.ts +7 -0
- package/dist/types/timer.d.ts.map +1 -1
- package/dist/types/timespan.d.ts +202 -0
- package/dist/types/timespan.d.ts.map +1 -1
- package/dist/types/type.d.ts +40 -0
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/utilities.d.ts +171 -0
- package/dist/types/utilities.d.ts.map +1 -1
- package/dist/web/array.js +42 -1
- package/dist/web/array.js.map +1 -1
- package/dist/web/async-pool-executor.js +15 -0
- package/dist/web/async-pool-executor.js.map +1 -1
- package/dist/web/browser/form-utilities.js +26 -0
- package/dist/web/browser/form-utilities.js.map +1 -1
- package/dist/web/browser/geo-location.js +5 -0
- package/dist/web/browser/geo-location.js.map +1 -1
- package/dist/web/browser/index.js.map +1 -1
- package/dist/web/browser/native-bridge.js +9 -0
- package/dist/web/browser/native-bridge.js.map +1 -1
- package/dist/web/cancellable-promise.js.map +1 -1
- package/dist/web/clean-url-search-params.js +1 -0
- package/dist/web/clean-url-search-params.js.map +1 -1
- package/dist/web/collections/cache-dictionary.js +39 -0
- package/dist/web/collections/cache-dictionary.js.map +1 -1
- package/dist/web/collections/default-map.js +25 -1
- package/dist/web/collections/default-map.js.map +1 -1
- package/dist/web/collections/default-weak-map.js +22 -2
- package/dist/web/collections/default-weak-map.js.map +1 -1
- package/dist/web/{from.js → collections/from.js} +279 -5
- package/dist/web/collections/from.js.map +1 -0
- package/dist/web/collections/index.js +1 -0
- package/dist/web/collections/index.js.map +1 -1
- package/dist/web/draft.js +37 -0
- package/dist/web/draft.js.map +1 -1
- package/dist/web/event-bus.js +1 -0
- package/dist/web/event-bus.js.map +1 -1
- package/dist/web/index.js +14 -1
- package/dist/web/index.js.map +1 -1
- package/dist/web/key-value-pair.js +6 -0
- package/dist/web/key-value-pair.js.map +1 -1
- package/dist/web/lazy.js +4 -0
- package/dist/web/lazy.js.map +1 -1
- package/dist/web/math.js +19 -0
- package/dist/web/math.js.map +1 -1
- package/dist/web/order.js.map +1 -1
- package/dist/web/parse.js +96 -0
- package/dist/web/parse.js.map +1 -1
- package/dist/web/patch.js +10 -2
- 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 +10 -0
- package/dist/web/stopwatch.js.map +1 -1
- package/dist/web/string.js +62 -0
- package/dist/web/string.js.map +1 -1
- package/dist/web/timer.js +7 -0
- package/dist/web/timer.js.map +1 -1
- package/dist/web/timespan.js +202 -0
- package/dist/web/timespan.js.map +1 -1
- package/dist/web/type-code.js.map +1 -1
- package/dist/web/type.js +80 -1
- package/dist/web/type.js.map +1 -1
- package/dist/web/types.js.map +1 -1
- package/dist/web/utilities.js +183 -0
- package/dist/web/utilities.js.map +1 -1
- package/package.json +1 -1
- package/src/array.ts +16 -0
- package/src/browser/index.ts +2 -2
- package/src/collections/cache-dictionary.ts +39 -1
- package/src/collections/default-map.ts +15 -9
- package/src/collections/default-weak-map.ts +14 -12
- package/src/{from.ts → collections/from.ts} +285 -15
- package/src/collections/index.ts +4 -3
- package/src/index.ts +43 -42
- package/src/range.ts +165 -0
- package/src/timespan.ts +199 -20
- package/dist/cjs/from.js.map +0 -1
- package/dist/esm/from.js.map +0 -1
- package/dist/types/from.d.ts +0 -59
- package/dist/types/from.d.ts.map +0 -1
- package/dist/web/from.js.map +0 -1
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import { shallowEqual, toPairs } from "
|
|
2
|
-
import Order from "
|
|
1
|
+
import { shallowEqual, toPairs } from "../utilities.js";
|
|
2
|
+
import Order from "../order.js";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new From instance from an Iterable or a Supplier of an Iterable.
|
|
5
|
+
* @template T - The type of elements in the source.
|
|
6
|
+
* @param {Iterable<T> | Supplier<Iterable<T>>} source - The source data.
|
|
7
|
+
* @returns {From<T> | From<KeyValuePair<any, any>>} A new From instance.
|
|
8
|
+
* @throws {string} If source is null or invalid.
|
|
9
|
+
*/
|
|
3
10
|
export default function from(source) {
|
|
4
11
|
if (source == null)
|
|
5
12
|
throw "Source is null.";
|
|
@@ -13,7 +20,16 @@ export default function from(source) {
|
|
|
13
20
|
}
|
|
14
21
|
throw "Invalid source.";
|
|
15
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* A LINQ-like wrapper for iterables that provides various transformation and query methods.
|
|
25
|
+
* @template T - The type of elements in the sequence.
|
|
26
|
+
*/
|
|
16
27
|
export class From {
|
|
28
|
+
/**
|
|
29
|
+
* Creates a From instance from an object's entries.
|
|
30
|
+
* @param {any} obj - The object to convert to pairs.
|
|
31
|
+
* @returns {From<KeyValuePair<any, any>>} A From instance of key-value pairs.
|
|
32
|
+
*/
|
|
17
33
|
static object(obj) {
|
|
18
34
|
return new From(function () {
|
|
19
35
|
return toPairs(obj);
|
|
@@ -22,14 +38,30 @@ export class From {
|
|
|
22
38
|
constructor(fn) {
|
|
23
39
|
this._fn = fn;
|
|
24
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Creates a From instance from a supplier function.
|
|
43
|
+
* @template T
|
|
44
|
+
* @param {Supplier<Iterable<T>>} callable - A function that returns an Iterable.
|
|
45
|
+
* @returns {From<T>} A new From instance.
|
|
46
|
+
*/
|
|
25
47
|
static fn(callable) {
|
|
26
48
|
return new From(callable);
|
|
27
49
|
}
|
|
28
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Collects all elements into an array and returns a new From instance from it.
|
|
52
|
+
* Useful for freezing the sequence and preventing multiple iterations over a generator.
|
|
53
|
+
* @returns {From<T>} A new From instance with cached data.
|
|
54
|
+
*/
|
|
29
55
|
collect() {
|
|
30
56
|
const cache = Array.from(this);
|
|
31
57
|
return From.iterable(cache);
|
|
32
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates a From instance from an Iterable.
|
|
61
|
+
* @template T
|
|
62
|
+
* @param {Iterable<T>} iterable - The source iterable.
|
|
63
|
+
* @returns {From<T>} A new From instance.
|
|
64
|
+
*/
|
|
33
65
|
static iterable(iterable) {
|
|
34
66
|
return From.fn(function* () {
|
|
35
67
|
for (const item of iterable) {
|
|
@@ -37,7 +69,12 @@ export class From {
|
|
|
37
69
|
}
|
|
38
70
|
});
|
|
39
71
|
}
|
|
40
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Maps each element of the sequence into a new form.
|
|
74
|
+
* @template R
|
|
75
|
+
* @param {UnaryFunction<T, R>} mapper - A transformation function.
|
|
76
|
+
* @returns {From<R>} A new From instance with the mapped elements.
|
|
77
|
+
*/
|
|
41
78
|
map(mapper) {
|
|
42
79
|
const self = this;
|
|
43
80
|
return From.fn(function* () {
|
|
@@ -49,6 +86,11 @@ export class From {
|
|
|
49
86
|
*[Symbol.iterator]() {
|
|
50
87
|
yield* this._fn();
|
|
51
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Checks if all elements in the sequence satisfy the given predicate.
|
|
91
|
+
* @param {Predicate<T>} predicate - A function to test each element.
|
|
92
|
+
* @returns {boolean} True if all elements satisfy the predicate.
|
|
93
|
+
*/
|
|
52
94
|
all(predicate) {
|
|
53
95
|
for (let item of this._fn()) {
|
|
54
96
|
if (!predicate(item)) {
|
|
@@ -57,6 +99,12 @@ export class From {
|
|
|
57
99
|
}
|
|
58
100
|
return true;
|
|
59
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Checks if any element in the sequence satisfies the given predicate.
|
|
104
|
+
* If no predicate is provided, checks if the sequence is not empty.
|
|
105
|
+
* @param {Optional<Predicate<T>>} predicate - A function to test each element.
|
|
106
|
+
* @returns {boolean} True if any element satisfies the predicate.
|
|
107
|
+
*/
|
|
60
108
|
any(predicate = undefined) {
|
|
61
109
|
for (let item of this) {
|
|
62
110
|
if (predicate == null || predicate(item)) {
|
|
@@ -65,9 +113,19 @@ export class From {
|
|
|
65
113
|
}
|
|
66
114
|
return false;
|
|
67
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Gets the number of elements in the sequence.
|
|
118
|
+
* @returns {number} The count of elements.
|
|
119
|
+
*/
|
|
68
120
|
get length() {
|
|
69
121
|
return this.count();
|
|
70
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Counts the number of elements that satisfy the given predicate.
|
|
125
|
+
* If no predicate is provided, counts all elements.
|
|
126
|
+
* @param {Optional<Predicate<T>>} predicate - A function to test each element.
|
|
127
|
+
* @returns {number} The count of elements.
|
|
128
|
+
*/
|
|
71
129
|
count(predicate = undefined) {
|
|
72
130
|
let output = 0;
|
|
73
131
|
for (let item of this) {
|
|
@@ -77,6 +135,11 @@ export class From {
|
|
|
77
135
|
}
|
|
78
136
|
return output;
|
|
79
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Filters the sequence based on a predicate.
|
|
140
|
+
* @param {Predicate<T>} predicate - A function to test each element.
|
|
141
|
+
* @returns {From<T>} A new From instance with the filtered elements.
|
|
142
|
+
*/
|
|
80
143
|
filter(predicate) {
|
|
81
144
|
const self = this;
|
|
82
145
|
return From.fn(function* () {
|
|
@@ -87,6 +150,11 @@ export class From {
|
|
|
87
150
|
}
|
|
88
151
|
});
|
|
89
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Checks if the sequence contains a specific value.
|
|
155
|
+
* @param {T} value - The value to search for.
|
|
156
|
+
* @returns {boolean} True if the value is found.
|
|
157
|
+
*/
|
|
90
158
|
contains(value) {
|
|
91
159
|
for (let item of this) {
|
|
92
160
|
if (item === value) {
|
|
@@ -95,6 +163,11 @@ export class From {
|
|
|
95
163
|
}
|
|
96
164
|
return false;
|
|
97
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Returns the first element of the sequence that satisfies a condition.
|
|
168
|
+
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
169
|
+
* @returns {T | undefined} The first element or undefined.
|
|
170
|
+
*/
|
|
98
171
|
first(predicate) {
|
|
99
172
|
if (predicate) {
|
|
100
173
|
for (let item of this) {
|
|
@@ -104,12 +177,19 @@ export class From {
|
|
|
104
177
|
}
|
|
105
178
|
}
|
|
106
179
|
else {
|
|
180
|
+
// noinspection LoopStatementThatDoesntLoopJS
|
|
107
181
|
for (let item of this) {
|
|
108
182
|
return item;
|
|
109
183
|
}
|
|
110
184
|
}
|
|
111
185
|
return undefined;
|
|
112
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Returns the only element of the sequence that satisfies a condition, or throws if more than one exists.
|
|
189
|
+
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
190
|
+
* @returns {T | null} The single element or null if not found.
|
|
191
|
+
* @throws {Error} If more than one element is found.
|
|
192
|
+
*/
|
|
113
193
|
single(predicate) {
|
|
114
194
|
let found = false;
|
|
115
195
|
let output = null;
|
|
@@ -125,6 +205,7 @@ export class From {
|
|
|
125
205
|
}
|
|
126
206
|
}
|
|
127
207
|
else {
|
|
208
|
+
// noinspection LoopStatementThatDoesntLoopJS
|
|
128
209
|
for (let item of this) {
|
|
129
210
|
if (found) {
|
|
130
211
|
throw new Error("Sequence contains more than one element.");
|
|
@@ -135,6 +216,11 @@ export class From {
|
|
|
135
216
|
}
|
|
136
217
|
return output;
|
|
137
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Appends an item to the end of the sequence.
|
|
221
|
+
* @param {T} item - The item to append.
|
|
222
|
+
* @returns {From<T>} A new From instance.
|
|
223
|
+
*/
|
|
138
224
|
append(item) {
|
|
139
225
|
const self = this;
|
|
140
226
|
return From.fn(function* () {
|
|
@@ -144,6 +230,11 @@ export class From {
|
|
|
144
230
|
yield item;
|
|
145
231
|
});
|
|
146
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Prepends an item to the beginning of the sequence.
|
|
235
|
+
* @param {T} item - The item to prepend.
|
|
236
|
+
* @returns {From<T>} A new From instance.
|
|
237
|
+
*/
|
|
147
238
|
prepend(item) {
|
|
148
239
|
const self = this;
|
|
149
240
|
return From.fn(function* () {
|
|
@@ -153,6 +244,11 @@ export class From {
|
|
|
153
244
|
}
|
|
154
245
|
});
|
|
155
246
|
}
|
|
247
|
+
/**
|
|
248
|
+
* Gets the element at a specific index.
|
|
249
|
+
* @param {number} index - The zero-based index.
|
|
250
|
+
* @returns {T | undefined} The element at the index or undefined.
|
|
251
|
+
*/
|
|
156
252
|
at(index) {
|
|
157
253
|
let idx = 0;
|
|
158
254
|
for (let item of this) {
|
|
@@ -162,6 +258,11 @@ export class From {
|
|
|
162
258
|
}
|
|
163
259
|
return undefined;
|
|
164
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Returns the last element of the sequence that satisfies a condition.
|
|
263
|
+
* @param {Predicate<T>} [predicate] - Optional condition.
|
|
264
|
+
* @returns {T | undefined} The last element or undefined.
|
|
265
|
+
*/
|
|
165
266
|
last(predicate) {
|
|
166
267
|
let last = undefined;
|
|
167
268
|
if (predicate)
|
|
@@ -177,6 +278,12 @@ export class From {
|
|
|
177
278
|
}
|
|
178
279
|
return last;
|
|
179
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Projects each element of a sequence to an Iterable and flattens the resulting sequences into one sequence.
|
|
283
|
+
* @template R
|
|
284
|
+
* @param {UnaryFunction<T, Iterable<R>>} mapper - A transformation function.
|
|
285
|
+
* @returns {From<R>} A new From instance.
|
|
286
|
+
*/
|
|
180
287
|
mapMany(mapper) {
|
|
181
288
|
const self = this;
|
|
182
289
|
return From.fn(function* () {
|
|
@@ -188,6 +295,10 @@ export class From {
|
|
|
188
295
|
}
|
|
189
296
|
});
|
|
190
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Flattens a sequence of iterables.
|
|
300
|
+
* @returns {From<any>} A new From instance.
|
|
301
|
+
*/
|
|
191
302
|
flatten() {
|
|
192
303
|
const self = this;
|
|
193
304
|
return From.fn(function* () {
|
|
@@ -199,6 +310,11 @@ export class From {
|
|
|
199
310
|
}
|
|
200
311
|
});
|
|
201
312
|
}
|
|
313
|
+
/**
|
|
314
|
+
* Calculates the sum of the elements in the sequence.
|
|
315
|
+
* @param {UnaryFunction<T, number>} [mapper] - Optional projection function.
|
|
316
|
+
* @returns {number} The sum.
|
|
317
|
+
*/
|
|
202
318
|
sum(mapper) {
|
|
203
319
|
let total = 0;
|
|
204
320
|
if (mapper)
|
|
@@ -211,6 +327,11 @@ export class From {
|
|
|
211
327
|
}
|
|
212
328
|
return total;
|
|
213
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* Calculates the average of the elements in the sequence.
|
|
332
|
+
* @param {UnaryFunction<T, number>} [mapper] - Optional projection function.
|
|
333
|
+
* @returns {number} The average.
|
|
334
|
+
*/
|
|
214
335
|
avg(mapper) {
|
|
215
336
|
let total = 0;
|
|
216
337
|
let count = 0;
|
|
@@ -226,6 +347,11 @@ export class From {
|
|
|
226
347
|
}
|
|
227
348
|
return count > 0 ? total / count : 0;
|
|
228
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Returns the maximum value in the sequence.
|
|
352
|
+
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
353
|
+
* @returns {number} The maximum value.
|
|
354
|
+
*/
|
|
229
355
|
max(mapper) {
|
|
230
356
|
let max = -Infinity;
|
|
231
357
|
for (let item of this) {
|
|
@@ -234,6 +360,11 @@ export class From {
|
|
|
234
360
|
}
|
|
235
361
|
return max;
|
|
236
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Returns the minimum value in the sequence.
|
|
365
|
+
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
366
|
+
* @returns {number} The minimum value.
|
|
367
|
+
*/
|
|
237
368
|
min(mapper) {
|
|
238
369
|
let min = Infinity;
|
|
239
370
|
for (let item of this) {
|
|
@@ -242,6 +373,11 @@ export class From {
|
|
|
242
373
|
}
|
|
243
374
|
return min;
|
|
244
375
|
}
|
|
376
|
+
/**
|
|
377
|
+
* Returns the element with the maximum value.
|
|
378
|
+
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
379
|
+
* @returns {T | undefined} The element with the maximum value.
|
|
380
|
+
*/
|
|
245
381
|
maxBy(mapper) {
|
|
246
382
|
let max = -Infinity;
|
|
247
383
|
let maxItem;
|
|
@@ -254,6 +390,11 @@ export class From {
|
|
|
254
390
|
}
|
|
255
391
|
return maxItem;
|
|
256
392
|
}
|
|
393
|
+
/**
|
|
394
|
+
* Returns the element with the minimum value.
|
|
395
|
+
* @param {UnaryFunction<T, number>} mapper - Projection function.
|
|
396
|
+
* @returns {T | undefined} The element with the minimum value.
|
|
397
|
+
*/
|
|
257
398
|
minBy(mapper) {
|
|
258
399
|
let min = Infinity;
|
|
259
400
|
let minItem;
|
|
@@ -266,6 +407,13 @@ export class From {
|
|
|
266
407
|
}
|
|
267
408
|
return minItem;
|
|
268
409
|
}
|
|
410
|
+
/**
|
|
411
|
+
* Sorts the elements of the sequence.
|
|
412
|
+
* @template R
|
|
413
|
+
* @param {UnaryFunction<T, R>} mapper - Projection function for the key to sort by.
|
|
414
|
+
* @param {Order} [order] - Ascending or Descending.
|
|
415
|
+
* @returns {From<T>} A new From instance.
|
|
416
|
+
*/
|
|
269
417
|
orderBy(mapper, order = Order.Ascending) {
|
|
270
418
|
const array = Array.from(this._fn());
|
|
271
419
|
if (order == Order.Ascending) {
|
|
@@ -287,6 +435,13 @@ export class From {
|
|
|
287
435
|
}
|
|
288
436
|
return From.iterable(array);
|
|
289
437
|
}
|
|
438
|
+
/**
|
|
439
|
+
* Groups the elements of the sequence.
|
|
440
|
+
* @template TKey
|
|
441
|
+
* @param {UnaryFunction<T, TKey>} keySelector - Function to extract the key for each element.
|
|
442
|
+
* @param {EqualityComparer<TKey>} [equalityComparer] - Optional comparer for keys.
|
|
443
|
+
* @returns {From<Grouping<TKey, T>>} A new From instance.
|
|
444
|
+
*/
|
|
290
445
|
groupBy(keySelector, equalityComparer) {
|
|
291
446
|
equalityComparer = (equalityComparer == null ? shallowEqual : equalityComparer);
|
|
292
447
|
const self = this;
|
|
@@ -309,6 +464,11 @@ export class From {
|
|
|
309
464
|
yield* groups.map(([key, values]) => new Grouping(key, values));
|
|
310
465
|
});
|
|
311
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* Returns a specified number of contiguous elements from the start of the sequence.
|
|
469
|
+
* @param {number} n - The number of elements to return.
|
|
470
|
+
* @returns {From<T>} A new From instance.
|
|
471
|
+
*/
|
|
312
472
|
head(n) {
|
|
313
473
|
const self = this;
|
|
314
474
|
return From.fn(function* () {
|
|
@@ -323,6 +483,11 @@ export class From {
|
|
|
323
483
|
}
|
|
324
484
|
});
|
|
325
485
|
}
|
|
486
|
+
/**
|
|
487
|
+
* Returns a specified number of contiguous elements from the end of the sequence.
|
|
488
|
+
* @param {number} n - The number of elements to return.
|
|
489
|
+
* @returns {From<T>} A new From instance.
|
|
490
|
+
*/
|
|
326
491
|
tail(n) {
|
|
327
492
|
const self = this;
|
|
328
493
|
return From.fn(function* () {
|
|
@@ -336,14 +501,29 @@ export class From {
|
|
|
336
501
|
yield* buffer;
|
|
337
502
|
});
|
|
338
503
|
}
|
|
504
|
+
/**
|
|
505
|
+
* Performs the specified action on each element of the sequence.
|
|
506
|
+
* @param {Consumer<T>} action - The action to perform.
|
|
507
|
+
*/
|
|
339
508
|
forEach(action) {
|
|
340
509
|
for (let item of this) {
|
|
341
510
|
action(item);
|
|
342
511
|
}
|
|
343
512
|
}
|
|
513
|
+
/**
|
|
514
|
+
* Converts the sequence to an array.
|
|
515
|
+
* @returns {T[]} The array.
|
|
516
|
+
*/
|
|
344
517
|
toArray() {
|
|
345
518
|
return Array.from(this);
|
|
346
519
|
}
|
|
520
|
+
/**
|
|
521
|
+
* Converts the sequence to a record.
|
|
522
|
+
* @template R
|
|
523
|
+
* @param {UnaryFunction<T, string | symbol | number>} keySelector - Key selector.
|
|
524
|
+
* @param {UnaryFunction<T, R>} valueSelector - Value selector.
|
|
525
|
+
* @returns {Record<string | symbol | number, R>} The record.
|
|
526
|
+
*/
|
|
347
527
|
toRecord(keySelector, valueSelector) {
|
|
348
528
|
const output = {};
|
|
349
529
|
for (let item of this) {
|
|
@@ -351,6 +531,13 @@ export class From {
|
|
|
351
531
|
}
|
|
352
532
|
return output;
|
|
353
533
|
}
|
|
534
|
+
/**
|
|
535
|
+
* Converts the sequence to a Map.
|
|
536
|
+
* @template RKey, RValue
|
|
537
|
+
* @param {UnaryFunction<T, RKey>} keySelector - Key selector.
|
|
538
|
+
* @param {UnaryFunction<T, RValue>} valueSelector - Value selector.
|
|
539
|
+
* @returns {Map<RKey, RValue>} The map.
|
|
540
|
+
*/
|
|
354
541
|
toMap(keySelector, valueSelector) {
|
|
355
542
|
const map = new Map();
|
|
356
543
|
for (let item of this) {
|
|
@@ -360,9 +547,19 @@ export class From {
|
|
|
360
547
|
}
|
|
361
548
|
return map;
|
|
362
549
|
}
|
|
550
|
+
/**
|
|
551
|
+
* Returns elements that are instances of a specific primitive type.
|
|
552
|
+
* @param {string} type - The type string (e.g., 'string', 'number').
|
|
553
|
+
* @returns {From<T>} A new From instance.
|
|
554
|
+
*/
|
|
363
555
|
instancesOf(type) {
|
|
364
556
|
return this.filter(item => typeof item === type);
|
|
365
557
|
}
|
|
558
|
+
/**
|
|
559
|
+
* Checks if all elements are instances of a specific class.
|
|
560
|
+
* @param {new (...args: any[]) => any} type - The class constructor.
|
|
561
|
+
* @returns {boolean} True if all elements are instances of the type.
|
|
562
|
+
*/
|
|
366
563
|
allInstanceOf(type) {
|
|
367
564
|
for (let item of this) {
|
|
368
565
|
if (!(item instanceof type)) {
|
|
@@ -371,6 +568,11 @@ export class From {
|
|
|
371
568
|
}
|
|
372
569
|
return true;
|
|
373
570
|
}
|
|
571
|
+
/**
|
|
572
|
+
* Returns distinct elements from the sequence.
|
|
573
|
+
* @param {EqualityComparer<T>} [eq_comparer] - Optional equality comparer.
|
|
574
|
+
* @returns {From<T>} A new From instance.
|
|
575
|
+
*/
|
|
374
576
|
distinct(eq_comparer) {
|
|
375
577
|
if (eq_comparer == null)
|
|
376
578
|
eq_comparer = shallowEqual;
|
|
@@ -385,6 +587,12 @@ export class From {
|
|
|
385
587
|
}
|
|
386
588
|
});
|
|
387
589
|
}
|
|
590
|
+
/**
|
|
591
|
+
* Inserts an item at a specific index.
|
|
592
|
+
* @param {T} item - The item to insert.
|
|
593
|
+
* @param {number} index - The zero-based index.
|
|
594
|
+
* @returns {From<T>} A new From instance.
|
|
595
|
+
*/
|
|
388
596
|
insert(item, index) {
|
|
389
597
|
const self = this;
|
|
390
598
|
return From.fn(function* () {
|
|
@@ -403,6 +611,11 @@ export class From {
|
|
|
403
611
|
}
|
|
404
612
|
});
|
|
405
613
|
}
|
|
614
|
+
/**
|
|
615
|
+
* Bypasses a specified number of elements in a sequence and then returns the remaining elements.
|
|
616
|
+
* @param {number} n - The number of elements to skip.
|
|
617
|
+
* @returns {From<T>} A new From instance.
|
|
618
|
+
*/
|
|
406
619
|
skip(n) {
|
|
407
620
|
const self = this;
|
|
408
621
|
return From.fn(function* () {
|
|
@@ -415,6 +628,11 @@ export class From {
|
|
|
415
628
|
}
|
|
416
629
|
});
|
|
417
630
|
}
|
|
631
|
+
/**
|
|
632
|
+
* Concatenates two sequences.
|
|
633
|
+
* @param {Iterable<T>} other - The other sequence.
|
|
634
|
+
* @returns {From<T>} A new From instance.
|
|
635
|
+
*/
|
|
418
636
|
union(other) {
|
|
419
637
|
const self = this;
|
|
420
638
|
return From.fn(function* () {
|
|
@@ -422,6 +640,15 @@ export class From {
|
|
|
422
640
|
yield* other;
|
|
423
641
|
});
|
|
424
642
|
}
|
|
643
|
+
/**
|
|
644
|
+
* Correlates the elements of two sequences based on matching keys.
|
|
645
|
+
* @template T2, R, R2
|
|
646
|
+
* @param {Iterable<T2>} other - The other sequence.
|
|
647
|
+
* @param {UnaryFunction<T, R>} thisKeySelector - Key selector for this sequence.
|
|
648
|
+
* @param {UnaryFunction<T2, R>} otherKeySelector - Key selector for the other sequence.
|
|
649
|
+
* @param {(thisItem: T, otherItem: T2) => R2} resultSelector - Function to create a result element from two matching elements.
|
|
650
|
+
* @returns {From<R2>} A new From instance.
|
|
651
|
+
*/
|
|
425
652
|
innerJoin(other, thisKeySelector, otherKeySelector, resultSelector) {
|
|
426
653
|
const self = this;
|
|
427
654
|
return From.fn(() => {
|
|
@@ -434,6 +661,15 @@ export class From {
|
|
|
434
661
|
.map(item => resultSelector(item, otherByKey.get(thisKeySelector(item))));
|
|
435
662
|
});
|
|
436
663
|
}
|
|
664
|
+
/**
|
|
665
|
+
* Performs a left join on two sequences.
|
|
666
|
+
* @template U, K, R
|
|
667
|
+
* @param {Iterable<U>} other - The other sequence.
|
|
668
|
+
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
669
|
+
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
670
|
+
* @param {(thisItem: T, otherItem: U | undefined) => R} resultSelector - Function to create a result element.
|
|
671
|
+
* @returns {From<R>} A new From instance.
|
|
672
|
+
*/
|
|
437
673
|
leftJoin(other, thisKeySelector, otherKeySelector, resultSelector) {
|
|
438
674
|
const self = this;
|
|
439
675
|
return From.fn(() => {
|
|
@@ -444,6 +680,15 @@ export class From {
|
|
|
444
680
|
return Array.from(self).map(item => resultSelector(item, otherByKey.get(thisKeySelector(item))));
|
|
445
681
|
});
|
|
446
682
|
}
|
|
683
|
+
/**
|
|
684
|
+
* Performs a left group join on two sequences.
|
|
685
|
+
* @template U, K, R
|
|
686
|
+
* @param {Iterable<U>} other - The other sequence.
|
|
687
|
+
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
688
|
+
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
689
|
+
* @param {(thisItem: T, otherItems: Array<U>) => R} resultSelector - Function to create a result element.
|
|
690
|
+
* @returns {From<R>} A new From instance.
|
|
691
|
+
*/
|
|
447
692
|
leftGroupJoin(other, thisKeySelector, otherKeySelector, resultSelector) {
|
|
448
693
|
const self = this;
|
|
449
694
|
return From.fn(() => {
|
|
@@ -458,6 +703,15 @@ export class From {
|
|
|
458
703
|
return Array.from(self).map(item => resultSelector(item, otherByKeys.get(thisKeySelector(item)) || []));
|
|
459
704
|
});
|
|
460
705
|
}
|
|
706
|
+
/**
|
|
707
|
+
* Performs a right group join on two sequences.
|
|
708
|
+
* @template U, K, R
|
|
709
|
+
* @param {Iterable<U>} other - The other sequence.
|
|
710
|
+
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
711
|
+
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
712
|
+
* @param {(thisItems: Array<T>, otherItem: U) => R} resultSelector - Function to create a result element.
|
|
713
|
+
* @returns {From<R>} A new From instance.
|
|
714
|
+
*/
|
|
461
715
|
rightGroupJoin(other, thisKeySelector, otherKeySelector, resultSelector) {
|
|
462
716
|
const self = this;
|
|
463
717
|
return From.fn(() => {
|
|
@@ -472,6 +726,15 @@ export class From {
|
|
|
472
726
|
return Array.from(other).map(item => resultSelector(thisByKeys.get(otherKeySelector(item)) || [], item));
|
|
473
727
|
});
|
|
474
728
|
}
|
|
729
|
+
/**
|
|
730
|
+
* Performs a right join on two sequences.
|
|
731
|
+
* @template U, K, R
|
|
732
|
+
* @param {Iterable<U>} other - The other sequence.
|
|
733
|
+
* @param {UnaryFunction<T, K>} thisKeySelector - Key selector for this sequence.
|
|
734
|
+
* @param {UnaryFunction<U, K>} otherKeySelector - Key selector for the other sequence.
|
|
735
|
+
* @param {(thisItem: T | undefined, otherItem: U) => R} resultSelector - Function to create a result element.
|
|
736
|
+
* @returns {From<R>} A new From instance.
|
|
737
|
+
*/
|
|
475
738
|
rightJoin(other, thisKeySelector, otherKeySelector, resultSelector) {
|
|
476
739
|
const self = this;
|
|
477
740
|
return From.fn(() => {
|
|
@@ -483,10 +746,21 @@ export class From {
|
|
|
483
746
|
});
|
|
484
747
|
}
|
|
485
748
|
}
|
|
749
|
+
/**
|
|
750
|
+
* Represents a group of elements that have a common key.
|
|
751
|
+
* @template TKey, T
|
|
752
|
+
*/
|
|
486
753
|
export class Grouping extends From {
|
|
754
|
+
/**
|
|
755
|
+
* Gets the key of the group.
|
|
756
|
+
*/
|
|
487
757
|
get key() {
|
|
488
758
|
return this._key;
|
|
489
759
|
}
|
|
760
|
+
/**
|
|
761
|
+
* @param {TKey} key - The group key.
|
|
762
|
+
* @param {Array<T>} values - The values in the group.
|
|
763
|
+
*/
|
|
490
764
|
constructor(key, values) {
|
|
491
765
|
super(() => values);
|
|
492
766
|
this._key = key;
|