@develia/commons 0.4.13 → 0.4.15
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 +27 -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 +33 -6
- 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 +43 -2
- 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 +14 -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/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 +213 -7
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/array.js +27 -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 +33 -6
- 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 +43 -2
- 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 +13 -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/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 +212 -7
- package/dist/esm/utilities.js.map +1 -1
- package/dist/types/array.d.ts +26 -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 +4 -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/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 +174 -2
- package/dist/types/utilities.d.ts.map +1 -1
- package/dist/web/array.js +27 -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 +33 -6
- 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 +43 -2
- 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 +13 -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/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 +212 -7
- package/dist/web/utilities.js.map +1 -1
- package/package.json +1 -1
- package/src/browser/form-utilities.ts +7 -6
- 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/draft.ts +6 -2
- package/src/index.ts +1 -1
- package/src/timespan.ts +199 -20
- package/src/utilities.ts +53 -9
- 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,9 +1,16 @@
|
|
|
1
|
-
import type {Consumer, EqualityComparer, UnaryFunction, Predicate, Supplier, Optional, Nullable} from "
|
|
2
|
-
import {shallowEqual, toPairs} from "
|
|
3
|
-
import KeyValuePair from "
|
|
4
|
-
import Order from "
|
|
5
|
-
|
|
6
|
-
|
|
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 {string} If source is null or invalid.
|
|
13
|
+
*/
|
|
7
14
|
export default function from<T>(source: Iterable<T> | Supplier<Iterable<T>>): From<T> | From<KeyValuePair<any, any>> {
|
|
8
15
|
if (source == null)
|
|
9
16
|
throw "Source is null.";
|
|
@@ -12,14 +19,23 @@ export default function from<T>(source: Iterable<T> | Supplier<Iterable<T>>): Fr
|
|
|
12
19
|
if (typeof source[Symbol.iterator] === 'function') {
|
|
13
20
|
return From.iterable<T>(source);
|
|
14
21
|
} else if (typeof source === 'object') {
|
|
15
|
-
return From.object(source)
|
|
22
|
+
return From.object(source);
|
|
16
23
|
}
|
|
17
24
|
|
|
18
25
|
throw "Invalid source.";
|
|
19
26
|
}
|
|
20
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
|
+
*/
|
|
21
32
|
export class From<T> implements Iterable<T> {
|
|
22
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
|
+
*/
|
|
23
39
|
public static object(obj: any): From<KeyValuePair<any, any>> {
|
|
24
40
|
return new From<KeyValuePair<any, any>>(function () {
|
|
25
41
|
return toPairs(obj);
|
|
@@ -33,37 +49,65 @@ export class From<T> implements Iterable<T> {
|
|
|
33
49
|
this._fn = fn;
|
|
34
50
|
}
|
|
35
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
|
+
*/
|
|
36
58
|
public static fn<T>(callable: Supplier<Iterable<T>>): From<T> {
|
|
37
59
|
return new From<T>(callable);
|
|
38
|
-
}
|
|
60
|
+
}
|
|
39
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
|
+
*/
|
|
40
67
|
public collect(): From<T> {
|
|
41
68
|
const cache = Array.from(this);
|
|
42
69
|
return From.iterable(cache);
|
|
43
70
|
}
|
|
44
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
|
+
*/
|
|
45
78
|
public static iterable<T>(iterable: Iterable<T>): From<T> {
|
|
46
79
|
return From.fn<T>(function* () {
|
|
47
80
|
for (const item of iterable) {
|
|
48
81
|
yield item;
|
|
49
82
|
}
|
|
50
83
|
});
|
|
51
|
-
}
|
|
84
|
+
}
|
|
52
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
|
+
*/
|
|
53
92
|
public map<R>(mapper: UnaryFunction<T, R>): From<R> {
|
|
54
93
|
const self = this;
|
|
55
94
|
return From.fn<R>(function* () {
|
|
56
95
|
for (let item of self) {
|
|
57
96
|
yield mapper(item);
|
|
58
97
|
}
|
|
59
|
-
})
|
|
98
|
+
});
|
|
60
99
|
}
|
|
61
100
|
|
|
62
101
|
* [Symbol.iterator](): Iterator<T> {
|
|
63
|
-
yield* this._fn()
|
|
102
|
+
yield* this._fn();
|
|
64
103
|
}
|
|
65
104
|
|
|
66
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
|
+
*/
|
|
67
111
|
public all(predicate: Predicate<T>): boolean {
|
|
68
112
|
for (let item of this._fn()) {
|
|
69
113
|
if (!predicate(item)) {
|
|
@@ -73,6 +117,12 @@ export class From<T> implements Iterable<T> {
|
|
|
73
117
|
return true;
|
|
74
118
|
}
|
|
75
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
|
+
*/
|
|
76
126
|
public any(predicate: Optional<Predicate<T>> = undefined): boolean {
|
|
77
127
|
for (let item of this) {
|
|
78
128
|
if (predicate == null || predicate(item)) {
|
|
@@ -82,10 +132,20 @@ export class From<T> implements Iterable<T> {
|
|
|
82
132
|
return false;
|
|
83
133
|
}
|
|
84
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Gets the number of elements in the sequence.
|
|
137
|
+
* @returns {number} The count of elements.
|
|
138
|
+
*/
|
|
85
139
|
public get length(): number {
|
|
86
140
|
return this.count();
|
|
87
141
|
}
|
|
88
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
|
+
*/
|
|
89
149
|
public count(predicate: Optional<Predicate<T>> = undefined): number {
|
|
90
150
|
|
|
91
151
|
let output = 0;
|
|
@@ -99,6 +159,11 @@ export class From<T> implements Iterable<T> {
|
|
|
99
159
|
return output;
|
|
100
160
|
}
|
|
101
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
|
+
*/
|
|
102
167
|
public filter(predicate: Predicate<T>): From<T> {
|
|
103
168
|
const self = this;
|
|
104
169
|
return From.fn<T>(function* () {
|
|
@@ -110,6 +175,11 @@ export class From<T> implements Iterable<T> {
|
|
|
110
175
|
});
|
|
111
176
|
}
|
|
112
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
|
+
*/
|
|
113
183
|
public contains(value: T): boolean {
|
|
114
184
|
for (let item of this) {
|
|
115
185
|
if (item === value) {
|
|
@@ -120,7 +190,11 @@ export class From<T> implements Iterable<T> {
|
|
|
120
190
|
}
|
|
121
191
|
|
|
122
192
|
|
|
123
|
-
|
|
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
|
+
*/
|
|
124
198
|
public first(predicate?: Predicate<T>): T | undefined {
|
|
125
199
|
|
|
126
200
|
if (predicate) {
|
|
@@ -139,7 +213,12 @@ export class From<T> implements Iterable<T> {
|
|
|
139
213
|
return undefined;
|
|
140
214
|
}
|
|
141
215
|
|
|
142
|
-
|
|
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
|
+
*/
|
|
143
222
|
public single(predicate?: Predicate<T>): T | null {
|
|
144
223
|
|
|
145
224
|
let found = false;
|
|
@@ -169,6 +248,11 @@ export class From<T> implements Iterable<T> {
|
|
|
169
248
|
return output;
|
|
170
249
|
}
|
|
171
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
|
+
*/
|
|
172
256
|
public append(item: T): From<T> {
|
|
173
257
|
const self = this;
|
|
174
258
|
return From.fn<T>(function* () {
|
|
@@ -179,6 +263,11 @@ export class From<T> implements Iterable<T> {
|
|
|
179
263
|
});
|
|
180
264
|
}
|
|
181
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
|
+
*/
|
|
182
271
|
public prepend(item: T): From<T> {
|
|
183
272
|
const self = this;
|
|
184
273
|
return From.fn<T>(function* () {
|
|
@@ -189,6 +278,11 @@ export class From<T> implements Iterable<T> {
|
|
|
189
278
|
});
|
|
190
279
|
}
|
|
191
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
|
+
*/
|
|
192
286
|
public at(index: number): T | undefined {
|
|
193
287
|
let idx = 0;
|
|
194
288
|
for (let item of this) {
|
|
@@ -199,6 +293,11 @@ export class From<T> implements Iterable<T> {
|
|
|
199
293
|
return undefined;
|
|
200
294
|
}
|
|
201
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
|
+
*/
|
|
202
301
|
public last(predicate?: Predicate<T>): T | undefined {
|
|
203
302
|
|
|
204
303
|
|
|
@@ -218,6 +317,12 @@ export class From<T> implements Iterable<T> {
|
|
|
218
317
|
return last;
|
|
219
318
|
}
|
|
220
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
|
+
*/
|
|
221
326
|
public mapMany<R>(mapper: UnaryFunction<T, Iterable<R>>): From<R> {
|
|
222
327
|
const self = this;
|
|
223
328
|
return From.fn(function* () {
|
|
@@ -230,6 +335,10 @@ export class From<T> implements Iterable<T> {
|
|
|
230
335
|
})
|
|
231
336
|
}
|
|
232
337
|
|
|
338
|
+
/**
|
|
339
|
+
* Flattens a sequence of iterables.
|
|
340
|
+
* @returns {From<any>} A new From instance.
|
|
341
|
+
*/
|
|
233
342
|
public flatten(): From<any> {
|
|
234
343
|
const self = this;
|
|
235
344
|
return From.fn<T>(function* () {
|
|
@@ -242,6 +351,11 @@ export class From<T> implements Iterable<T> {
|
|
|
242
351
|
});
|
|
243
352
|
}
|
|
244
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
|
+
*/
|
|
245
359
|
public sum(mapper?: UnaryFunction<T, number>): number {
|
|
246
360
|
let total = 0;
|
|
247
361
|
|
|
@@ -256,6 +370,11 @@ export class From<T> implements Iterable<T> {
|
|
|
256
370
|
return total;
|
|
257
371
|
}
|
|
258
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
|
+
*/
|
|
259
378
|
public avg(mapper?: UnaryFunction<T, number>): number {
|
|
260
379
|
let total = 0;
|
|
261
380
|
let count = 0;
|
|
@@ -273,6 +392,11 @@ export class From<T> implements Iterable<T> {
|
|
|
273
392
|
return count > 0 ? total / count : 0;
|
|
274
393
|
}
|
|
275
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
|
+
*/
|
|
276
400
|
public max(mapper: UnaryFunction<T, number>): number {
|
|
277
401
|
let max = -Infinity;
|
|
278
402
|
for (let item of this) {
|
|
@@ -282,6 +406,11 @@ export class From<T> implements Iterable<T> {
|
|
|
282
406
|
return max;
|
|
283
407
|
}
|
|
284
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
|
+
*/
|
|
285
414
|
public min(mapper: UnaryFunction<T, number>): number {
|
|
286
415
|
let min = Infinity;
|
|
287
416
|
for (let item of this) {
|
|
@@ -291,6 +420,11 @@ export class From<T> implements Iterable<T> {
|
|
|
291
420
|
return min;
|
|
292
421
|
}
|
|
293
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
|
+
*/
|
|
294
428
|
public maxBy(mapper: UnaryFunction<T, number>): T | undefined {
|
|
295
429
|
let max = -Infinity;
|
|
296
430
|
let maxItem: T | undefined;
|
|
@@ -304,6 +438,11 @@ export class From<T> implements Iterable<T> {
|
|
|
304
438
|
return maxItem;
|
|
305
439
|
}
|
|
306
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
|
+
*/
|
|
307
446
|
public minBy(mapper: UnaryFunction<T, number>): T | undefined {
|
|
308
447
|
let min = Infinity;
|
|
309
448
|
let minItem: T | undefined;
|
|
@@ -317,6 +456,13 @@ export class From<T> implements Iterable<T> {
|
|
|
317
456
|
return minItem;
|
|
318
457
|
}
|
|
319
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
|
+
*/
|
|
320
466
|
public orderBy<R>(mapper: UnaryFunction<T, R>, order: Order = Order.Ascending): From<T> {
|
|
321
467
|
const array = Array.from(this._fn());
|
|
322
468
|
if (order == Order.Ascending) {
|
|
@@ -338,6 +484,13 @@ export class From<T> implements Iterable<T> {
|
|
|
338
484
|
return From.iterable(array);
|
|
339
485
|
}
|
|
340
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
|
+
*/
|
|
341
494
|
public groupBy<TKey>(keySelector: UnaryFunction<T, TKey>, equalityComparer?: EqualityComparer<TKey>): From<Grouping<TKey, T>> {
|
|
342
495
|
|
|
343
496
|
equalityComparer = (equalityComparer == null ? shallowEqual : equalityComparer);
|
|
@@ -363,6 +516,11 @@ export class From<T> implements Iterable<T> {
|
|
|
363
516
|
});
|
|
364
517
|
}
|
|
365
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
|
+
*/
|
|
366
524
|
public head(n: number): From<T> {
|
|
367
525
|
|
|
368
526
|
const self = this;
|
|
@@ -378,6 +536,11 @@ export class From<T> implements Iterable<T> {
|
|
|
378
536
|
});
|
|
379
537
|
}
|
|
380
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
|
+
*/
|
|
381
544
|
public tail(n: number): From<T> {
|
|
382
545
|
|
|
383
546
|
const self = this;
|
|
@@ -393,16 +556,31 @@ export class From<T> implements Iterable<T> {
|
|
|
393
556
|
});
|
|
394
557
|
}
|
|
395
558
|
|
|
559
|
+
/**
|
|
560
|
+
* Performs the specified action on each element of the sequence.
|
|
561
|
+
* @param {Consumer<T>} action - The action to perform.
|
|
562
|
+
*/
|
|
396
563
|
public forEach(action: Consumer<T>): void {
|
|
397
564
|
for (let item of this) {
|
|
398
565
|
action(item);
|
|
399
566
|
}
|
|
400
567
|
}
|
|
401
568
|
|
|
569
|
+
/**
|
|
570
|
+
* Converts the sequence to an array.
|
|
571
|
+
* @returns {T[]} The array.
|
|
572
|
+
*/
|
|
402
573
|
public toArray(): T[] {
|
|
403
574
|
return Array.from(this);
|
|
404
575
|
}
|
|
405
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
|
+
*/
|
|
406
584
|
public toRecord<R>(keySelector: UnaryFunction<T, string | symbol | number>, valueSelector: UnaryFunction<T, R>): Record<string | symbol | number, R> {
|
|
407
585
|
const output: Record<string | symbol | number, R> = {};
|
|
408
586
|
|
|
@@ -412,6 +590,13 @@ export class From<T> implements Iterable<T> {
|
|
|
412
590
|
return output;
|
|
413
591
|
}
|
|
414
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
|
+
*/
|
|
415
600
|
public toMap<RKey, RValue>(keySelector: UnaryFunction<T, RKey>, valueSelector: UnaryFunction<T, RValue>): Map<RKey, RValue> {
|
|
416
601
|
const map = new Map<any, any>();
|
|
417
602
|
|
|
@@ -425,10 +610,20 @@ export class From<T> implements Iterable<T> {
|
|
|
425
610
|
}
|
|
426
611
|
|
|
427
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
|
+
*/
|
|
428
618
|
public instancesOf(type: string): From<T> {
|
|
429
619
|
return this.filter(item => typeof item === type);
|
|
430
620
|
}
|
|
431
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
|
+
*/
|
|
432
627
|
public allInstanceOf(type: new (...args: any[]) => any): boolean {
|
|
433
628
|
for (let item of this) {
|
|
434
629
|
if (!(item instanceof type)) {
|
|
@@ -438,6 +633,11 @@ export class From<T> implements Iterable<T> {
|
|
|
438
633
|
return true;
|
|
439
634
|
}
|
|
440
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
|
+
*/
|
|
441
641
|
public distinct(eq_comparer?: EqualityComparer<T>): From<T> {
|
|
442
642
|
|
|
443
643
|
if (eq_comparer == null)
|
|
@@ -457,6 +657,12 @@ export class From<T> implements Iterable<T> {
|
|
|
457
657
|
});
|
|
458
658
|
}
|
|
459
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
|
+
*/
|
|
460
666
|
public insert(item: T, index: number): From<T> {
|
|
461
667
|
|
|
462
668
|
const self = this;
|
|
@@ -477,6 +683,11 @@ export class From<T> implements Iterable<T> {
|
|
|
477
683
|
});
|
|
478
684
|
}
|
|
479
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
|
+
*/
|
|
480
691
|
public skip(n: number): From<T> {
|
|
481
692
|
const self = this;
|
|
482
693
|
return From.fn<T>(function* () {
|
|
@@ -491,6 +702,11 @@ export class From<T> implements Iterable<T> {
|
|
|
491
702
|
}
|
|
492
703
|
|
|
493
704
|
|
|
705
|
+
/**
|
|
706
|
+
* Concatenates two sequences.
|
|
707
|
+
* @param {Iterable<T>} other - The other sequence.
|
|
708
|
+
* @returns {From<T>} A new From instance.
|
|
709
|
+
*/
|
|
494
710
|
public union(other: Iterable<T>): From<T> {
|
|
495
711
|
const self = this;
|
|
496
712
|
return From.fn<T>(function* () {
|
|
@@ -499,6 +715,15 @@ export class From<T> implements Iterable<T> {
|
|
|
499
715
|
});
|
|
500
716
|
}
|
|
501
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
|
+
*/
|
|
502
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> {
|
|
503
728
|
|
|
504
729
|
const self = this;
|
|
@@ -513,6 +738,15 @@ export class From<T> implements Iterable<T> {
|
|
|
513
738
|
});
|
|
514
739
|
}
|
|
515
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
|
+
*/
|
|
516
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> {
|
|
517
751
|
|
|
518
752
|
const self = this;
|
|
@@ -526,6 +760,15 @@ export class From<T> implements Iterable<T> {
|
|
|
526
760
|
}
|
|
527
761
|
|
|
528
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
|
+
*/
|
|
529
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> {
|
|
530
773
|
|
|
531
774
|
const self = this;
|
|
@@ -542,6 +785,15 @@ export class From<T> implements Iterable<T> {
|
|
|
542
785
|
});
|
|
543
786
|
}
|
|
544
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
|
+
*/
|
|
545
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> {
|
|
546
798
|
|
|
547
799
|
const self = this;
|
|
@@ -559,6 +811,15 @@ export class From<T> implements Iterable<T> {
|
|
|
559
811
|
}
|
|
560
812
|
|
|
561
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
|
+
*/
|
|
562
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> {
|
|
563
824
|
|
|
564
825
|
const self = this;
|
|
@@ -573,18 +834,27 @@ export class From<T> implements Iterable<T> {
|
|
|
573
834
|
|
|
574
835
|
}
|
|
575
836
|
|
|
837
|
+
/**
|
|
838
|
+
* Represents a group of elements that have a common key.
|
|
839
|
+
* @template TKey, T
|
|
840
|
+
*/
|
|
576
841
|
export class Grouping<TKey, T> extends From<T> {
|
|
577
842
|
|
|
578
843
|
private readonly _key: TKey;
|
|
579
844
|
|
|
845
|
+
/**
|
|
846
|
+
* Gets the key of the group.
|
|
847
|
+
*/
|
|
580
848
|
public get key(): TKey {
|
|
581
849
|
return this._key;
|
|
582
850
|
}
|
|
583
851
|
|
|
852
|
+
/**
|
|
853
|
+
* @param {TKey} key - The group key.
|
|
854
|
+
* @param {Array<T>} values - The values in the group.
|
|
855
|
+
*/
|
|
584
856
|
public constructor(key: TKey, values: Array<T>) {
|
|
585
857
|
super(() => values);
|
|
586
858
|
this._key = key;
|
|
587
859
|
}
|
|
588
|
-
|
|
589
|
-
|
|
590
860
|
}
|
package/src/collections/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export {default as DefaultWeakMap} from './default-weak-map.js'
|
|
2
|
-
export {default as DefaultMap} from './default-map.js'
|
|
3
|
-
export {default as CacheDictionary} from './cache-dictionary.js';
|
|
1
|
+
export {default as DefaultWeakMap} from './default-weak-map.js';
|
|
2
|
+
export {default as DefaultMap} from './default-map.js';
|
|
3
|
+
export {default as CacheDictionary} from './cache-dictionary.js';
|
|
4
|
+
export {default as from} from './from.js';
|
package/src/draft.ts
CHANGED
|
@@ -89,12 +89,16 @@ function deepDraft(value: any, parentLink?: ParentLink): any {
|
|
|
89
89
|
|
|
90
90
|
if (value instanceof Map) {
|
|
91
91
|
const copy = new Map();
|
|
92
|
-
|
|
92
|
+
for (const [k, v] of value) {
|
|
93
|
+
copy.set(deepDraft(k), deepDraft(v));
|
|
94
|
+
}
|
|
93
95
|
return copy;
|
|
94
96
|
}
|
|
95
97
|
if (value instanceof Set) {
|
|
96
98
|
const copy = new Set();
|
|
97
|
-
|
|
99
|
+
for (const v of value) {
|
|
100
|
+
copy.add(deepDraft(v));
|
|
101
|
+
}
|
|
98
102
|
return copy;
|
|
99
103
|
}
|
|
100
104
|
|
package/src/index.ts
CHANGED
|
@@ -23,7 +23,7 @@ export {default as AsyncPoolExecutor} from './async-pool-executor.js';
|
|
|
23
23
|
|
|
24
24
|
// ─── Patterns ────────────────────────────────────────
|
|
25
25
|
export {default as Lazy} from './lazy.js';
|
|
26
|
-
export {default as from
|
|
26
|
+
export {default as from} from './collections/from.js';
|
|
27
27
|
export {EventBus, Event} from './event-bus.js';
|
|
28
28
|
|
|
29
29
|
// ─── Draft ────────────────────────────────────────
|