@daiso-tech/core 0.8.0 → 0.9.0
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/collection/implementations/async-iterable-collection/_shared/async-update-iterable.js.map +1 -1
- package/dist/cjs/collection/implementations/async-iterable-collection/async-iterable-collection.js +19 -0
- package/dist/cjs/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
- package/dist/cjs/collection/implementations/iterable-collection/_shared/update-iterable.js.map +1 -1
- package/dist/cjs/collection/implementations/iterable-collection/iterable-collection.js +19 -0
- package/dist/cjs/collection/implementations/iterable-collection/iterable-collection.js.map +1 -1
- package/dist/cjs/collection/implementations/list-collection/list-collection.js +31 -2
- package/dist/cjs/collection/implementations/list-collection/list-collection.js.map +1 -1
- package/dist/cjs/event-bus/contracts/_shared.js +8 -1
- package/dist/cjs/event-bus/contracts/_shared.js.map +1 -1
- package/dist/cjs/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.js +2 -2
- package/dist/cjs/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
- package/dist/cjs/event-bus/implementations/event-bus/event-bus-manager.js +14 -7
- package/dist/cjs/event-bus/implementations/event-bus/event-bus-manager.js.map +1 -1
- package/dist/cjs/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.js +2 -2
- package/dist/cjs/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.js.map +1 -1
- package/dist/cjs/storage/implementations/storage/storage-manager.js +17 -7
- package/dist/cjs/storage/implementations/storage/storage-manager.js.map +1 -1
- package/dist/cjs/storage/implementations/storage/with-event-storage-adapter.js +8 -7
- package/dist/cjs/storage/implementations/storage/with-event-storage-adapter.js.map +1 -1
- package/dist/esm/collection/implementations/async-iterable-collection/_shared/async-update-iterable.js.map +1 -1
- package/dist/esm/collection/implementations/async-iterable-collection/async-iterable-collection.js +19 -0
- package/dist/esm/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
- package/dist/esm/collection/implementations/iterable-collection/_shared/update-iterable.js.map +1 -1
- package/dist/esm/collection/implementations/iterable-collection/iterable-collection.js +19 -0
- package/dist/esm/collection/implementations/iterable-collection/iterable-collection.js.map +1 -1
- package/dist/esm/collection/implementations/list-collection/list-collection.js +31 -2
- package/dist/esm/collection/implementations/list-collection/list-collection.js.map +1 -1
- package/dist/esm/event-bus/contracts/_shared.js +6 -0
- package/dist/esm/event-bus/contracts/_shared.js.map +1 -1
- package/dist/esm/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.js +1 -1
- package/dist/esm/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
- package/dist/esm/event-bus/implementations/event-bus/event-bus-manager.js +15 -8
- package/dist/esm/event-bus/implementations/event-bus/event-bus-manager.js.map +1 -1
- package/dist/esm/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.js +1 -1
- package/dist/esm/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.js.map +1 -1
- package/dist/esm/storage/implementations/storage/storage-manager.js +18 -8
- package/dist/esm/storage/implementations/storage/storage-manager.js.map +1 -1
- package/dist/esm/storage/implementations/storage/with-event-storage-adapter.js +8 -7
- package/dist/esm/storage/implementations/storage/with-event-storage-adapter.js.map +1 -1
- package/dist/types/collection/contracts/_shared.d.ts +0 -1
- package/dist/types/collection/contracts/async-collection.contract.d.ts +283 -37
- package/dist/types/collection/contracts/collection.contract.d.ts +277 -37
- package/dist/types/collection/implementations/async-iterable-collection/_shared/async-update-iterable.d.ts +3 -3
- package/dist/types/collection/implementations/async-iterable-collection/async-iterable-collection.d.ts +5 -2
- package/dist/types/collection/implementations/iterable-collection/_shared/update-iterable.d.ts +3 -3
- package/dist/types/collection/implementations/iterable-collection/iterable-collection.d.ts +5 -2
- package/dist/types/collection/implementations/list-collection/list-collection.d.ts +5 -2
- package/dist/types/event-bus/contracts/_shared.d.ts +6 -0
- package/dist/types/event-bus/contracts/event-bus-manager.contract.d.ts +107 -7
- package/dist/types/event-bus/contracts/event-bus.contract.d.ts +50 -22
- package/dist/types/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.d.ts +17 -1
- package/dist/types/event-bus/implementations/event-bus/event-bus-manager.d.ts +9 -6
- package/dist/types/serializer/contracts/serializer.contract.d.ts +0 -3
- package/dist/types/storage/contracts/storage-adapter.contract.d.ts +1 -18
- package/dist/types/storage/contracts/storage-events.contract.d.ts +1 -0
- package/dist/types/storage/contracts/storage-manager.contract.d.ts +87 -6
- package/dist/types/storage/contracts/storage.contract.d.ts +41 -33
- package/dist/types/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.d.ts +18 -2
- package/dist/types/storage/implementations/storage/storage-manager.d.ts +12 -6
- package/dist/types/storage/implementations/storage/with-event-storage-adapter.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Collection
|
|
3
3
|
*/
|
|
4
|
-
import type { Comparator, Predicate, ForEach, Map, Modifier, Tap, Transform,
|
|
4
|
+
import type { Comparator, Predicate, ForEach, Map, Modifier, Tap, Transform, Reduce, CrossJoinResult } from "../../collection/contracts/_shared";
|
|
5
5
|
import type { Lazyable, RecordItem } from "../../_shared/types";
|
|
6
6
|
export type Collapse<TValue> = TValue extends Array<infer TItem> | Iterable<infer TItem> | ICollection<infer TItem> ? TItem : TValue;
|
|
7
7
|
/**
|
|
8
8
|
* The <i>ICollection</i> contract offers a fluent and efficient approach to working with {@link Iterable} objects.
|
|
9
9
|
* <i>ICollection</i> is immutable.
|
|
10
|
-
* @throws {CollectionError}
|
|
11
|
-
* @throws {UnexpectedCollectionError}
|
|
12
|
-
* @throws {ItemNotFoundCollectionError}
|
|
13
|
-
* @throws {MultipleItemsFoundCollectionError}
|
|
14
|
-
* @throws {TypeCollectionError}
|
|
15
|
-
* @throws {EmptyCollectionError}
|
|
16
10
|
* @group Contracts
|
|
17
11
|
*/
|
|
18
12
|
export type ICollection<TInput> = Iterable<TInput> & {
|
|
@@ -35,47 +29,56 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
35
29
|
/**
|
|
36
30
|
* The <i>filter</i> method filters the collection using <i>predicateFn</i>, keeping only those items that pass <i>predicateFn</i>.
|
|
37
31
|
* @example
|
|
32
|
+
* ```ts
|
|
38
33
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
39
34
|
*
|
|
40
35
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6]);
|
|
41
36
|
* const filtered = collection.filter(item => 2 < item && item < 5);
|
|
42
37
|
* filtered.toArray();
|
|
43
38
|
* // [3, 4]
|
|
39
|
+
* ```
|
|
44
40
|
*/
|
|
45
41
|
filter<TOutput extends TInput>(predicateFn: Predicate<TInput, ICollection<TInput>, TOutput>): ICollection<TOutput>;
|
|
46
42
|
/**
|
|
47
43
|
* The <i>reject</i> method filters the collection using <i>predicateFn</i>, keeping only those items that not pass <i>predicateFn</i>.
|
|
48
44
|
* @example
|
|
45
|
+
* ```ts
|
|
49
46
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
50
47
|
*
|
|
51
48
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6]);
|
|
52
49
|
* const filtered = collection.reject(item => 2 < item && item < 5);
|
|
53
50
|
* filtered.toArray();
|
|
54
51
|
* // [1, 2, 5, 6]
|
|
52
|
+
* ```
|
|
55
53
|
*/
|
|
56
54
|
reject<TOutput extends TInput>(predicateFn: Predicate<TInput, ICollection<TInput>, TOutput>): ICollection<Exclude<TInput, TOutput>>;
|
|
57
55
|
/**
|
|
58
56
|
* The <i>map</i> method iterates through the collection and passes each item to <i>mapFn</i>.
|
|
59
57
|
* The <i>mapFn</i> is free to modify the item and return it, thus forming a new collection of modified items.
|
|
60
58
|
* @example
|
|
59
|
+
* ```ts
|
|
61
60
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
62
61
|
*
|
|
63
62
|
* const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
64
63
|
* const mapped = collection.map(item => item * 2);
|
|
65
64
|
* mapped.toArray();
|
|
66
65
|
* // [2, 4, 6, 8, 10]
|
|
66
|
+
* ```
|
|
67
67
|
*/
|
|
68
68
|
map<TOutput>(mapFn: Map<TInput, ICollection<TInput>, TOutput>): ICollection<TOutput>;
|
|
69
69
|
/**
|
|
70
70
|
* The <i>reduce</i> method executes <i> reduceFn </i> function on each item of the array, passing in the return value from the calculation on the preceding item.
|
|
71
71
|
* The final result of running the reducer across all items of the array is a single value.
|
|
72
72
|
* @example
|
|
73
|
+
* ```ts
|
|
73
74
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
74
75
|
*
|
|
75
76
|
* const collection = new ListCollection([1, 2, 3]);
|
|
76
77
|
* collection.reduce((sum, item) => sum + item);
|
|
77
78
|
* // 6
|
|
79
|
+
* ```
|
|
78
80
|
* @example
|
|
81
|
+
* ```ts
|
|
79
82
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
80
83
|
*
|
|
81
84
|
* const collection = new ListCollection(["a", "b", "c"]);
|
|
@@ -87,6 +90,7 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
87
90
|
* {} as Record<number, string>
|
|
88
91
|
* );
|
|
89
92
|
* // { 0: "a", 1: "b", 2: "c" }
|
|
93
|
+
* ```
|
|
90
94
|
*/
|
|
91
95
|
reduce(reduceFn: Reduce<TInput, ICollection<TInput>, TInput>): TInput;
|
|
92
96
|
reduce(reduceFn: Reduce<TInput, ICollection<TInput>, TInput>, initialValue: TInput): TInput;
|
|
@@ -95,73 +99,140 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
95
99
|
* The <i>join</i> method joins the collection's items with <i> separator </i>. An error will be thrown when if a none string item is encounterd.
|
|
96
100
|
* @throws {TypeCollectionError}
|
|
97
101
|
* @example
|
|
102
|
+
* ```ts
|
|
98
103
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
99
104
|
*
|
|
100
105
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
101
106
|
* collection.map(item => item.toString()).join();
|
|
102
107
|
* // "1,2,3,4"
|
|
103
108
|
* @example
|
|
109
|
+
* ```
|
|
110
|
+
* ```ts
|
|
104
111
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
105
112
|
*
|
|
106
113
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
107
114
|
* collection.map(item => item.toString()).join("_");
|
|
108
115
|
* // "1_2_3_4"
|
|
116
|
+
* ```
|
|
109
117
|
*/
|
|
110
118
|
join(separator?: string): Extract<TInput, string>;
|
|
111
119
|
/**
|
|
112
120
|
* The <i>collapse</i> method collapses a collection of iterables into a single, flat collection.
|
|
113
121
|
* @example
|
|
122
|
+
* ```ts
|
|
114
123
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
115
124
|
*
|
|
116
125
|
* const collection = new ListCollection([[1, 2], [3, 4]]);
|
|
117
126
|
* const collapsed = collection.collapse();
|
|
118
127
|
* collapsed.toArray();
|
|
119
128
|
* // [1, 2, 3, 4]
|
|
129
|
+
* ```
|
|
120
130
|
*/
|
|
121
131
|
collapse(): ICollection<Collapse<TInput>>;
|
|
122
132
|
/**
|
|
123
133
|
* The <i>flatMap</i> method returns a new array formed by applying <i>mapFn</i> to each item of the array, and then collapses the result by one level.
|
|
124
134
|
* It is identical to a <i>map</i> method followed by a <i>collapse</i> method.
|
|
125
135
|
* @example
|
|
136
|
+
* ```ts
|
|
126
137
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
127
138
|
*
|
|
128
139
|
* const collection = new ListCollection([["a", "b"], ["c", "d"]]).flatMap(item => [item.length, ...item]);
|
|
129
140
|
* collection.toArray();
|
|
130
141
|
* // [2, "a", "b", 2, "c", "d"]
|
|
142
|
+
* ```
|
|
131
143
|
*/
|
|
132
144
|
flatMap<TOutput>(mapFn: Map<TInput, ICollection<TInput>, Iterable<TOutput>>): ICollection<TOutput>;
|
|
133
145
|
/**
|
|
134
146
|
* The <i>change</i> method changes only the items that passes <i>predicateFn</i> using <i>mapFn</i>.
|
|
135
147
|
* @example
|
|
148
|
+
* ```ts
|
|
136
149
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
137
150
|
*
|
|
138
151
|
* const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
139
152
|
* const newCollection = collection.change(item => item % 2 === 0, item => item * 2);
|
|
140
153
|
* newCollection.toArray();
|
|
141
154
|
* // [1, 4, 3, 8, 5]
|
|
155
|
+
* ```
|
|
142
156
|
*/
|
|
143
|
-
change<TFilterOutput extends TInput, TMapOutput>(predicateFn: Predicate<TInput, ICollection<TInput>, TFilterOutput>, mapFn: Map<TFilterOutput, ICollection<TInput>, TMapOutput>): ICollection<
|
|
157
|
+
change<TFilterOutput extends TInput, TMapOutput>(predicateFn: Predicate<TInput, ICollection<TInput>, TFilterOutput>, mapFn: Map<TFilterOutput, ICollection<TInput>, TMapOutput>): ICollection<TInput | TFilterOutput | TMapOutput>;
|
|
158
|
+
/**
|
|
159
|
+
* The <i>set</i> method changes a item by i>index</i> using <i>value</i>.
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* import { ListCollection } from "@daiso-tech/core";;
|
|
163
|
+
*
|
|
164
|
+
* const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
165
|
+
* const newCollection = collection.set(1, -1);
|
|
166
|
+
* newCollection.toArray();
|
|
167
|
+
* // [1, -1, 3, 4, 5]
|
|
168
|
+
* ```
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* import { ListCollection } from "@daiso-tech/core";;
|
|
172
|
+
*
|
|
173
|
+
* const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
174
|
+
* const newCollection = collection.set(1, (prevValue) => prevValue - 2);
|
|
175
|
+
* newCollection.toArray();
|
|
176
|
+
* // [1, 0, 3, 4, 5]
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
set(index: number, value: TInput | Map<TInput, ICollection<TInput>, TInput>): ICollection<TInput>;
|
|
180
|
+
/**
|
|
181
|
+
* The <i>get</i> method returns the item by index. If the item is not found null will returned.
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* import { ListCollection } from "@daiso-tech/core";;
|
|
185
|
+
*
|
|
186
|
+
* const collection = new ListCollection([1, 4, 2, 8, -2]);
|
|
187
|
+
* // Will be 2
|
|
188
|
+
* collection.get(2);
|
|
189
|
+
*
|
|
190
|
+
* // Will be null
|
|
191
|
+
* collection.get(5);
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
get(index: number): TInput | null;
|
|
195
|
+
/**
|
|
196
|
+
* The <i>getOrFail</i> method returns the item by index. If the item is not found an error will be thrown.
|
|
197
|
+
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
198
|
+
* @throws {ItemNotFoundCollectionError} {@link ItemNotFoundCollectionError}
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* import { ListCollection } from "@daiso-tech/core";;
|
|
202
|
+
*
|
|
203
|
+
* const collection = new ListCollection([1, 4, 2, 8, -2]);
|
|
204
|
+
* // Will be 2
|
|
205
|
+
* collection.getOrFail(2);
|
|
206
|
+
*
|
|
207
|
+
* // An error will thrown
|
|
208
|
+
* collection.getOrFail(5);
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
getOrFail(index: number): TInput;
|
|
144
212
|
/**
|
|
145
213
|
* The <i>page</i> method returns a new collection containing the items that would be present on <i> page </i> with custom <i> pageSize </i>.
|
|
146
214
|
* @example
|
|
215
|
+
* ```ts
|
|
147
216
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
148
217
|
*
|
|
149
218
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
150
219
|
* const page = collection.page(2, 3);
|
|
151
220
|
* page.toArray();
|
|
152
221
|
* // [4, 5, 6]
|
|
222
|
+
* ```
|
|
153
223
|
*/
|
|
154
224
|
page(page: number, pageSize: number): ICollection<TInput>;
|
|
155
225
|
/**
|
|
156
226
|
* The <i>sum</i> method returns the sum of all items in the collection. If the collection includes other than number items an error will be thrown.
|
|
157
227
|
* If the collection is empty an error will also be thrown.
|
|
158
228
|
* @example
|
|
229
|
+
* ```ts
|
|
159
230
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
160
231
|
*
|
|
161
232
|
* const collection = new ListCollection([1, 2, 3]);
|
|
162
233
|
* collection.sum();
|
|
163
234
|
* // 6
|
|
164
|
-
*
|
|
235
|
+
* ```
|
|
165
236
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
166
237
|
* @throws {TypeCollectionError} {@link TypeCollectionError}
|
|
167
238
|
* @throws {EmptyCollectionError} {@link EmptyCollectionError}
|
|
@@ -171,12 +242,13 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
171
242
|
* The <i>average</i> method returns the average of all items in the collection. If the collection includes other than number items an error will be thrown.
|
|
172
243
|
* If the collection is empty an error will also be thrown.
|
|
173
244
|
* @example
|
|
245
|
+
* ```ts
|
|
174
246
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
175
247
|
*
|
|
176
248
|
* const collection = new ListCollection([1, 2, 3]);
|
|
177
249
|
* collection.average();
|
|
178
250
|
* // 2
|
|
179
|
-
*
|
|
251
|
+
* ```
|
|
180
252
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
181
253
|
* @throws {TypeCollectionError} {@link TypeCollectionError}
|
|
182
254
|
* @throws {EmptyCollectionError} {@link EmptyCollectionError}
|
|
@@ -186,12 +258,13 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
186
258
|
* The <i>median</i> method returns the median of all items in the collection. If the collection includes other than number items an error will be thrown.
|
|
187
259
|
* If the collection is empty an error will also be thrown.
|
|
188
260
|
* @example
|
|
261
|
+
* ```ts
|
|
189
262
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
190
263
|
*
|
|
191
264
|
* const collection = new ListCollection([1, 2, 3]);
|
|
192
265
|
* collection.median();
|
|
193
266
|
* // 2
|
|
194
|
-
*
|
|
267
|
+
* ```
|
|
195
268
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
196
269
|
* @throws {TypeCollectionError} {@link TypeCollectionError}
|
|
197
270
|
* @throws {EmptyCollectionError} {@link EmptyCollectionError}
|
|
@@ -201,12 +274,13 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
201
274
|
* The <i>min</i> method returns the min of all items in the collection. If the collection includes other than number items an error will be thrown.
|
|
202
275
|
* If the collection is empty an error will also be thrown.
|
|
203
276
|
* @example
|
|
277
|
+
* ```ts
|
|
204
278
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
205
279
|
*
|
|
206
280
|
* const collection = new ListCollection([1, 2, 3]);
|
|
207
281
|
* collection.min();
|
|
208
282
|
* // 1
|
|
209
|
-
*
|
|
283
|
+
* ```
|
|
210
284
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
211
285
|
* @throws {TypeCollectionError} {@link TypeCollectionError}
|
|
212
286
|
* @throws {EmptyCollectionError} {@link EmptyCollectionError}
|
|
@@ -216,12 +290,13 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
216
290
|
* The <i>max</i> method returns the max of all items in the collection. If the collection includes other than number items an error will be thrown.
|
|
217
291
|
* If the collection is empty an error will also be thrown.
|
|
218
292
|
* @example
|
|
293
|
+
* ```ts
|
|
219
294
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
220
295
|
*
|
|
221
296
|
* const collection = new ListCollection([1, 2, 3]);
|
|
222
297
|
* collection.max();
|
|
223
298
|
* // 3
|
|
224
|
-
*
|
|
299
|
+
* ```
|
|
225
300
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
226
301
|
* @throws {TypeCollectionError} {@link TypeCollectionError}
|
|
227
302
|
* @throws {EmptyCollectionError} {@link EmptyCollectionError}
|
|
@@ -231,12 +306,13 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
231
306
|
* The <i>percentage</i> method may be used to quickly determine the percentage of items in the collection that pass <i>predicateFn</i>.
|
|
232
307
|
* If the collection is empty an error will also be thrown.
|
|
233
308
|
* @example
|
|
309
|
+
* ```ts
|
|
234
310
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
235
311
|
*
|
|
236
312
|
* const collection = new ListCollection([1, 1, 2, 2, 2, 3]);
|
|
237
313
|
* collection.percentage(value => value === 1);
|
|
238
314
|
* // 33.333
|
|
239
|
-
*
|
|
315
|
+
* ```
|
|
240
316
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
241
317
|
* @throws {EmptyCollectionError} {@link EmptyCollectionError}
|
|
242
318
|
*/
|
|
@@ -244,100 +320,117 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
244
320
|
/**
|
|
245
321
|
* The <i>some</i> method determines whether at least one item in the collection matches <i>predicateFn</i>.
|
|
246
322
|
* @example
|
|
323
|
+
* ```ts
|
|
247
324
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
248
325
|
*
|
|
249
326
|
* const collection = new ListCollection([0, 1, 2, 3, 4, 5]);
|
|
250
327
|
* collection.some(item => item === 1);
|
|
251
328
|
* // true
|
|
252
|
-
*
|
|
329
|
+
* ```
|
|
253
330
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
254
331
|
*/
|
|
255
332
|
some<TOutput extends TInput>(predicateFn: Predicate<TInput, ICollection<TInput>, TOutput>): boolean;
|
|
256
333
|
/**
|
|
257
334
|
* The <i>every</i> method determines whether all items in the collection matches <i>predicateFn</i>.
|
|
258
335
|
* @example
|
|
336
|
+
* ```ts
|
|
259
337
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
260
338
|
*
|
|
261
339
|
* const collection = new ListCollection([0, 1, 2, 3, 4, 5]);
|
|
262
340
|
* collection.every(item => item < 6);
|
|
263
341
|
* // true
|
|
264
|
-
*
|
|
342
|
+
* ```
|
|
265
343
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
266
344
|
*/
|
|
267
345
|
every<TOutput extends TInput>(predicateFn: Predicate<TInput, ICollection<TInput>, TOutput>): boolean;
|
|
268
346
|
/**
|
|
269
347
|
* The <i>take</i> method takes the first <i>limit</i> items.
|
|
270
348
|
* @example
|
|
349
|
+
* ```ts
|
|
271
350
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
272
351
|
*
|
|
273
352
|
* const collection = new ListCollection([0, 1, 2, 3, 4, 5]);
|
|
274
353
|
* const chunk = collection.take(3);
|
|
275
354
|
* chunk.toArray();
|
|
276
355
|
* // [0, 1, 2]
|
|
356
|
+
* ```
|
|
277
357
|
* @example
|
|
358
|
+
* ```ts
|
|
278
359
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
279
360
|
*
|
|
280
361
|
* const collection = new ListCollection([0, 1, 2, 3, 4, 5]);
|
|
281
362
|
* const chunk = collection.take(-2);
|
|
282
363
|
* chunk.toArray();
|
|
283
364
|
* // [0, 1, 2, 3]
|
|
365
|
+
* ```
|
|
284
366
|
*/
|
|
285
367
|
take(limit: number): ICollection<TInput>;
|
|
286
368
|
/**
|
|
287
369
|
* The <i>takeUntil</i> method takes items until <i>predicateFn</i> returns true.
|
|
288
370
|
* @example
|
|
371
|
+
* ```ts
|
|
289
372
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
290
373
|
*
|
|
291
374
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
292
375
|
* const chunk = collection.takeUntil(item => item >= 3);
|
|
293
376
|
* chunk.toArray();
|
|
294
377
|
* // [1, 2]
|
|
378
|
+
* ```
|
|
295
379
|
*/
|
|
296
380
|
takeUntil(predicateFn: Predicate<TInput, ICollection<TInput>>): ICollection<TInput>;
|
|
297
381
|
/**
|
|
298
382
|
* The <i>takeWhile</i> method takes items until <i>predicateFn</i> returns false.
|
|
299
383
|
* @example
|
|
384
|
+
* ```ts
|
|
300
385
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
301
386
|
*
|
|
302
387
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
303
388
|
* const chunk = collection.takeWhile(item => item < 4);
|
|
304
389
|
* chunk.toArray();
|
|
305
390
|
* // [1, 2, 3]
|
|
391
|
+
* ```
|
|
306
392
|
*/
|
|
307
393
|
takeWhile(predicateFn: Predicate<TInput, ICollection<TInput>>): ICollection<TInput>;
|
|
308
394
|
/**
|
|
309
395
|
* The <i>skip</i> method skips the first <i>offset</i> items.
|
|
310
396
|
* @example
|
|
397
|
+
* ```ts
|
|
311
398
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
312
399
|
*
|
|
313
400
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).skip(4);
|
|
314
401
|
* collection.toArray();
|
|
315
402
|
* // [5, 6, 7, 8, 9, 10]
|
|
403
|
+
* ```
|
|
316
404
|
*/
|
|
317
405
|
skip(offset: number): ICollection<TInput>;
|
|
318
406
|
/**
|
|
319
407
|
* The <i>skipUntil</i> method skips items until <i>predicateFn</i> returns true.
|
|
320
408
|
* @example
|
|
409
|
+
* ```ts
|
|
321
410
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
322
411
|
*
|
|
323
412
|
* const collection = new ListCollection([1, 2, 3, 4]).skipUntil(item => item >= 3);
|
|
324
413
|
* collection.toArray();
|
|
325
414
|
* // [3, 4]
|
|
415
|
+
* ```
|
|
326
416
|
*/
|
|
327
417
|
skipUntil(predicateFn: Predicate<TInput, ICollection<TInput>>): ICollection<TInput>;
|
|
328
418
|
/**
|
|
329
419
|
* The <i>skipWhile</i> method skips items until <i>predicateFn</i> returns false.
|
|
330
420
|
* @example
|
|
421
|
+
* ```ts
|
|
331
422
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
332
423
|
*
|
|
333
424
|
* const collection = new ListCollection([1, 2, 3, 4]).skipWhile(item => item <= 3);
|
|
334
425
|
* collection.toArray();
|
|
335
426
|
* // [4]
|
|
427
|
+
* ```
|
|
336
428
|
*/
|
|
337
429
|
skipWhile(predicateFn: Predicate<TInput, ICollection<TInput>>): ICollection<TInput>;
|
|
338
430
|
/**
|
|
339
431
|
* The <i>when</i> method will execute <i>callback</i> when <i>condition</i> evaluates to true.
|
|
340
432
|
* @example
|
|
433
|
+
* ```ts
|
|
341
434
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
342
435
|
*
|
|
343
436
|
* const collection = new ListCollection([1, 2, 3, 4])
|
|
@@ -345,29 +438,35 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
345
438
|
* .when(false, collection => collection.append([20]));
|
|
346
439
|
* collection.toArray();
|
|
347
440
|
* // [1, 2, 3, 4, -3]
|
|
441
|
+
* ```
|
|
348
442
|
*/
|
|
349
443
|
when<TExtended = TInput>(condition: boolean, callback: Modifier<ICollection<TInput>, ICollection<TExtended>>): ICollection<TInput | TExtended>;
|
|
350
444
|
/**
|
|
351
445
|
* The <i>whenEmpty</i> method will execute <i>callback</i> when the collection is empty.
|
|
352
446
|
* @example
|
|
447
|
+
* ```ts
|
|
353
448
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
354
449
|
*
|
|
355
450
|
* const collection = new ListCollection([])
|
|
356
451
|
* .whenEmpty(collection => collection.append([-3]))
|
|
357
452
|
* collection.toArray();
|
|
358
453
|
* // [-3]
|
|
454
|
+
* ```
|
|
359
455
|
* @example
|
|
456
|
+
* ```ts
|
|
360
457
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
361
458
|
*
|
|
362
459
|
* const collection = new ListCollection([1])
|
|
363
460
|
* .whenEmpty(collection => collection.append([-3]))
|
|
364
461
|
* collection.toArray();
|
|
365
462
|
* // [1]
|
|
463
|
+
* ```
|
|
366
464
|
*/
|
|
367
465
|
whenEmpty<TExtended = TInput>(callback: Modifier<ICollection<TInput>, ICollection<TExtended>>): ICollection<TInput | TExtended>;
|
|
368
466
|
/**
|
|
369
467
|
* The <i>whenNot</i> method will execute <i>callback</i> when <i>condition</i> evaluates to false.
|
|
370
468
|
* @example
|
|
469
|
+
* ```ts
|
|
371
470
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
372
471
|
*
|
|
373
472
|
* const collection = new ListCollection([1, 2, 3, 4])
|
|
@@ -375,30 +474,36 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
375
474
|
* .whenNot(false, collection => collection.append([20]));
|
|
376
475
|
* collection.toArray();
|
|
377
476
|
* // [1, 2, 3, 4, 20]
|
|
477
|
+
* ```
|
|
378
478
|
*/
|
|
379
479
|
whenNot<TExtended = TInput>(condition: boolean, callback: Modifier<ICollection<TInput>, ICollection<TExtended>>): ICollection<TInput | TExtended>;
|
|
380
480
|
/**
|
|
381
481
|
* The <i>whenNotEmpty</i> method will execute <i>callback</i> when the collection is not empty.
|
|
382
482
|
* @example
|
|
483
|
+
* ```ts
|
|
383
484
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
384
485
|
*
|
|
385
486
|
* const collection = new ListCollection([])
|
|
386
487
|
* .whenNotEmpty(collection => collection.append([-3]))
|
|
387
488
|
* collection.toArray();
|
|
388
489
|
* // []
|
|
490
|
+
* ```
|
|
389
491
|
* @example
|
|
492
|
+
* ```ts
|
|
390
493
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
391
494
|
*
|
|
392
495
|
* const collection = new ListCollection([1])
|
|
393
496
|
* .whenNotEmpty(collection => collection.append([-3]))
|
|
394
497
|
* collection.toArray();
|
|
395
498
|
* // [1, -3]
|
|
499
|
+
* ```
|
|
396
500
|
*/
|
|
397
501
|
whenNotEmpty<TExtended = TInput>(callback: Modifier<ICollection<TInput>, ICollection<TExtended>>): ICollection<TInput | TExtended>;
|
|
398
502
|
/**
|
|
399
503
|
* The <i>pipe</i> method passes the orignal collection to <i>callback</i> and returns the result from <i>callback</i>.
|
|
400
504
|
* This method is useful when you want compose multiple smaller functions.
|
|
401
505
|
* @example
|
|
506
|
+
* ```ts
|
|
402
507
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
403
508
|
*
|
|
404
509
|
* const collection = new ListCollection([1, "2", "a", 1, 3, {}]);
|
|
@@ -415,11 +520,13 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
415
520
|
* const piped = collection.pipe(toNbrs).pipe(nbrToStr);
|
|
416
521
|
* console.log(piped);
|
|
417
522
|
* // [ 1, 2, 1, 3 ]
|
|
523
|
+
* ```
|
|
418
524
|
*/
|
|
419
525
|
pipe<TOutput = TInput>(callback: Transform<ICollection<TInput>, TOutput>): TOutput;
|
|
420
526
|
/**
|
|
421
527
|
* The <i>tap</i> method passes a copy of the original collection to <i>callback</i>, allowing you to do something with the items while not affecting the original collection.
|
|
422
528
|
* @example
|
|
529
|
+
* ```ts
|
|
423
530
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
424
531
|
*
|
|
425
532
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6])
|
|
@@ -430,24 +537,28 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
430
537
|
* })
|
|
431
538
|
* .toArray();
|
|
432
539
|
* // [1, 2, 3, 4, 5, 6]
|
|
540
|
+
* ```
|
|
433
541
|
*/
|
|
434
542
|
tap(callback: Tap<ICollection<TInput>>): ICollection<TInput>;
|
|
435
543
|
/**
|
|
436
544
|
* The <i>chunk</i> method breaks the collection into multiple, smaller collections of size <i>chunkSize</i>.
|
|
437
545
|
* If <i>chunkSize</i> is not divisible with total number of items then the last chunk will contain the remaining items.
|
|
438
546
|
* @example
|
|
547
|
+
* ```ts
|
|
439
548
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
440
549
|
*
|
|
441
550
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6, 7]);
|
|
442
551
|
* const chunks = collection.chunk(4);
|
|
443
552
|
* chunks.map(chunk => chunk.toArray()).toArray();
|
|
444
553
|
* // [[1, 2, 3, 4], [5, 6, 7]]
|
|
554
|
+
* ```
|
|
445
555
|
*/
|
|
446
556
|
chunk(chunkSize: number): ICollection<ICollection<TInput>>;
|
|
447
557
|
/**
|
|
448
558
|
* The <i>chunkWhile</i> method breaks the collection into multiple, smaller collections based on the evaluation of <i>predicateFn</i>.
|
|
449
559
|
* The chunk variable passed to the <i>predicateFn</i> may be used to inspect the previous item.
|
|
450
560
|
* @example
|
|
561
|
+
* ```ts
|
|
451
562
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
452
563
|
*
|
|
453
564
|
* const collection = new ListCollection("AABBCCCD");
|
|
@@ -456,59 +567,71 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
456
567
|
* });
|
|
457
568
|
* chunks.map(chunk => chunk.toArray()).toArray();
|
|
458
569
|
* // [["A", "A"], ["B", "B"], ["C", "C", "C"], ["D"]]
|
|
570
|
+
* ```
|
|
459
571
|
*/
|
|
460
572
|
chunkWhile(predicateFn: Predicate<TInput, ICollection<TInput>>): ICollection<ICollection<TInput>>;
|
|
461
573
|
/**
|
|
462
574
|
* The <i>split</i> method breaks a collection evenly into <i>chunkAmount</i> of chunks.
|
|
463
575
|
* @example
|
|
576
|
+
* ```ts
|
|
464
577
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
465
578
|
*
|
|
466
579
|
* const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
467
580
|
* const chunks = collection.split(3);
|
|
468
581
|
* chunks.map(chunk => chunk.toArray()).toArray();
|
|
469
582
|
* // [[1, 2], [3, 4], [5]]
|
|
583
|
+
* ```
|
|
470
584
|
* @example
|
|
585
|
+
* ```ts
|
|
471
586
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
472
587
|
*
|
|
473
588
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6]);
|
|
474
589
|
* const chunks = collection.split(3);
|
|
475
590
|
* chunks.map(chunk => chunk.toArray()).toArray();
|
|
476
591
|
* // [[1, 2], [3, 4], [5, 6]]
|
|
592
|
+
* ```
|
|
477
593
|
* @example
|
|
594
|
+
* ```ts
|
|
478
595
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
479
596
|
*
|
|
480
597
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6, 7]);
|
|
481
598
|
* const chunks = collection.split(3);
|
|
482
599
|
* chunks.map(chunk => chunk.toArray()).toArray();
|
|
483
600
|
* // [[1, 2, 7], [3, 4], [5, 6]]
|
|
601
|
+
* ```
|
|
484
602
|
*/
|
|
485
603
|
split(chunkAmount: number): ICollection<ICollection<TInput>>;
|
|
486
604
|
/**
|
|
487
605
|
* The <i>partition</i> method is used to separate items that pass <i>predicateFn</i> from those that do not.
|
|
488
606
|
* @example
|
|
607
|
+
* ```ts
|
|
489
608
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
490
609
|
*
|
|
491
610
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6]);
|
|
492
611
|
* const chunks = collection.partition(item => item < 3);
|
|
493
612
|
* chunks.map(chunk => chunk.toArray()).toArray();
|
|
494
613
|
* // [[1, 2], [3, 4, 5, 6]]
|
|
614
|
+
* ```
|
|
495
615
|
*/
|
|
496
616
|
partition(predicateFn: Predicate<TInput, ICollection<TInput>>): ICollection<ICollection<TInput>>;
|
|
497
617
|
/**
|
|
498
618
|
* The <i>sliding</i> method returns a new collection of chunks representing a "sliding window" view of the items in the collection.
|
|
499
619
|
* @example
|
|
620
|
+
* ```ts
|
|
500
621
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
501
622
|
*
|
|
502
623
|
* const collection = new ListCollection([1, 2, 3, 4, 5])
|
|
503
624
|
* const chunks = collection.sliding(2);
|
|
504
625
|
* chunks.map(chunk => chunk.toArray()).toArray();
|
|
505
626
|
* // [[1, 2], [2, 3], [3, 4], [4, 5]]
|
|
627
|
+
* ```
|
|
506
628
|
*/
|
|
507
629
|
sliding(chunkSize: number, step?: number): ICollection<ICollection<TInput>>;
|
|
508
630
|
/**
|
|
509
631
|
* The <i>groupBy</i> method groups the collection's items by <i> selectFn </i>.
|
|
510
632
|
* By default the equality check occurs on the item.
|
|
511
633
|
* @example
|
|
634
|
+
* ```ts
|
|
512
635
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
513
636
|
*
|
|
514
637
|
* const collection = new ListCollection(["a", "a", "a", "b", "b", "c"]);
|
|
@@ -530,7 +653,9 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
530
653
|
* // ["c"]
|
|
531
654
|
* // ]
|
|
532
655
|
* // ]
|
|
656
|
+
* ```
|
|
533
657
|
* @example
|
|
658
|
+
* ```ts
|
|
534
659
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
535
660
|
*
|
|
536
661
|
* const collection = new ListCollection(["alice@gmail.com", "bob@yahoo.com", "carlos@gmail.com"]);
|
|
@@ -548,12 +673,14 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
548
673
|
* // ["bob@yahoo.com"]
|
|
549
674
|
* // ]
|
|
550
675
|
* // ]
|
|
676
|
+
* ```
|
|
551
677
|
*/
|
|
552
678
|
groupBy<TOutput = TInput>(selectFn?: Map<TInput, ICollection<TInput>, TOutput>): ICollection<RecordItem<TOutput, ICollection<TInput>>>;
|
|
553
679
|
/**
|
|
554
680
|
* The <i>countBy</i> method counts the occurrences of values in the collection by <i> selectFn </i>.
|
|
555
681
|
* By default the equality check occurs on the item.
|
|
556
682
|
* @example
|
|
683
|
+
* ```ts
|
|
557
684
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
558
685
|
*
|
|
559
686
|
* const collection = new ListCollection(["a", "a", "a", "b", "b", "c"]);
|
|
@@ -566,7 +693,9 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
566
693
|
* // ["b", 2],
|
|
567
694
|
* // ["c", 1]
|
|
568
695
|
* // ]
|
|
696
|
+
* ```
|
|
569
697
|
* @example
|
|
698
|
+
* ```ts
|
|
570
699
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
571
700
|
*
|
|
572
701
|
* const collection = new ListCollection(["alice@gmail.com", "bob@yahoo.com", "carlos@gmail.com"]);
|
|
@@ -577,18 +706,22 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
577
706
|
* // ["gmail.com", 2],
|
|
578
707
|
* // ["yahoo.com", 1]
|
|
579
708
|
* // ]
|
|
709
|
+
* ```
|
|
580
710
|
*/
|
|
581
711
|
countBy<TOutput = TInput>(selectFn?: Map<TInput, ICollection<TInput>, TOutput>): ICollection<RecordItem<TOutput, number>>;
|
|
582
712
|
/**
|
|
583
713
|
* The <i>unique</i> method removes all duplicate values from the collection by <i> selectFn </i>.
|
|
584
714
|
* By default the equality check occurs on the item.
|
|
585
715
|
* @example
|
|
716
|
+
* ```ts
|
|
586
717
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
587
718
|
*
|
|
588
719
|
* const collection = new ListCollection([1, 1, 2, 2, 3, 4, 2]);
|
|
589
720
|
* collection.unique().toArray();
|
|
590
721
|
* // [1, 2, 3, 4]
|
|
722
|
+
* ```
|
|
591
723
|
* @example
|
|
724
|
+
* ```ts
|
|
592
725
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
593
726
|
*
|
|
594
727
|
* const collection = new ListCollection([
|
|
@@ -605,19 +738,23 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
605
738
|
* // { name: "iPhone 6", brand: "Apple", type: "phone" },
|
|
606
739
|
* // { name: "Galaxy S6", brand: "Samsung", type: "phone" },
|
|
607
740
|
* // ]
|
|
741
|
+
* ```
|
|
608
742
|
*/
|
|
609
743
|
unique<TOutput = TInput>(selectFn?: Map<TInput, ICollection<TInput>, TOutput>): ICollection<TInput>;
|
|
610
744
|
/**
|
|
611
745
|
* The <i>difference</i> method will return the values in the original collection that are not present in <i>iterable</i>.
|
|
612
746
|
* By default the equality check occurs on the item.
|
|
613
747
|
* @example
|
|
748
|
+
* ```ts
|
|
614
749
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
615
750
|
*
|
|
616
751
|
* const collection = new ListCollection([1, 2, 2, 3, 4, 5]);
|
|
617
752
|
* const difference = collection.difference([2, 4, 6, 8]);
|
|
618
753
|
* difference.toArray();
|
|
619
754
|
* // [1, 3, 5]
|
|
755
|
+
* ```
|
|
620
756
|
* @example
|
|
757
|
+
* ```ts
|
|
621
758
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
622
759
|
*
|
|
623
760
|
* const collection = new ListCollection([
|
|
@@ -639,23 +776,27 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
639
776
|
* // { name: "iPhone 5", brand: "Apple", type: "phone" },
|
|
640
777
|
* // { name: "Galaxy S6", brand: "Samsung", type: "phone" },
|
|
641
778
|
* // ]
|
|
779
|
+
* ```
|
|
642
780
|
*/
|
|
643
781
|
difference<TOutput = TInput>(iterable: Iterable<TInput>, selectFn?: Map<TInput, ICollection<TInput>, TOutput>): ICollection<TInput>;
|
|
644
782
|
/**
|
|
645
783
|
* The <i>repeat</i> method will repeat the original collection <i>amount</i> times.
|
|
646
784
|
* @example
|
|
785
|
+
* ```ts
|
|
647
786
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
648
787
|
*
|
|
649
788
|
* const collection = new ListCollection([1, 2, 3]);
|
|
650
789
|
* const newCollection = collection.repeat(3);
|
|
651
790
|
* newCollection.toArray();
|
|
652
791
|
* // [1, 2, 3, 1, 2, 3, 1, 2, 3]
|
|
792
|
+
* ```
|
|
653
793
|
*/
|
|
654
794
|
repeat(amount: number): ICollection<TInput>;
|
|
655
795
|
/**
|
|
656
796
|
* The <i>padStart</i> method pads this collection with <i>fillItems</i> until the resulting collection size reaches <i>maxLength</i>.
|
|
657
797
|
* The padding is applied from the start of this collection.
|
|
658
798
|
* @example
|
|
799
|
+
* ```ts
|
|
659
800
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
660
801
|
*
|
|
661
802
|
* new ListCollection("abc").padStart(10, "foo").join("");
|
|
@@ -669,12 +810,14 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
669
810
|
*
|
|
670
811
|
* new ListCollection("abc").padStart(1, "_").join("");
|
|
671
812
|
* // "abc"
|
|
813
|
+
* ```
|
|
672
814
|
*/
|
|
673
815
|
padStart<TExtended = TInput>(maxLength: number, fillItems: Iterable<TExtended>): ICollection<TInput | TExtended>;
|
|
674
816
|
/**
|
|
675
817
|
* The <i>padEnd</i> method pads this collection with <i>fillItems</i> until the resulting collection size reaches <i>maxLength</i>.
|
|
676
818
|
* The padding is applied from the end of this collection.
|
|
677
819
|
* @example
|
|
820
|
+
* ```ts
|
|
678
821
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
679
822
|
*
|
|
680
823
|
* new ListCollection("abc").padEnd(10, "foo").join("");
|
|
@@ -688,92 +831,114 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
688
831
|
*
|
|
689
832
|
* new ListCollection("abc").padEnd(1, "_").join("");
|
|
690
833
|
* // "abc"
|
|
834
|
+
* ```
|
|
691
835
|
*/
|
|
692
836
|
padEnd<TExtended = TInput>(maxLength: number, fillItems: Iterable<TExtended>): ICollection<TInput | TExtended>;
|
|
693
837
|
/**
|
|
694
838
|
* The <i>slice</i> method creates porition of the original collection selected from <i>start</i> and <i>end</i>
|
|
695
839
|
* where <i>start</i> and <i>end</i> (end not included) represent the index of items in the collection.
|
|
696
840
|
* @example
|
|
841
|
+
* ```ts
|
|
697
842
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
698
843
|
*
|
|
699
844
|
* const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
|
|
700
845
|
* collection.slice(3).toArray();
|
|
701
846
|
* // ["d", "e", "f"]
|
|
847
|
+
* ```
|
|
702
848
|
* @example
|
|
849
|
+
* ```ts
|
|
703
850
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
704
851
|
*
|
|
705
852
|
* const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
|
|
706
853
|
* collection.slice(undefined, 2).toArray();
|
|
707
854
|
* // ["a", "b"]
|
|
855
|
+
* ```
|
|
708
856
|
* @example
|
|
857
|
+
* ```ts
|
|
709
858
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
710
859
|
*
|
|
711
860
|
* const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
|
|
712
861
|
* collection.slice(2, 5).toArray();
|
|
713
862
|
* // ["c", "d", "e"]
|
|
863
|
+
* ```
|
|
714
864
|
* @example
|
|
865
|
+
* ```ts
|
|
715
866
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
716
867
|
*
|
|
717
868
|
* const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
|
|
718
869
|
* collection.slice(-2).toArray();
|
|
719
870
|
* // ["e", "f"]
|
|
871
|
+
* ```
|
|
720
872
|
* @example
|
|
873
|
+
* ```ts
|
|
721
874
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
722
875
|
*
|
|
723
876
|
* const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
|
|
724
877
|
* collection.slice(undefined, -2).toArray();
|
|
725
878
|
* // ["a", "b", "c", "d"]
|
|
879
|
+
* ```
|
|
726
880
|
* @example
|
|
881
|
+
* ```ts
|
|
727
882
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
728
883
|
*
|
|
729
884
|
* const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]);
|
|
730
885
|
* collection.slice(-4, -2).toArray();
|
|
731
886
|
* // ["c", "d"]
|
|
887
|
+
* ```
|
|
732
888
|
*/
|
|
733
889
|
slice(start?: number, end?: number): ICollection<TInput>;
|
|
734
890
|
/**
|
|
735
891
|
* The <i>prepend</i> method adds <i>iterable</i> to the beginning of the collection.
|
|
736
892
|
* @example
|
|
893
|
+
* ```ts
|
|
737
894
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
738
895
|
*
|
|
739
896
|
* const collection = new ListCollection([1, 2, 3, 4, 5]).prepend([-1, 20]);
|
|
740
897
|
* collection.toArray();
|
|
741
898
|
* // [-1, 20, 1, 2, 3, 4, 5]
|
|
899
|
+
* ```
|
|
742
900
|
*/
|
|
743
901
|
prepend<TExtended = TInput>(iterable: Iterable<TInput | TExtended>): ICollection<TInput | TExtended>;
|
|
744
902
|
/**
|
|
745
903
|
* The <i>append</i> method adds <i>iterable</i> to the end of the collection.
|
|
746
904
|
* @example
|
|
905
|
+
* ```ts
|
|
747
906
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
748
907
|
*
|
|
749
908
|
* const collection = new ListCollection([1, 2, 3, 4, 5]).append([-1, -2]);
|
|
750
909
|
* collection.toArray();
|
|
751
910
|
* // [1, 2, 3, 4, 5, -1, -2,]
|
|
911
|
+
* ```
|
|
752
912
|
*/
|
|
753
913
|
append<TExtended = TInput>(iterable: Iterable<TInput | TExtended>): ICollection<TInput | TExtended>;
|
|
754
914
|
/**
|
|
755
915
|
* The <i>insertBefore</i> method adds <i>iterable</i> before the first item that matches <i>predicateFn</i>.
|
|
756
916
|
* @example
|
|
917
|
+
* ```ts
|
|
757
918
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
758
919
|
*
|
|
759
920
|
* const collection = new ListCollection([1, 2, 2, 3, 4, 5]).insertBefore(item => item === 2, [-1, 20]);
|
|
760
921
|
* collection.toArray();
|
|
761
922
|
* // [1, -1, 20, 2, 2, 3, 4, 5]
|
|
923
|
+
* ```
|
|
762
924
|
*/
|
|
763
925
|
insertBefore<TExtended = TInput>(predicateFn: Predicate<TInput, ICollection<TInput>>, iterable: Iterable<TInput | TExtended>): ICollection<TInput | TExtended>;
|
|
764
926
|
/**
|
|
765
927
|
* The <i>insertAfter</i> method adds <i>iterable</i> after the first item that matches <i>predicateFn</i>.
|
|
766
928
|
* @example
|
|
929
|
+
* ```ts
|
|
767
930
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
768
931
|
*
|
|
769
932
|
* const collection = new ListCollection([1, 2, 2, 3, 4, 5]).insertAfter(item => item === 2, [-1, 20]);
|
|
770
933
|
* collection.toArray();
|
|
771
934
|
* // [1, 2, -1, 20, 2, 3, 4, 5]
|
|
935
|
+
* ```
|
|
772
936
|
*/
|
|
773
937
|
insertAfter<TExtended = TInput>(predicateFn: Predicate<TInput, ICollection<TInput>>, iterable: Iterable<TInput | TExtended>): ICollection<TInput | TExtended>;
|
|
774
938
|
/**
|
|
775
939
|
* The <i>crossJoin</i> method cross joins the collection's values among <i>iterables</i>, returning a Cartesian product with all possible permutations.
|
|
776
940
|
* @example
|
|
941
|
+
* ```ts
|
|
777
942
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
778
943
|
*
|
|
779
944
|
* const collection = new ListCollection([1, 2]);
|
|
@@ -785,7 +950,9 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
785
950
|
* // [2, "a"],
|
|
786
951
|
* // [2, "b"],
|
|
787
952
|
* // ]
|
|
953
|
+
* ```
|
|
788
954
|
* @example
|
|
955
|
+
* ```ts
|
|
789
956
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
790
957
|
*
|
|
791
958
|
* const collection = new ListCollection([1, 2]);
|
|
@@ -801,43 +968,53 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
801
968
|
* // [2, "b", "I"],
|
|
802
969
|
* // [2, "b", "II"],
|
|
803
970
|
* // ]
|
|
971
|
+
* ```
|
|
804
972
|
*/
|
|
805
973
|
crossJoin<TExtended>(iterable: Iterable<TExtended>): ICollection<CrossJoinResult<TInput, TExtended>>;
|
|
806
974
|
/**
|
|
807
975
|
* The <i>zip</i> method merges together the values of <i>iterable</i> with the values of the collection at their corresponding index.
|
|
808
976
|
* The returned collection has size of the shortest collection.
|
|
809
977
|
* @example
|
|
978
|
+
* ```ts
|
|
810
979
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
811
980
|
*
|
|
812
981
|
* const collection = new ListCollection(["Chair", "Desk"]);
|
|
813
982
|
* const zipped = collection.zip([100, 200]);
|
|
814
983
|
* zipped.toArray();
|
|
815
984
|
* // [["Chair", 100], ["Desk", 200]]
|
|
985
|
+
* ```
|
|
816
986
|
* @example
|
|
987
|
+
* ```ts
|
|
817
988
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
818
989
|
*
|
|
819
990
|
* const collection = new ListCollection(["Chair", "Desk", "Couch"]);
|
|
820
991
|
* const zipped = collection.zip([100, 200]);
|
|
821
992
|
* zipped.toArray();
|
|
822
993
|
* // [["Chair", 100], ["Desk", 200]]
|
|
994
|
+
* ```
|
|
823
995
|
* @example
|
|
996
|
+
* ```ts
|
|
824
997
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
825
998
|
*
|
|
826
999
|
* const collection = new ListCollection(["Chair", "Desk"]);
|
|
827
1000
|
* const zipped = collection.zip([100, 200, 300]);
|
|
828
1001
|
* zipped.toArray();
|
|
829
1002
|
* // [["Chair", 100], ["Desk", 200]]
|
|
1003
|
+
* ```
|
|
830
1004
|
*/
|
|
831
1005
|
zip<TExtended>(iterable: Iterable<TExtended>): ICollection<RecordItem<TInput, TExtended>>;
|
|
832
1006
|
/**
|
|
833
1007
|
* The <i>sort</i> method sorts the collection. You can provide a <i>comparator</i> function.
|
|
834
1008
|
* @example
|
|
1009
|
+
* ```ts
|
|
835
1010
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
836
1011
|
*
|
|
837
1012
|
* const collection = new ListCollection([-1, 2, 4, 3]);
|
|
838
1013
|
* collection.sort().toArray();
|
|
839
1014
|
* // [-1, 2, 3, 4]
|
|
1015
|
+
* ```
|
|
840
1016
|
* @example
|
|
1017
|
+
* ```ts
|
|
841
1018
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
842
1019
|
*
|
|
843
1020
|
* const collection = new ListCollection([
|
|
@@ -853,17 +1030,20 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
853
1030
|
* // { name: "Hasan", age: 25 },
|
|
854
1031
|
* // { name: "Anders", age: 30 },
|
|
855
1032
|
* // ]
|
|
1033
|
+
* ```
|
|
856
1034
|
*/
|
|
857
1035
|
sort(comparator?: Comparator<TInput>): ICollection<TInput>;
|
|
858
1036
|
/**
|
|
859
1037
|
* The <i>reverse</i> method will reverse the order of the collection.
|
|
860
1038
|
* The reversing of the collection will be applied in chunks that are the size of <i> chunkSize </i>.
|
|
861
1039
|
* @example
|
|
1040
|
+
* ```ts
|
|
862
1041
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
863
1042
|
*
|
|
864
1043
|
* const collection = new ListCollection([-1, 2, 4, 3]);
|
|
865
1044
|
* collection.reverse().toArray();
|
|
866
1045
|
* // [3, 4, 2, -1]
|
|
1046
|
+
* ```
|
|
867
1047
|
*/
|
|
868
1048
|
reverse(chunkSize?: number): ICollection<TInput>;
|
|
869
1049
|
/**
|
|
@@ -874,24 +1054,28 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
874
1054
|
* The <i>first</i> method returns the first item in the collection that passes <i> predicateFn </i>.
|
|
875
1055
|
* By default it will get the first item. If the collection is empty or no items passes <i> predicateFn </i> than null i returned.
|
|
876
1056
|
* @example
|
|
1057
|
+
* ```ts
|
|
877
1058
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
878
1059
|
*
|
|
879
1060
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
880
1061
|
* collection.first();
|
|
881
1062
|
* // 1
|
|
882
1063
|
* @example
|
|
1064
|
+
* ```ts
|
|
883
1065
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
884
1066
|
*
|
|
885
1067
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
886
1068
|
* collection.first(item => item > 2);
|
|
887
1069
|
* // 3
|
|
1070
|
+
* ```
|
|
888
1071
|
* @example
|
|
1072
|
+
* ```ts
|
|
889
1073
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
890
1074
|
*
|
|
891
1075
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
892
1076
|
* collection.first(item => item > 10);
|
|
893
1077
|
* // null
|
|
894
|
-
*
|
|
1078
|
+
* ```
|
|
895
1079
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
896
1080
|
* // 3
|
|
897
1081
|
*/
|
|
@@ -900,30 +1084,37 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
900
1084
|
* The <i>firstOr</i> method returns the first item in the collection that passes <i> predicateFn </i>
|
|
901
1085
|
* By default it will get the first item. If the collection is empty or no items passes <i> predicateFn </i> than <i> defaultValue </i>.
|
|
902
1086
|
* @example
|
|
1087
|
+
* ```ts
|
|
903
1088
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
904
1089
|
*
|
|
905
1090
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
906
1091
|
* collection.firstOr(-1);
|
|
907
1092
|
* // 1
|
|
1093
|
+
* ```
|
|
908
1094
|
* @example
|
|
1095
|
+
* ```ts
|
|
909
1096
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
910
1097
|
*
|
|
911
1098
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
912
1099
|
* collection.firstOr(-1, item => item > 2);
|
|
913
1100
|
* // 3
|
|
1101
|
+
* ```
|
|
914
1102
|
* @example
|
|
1103
|
+
* ```ts
|
|
915
1104
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
916
1105
|
*
|
|
917
1106
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
918
1107
|
* collection.firstOr(-1, item => item > 10);
|
|
919
1108
|
* // -1
|
|
1109
|
+
* ```
|
|
920
1110
|
* @example
|
|
1111
|
+
* ```ts
|
|
921
1112
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
922
1113
|
*
|
|
923
1114
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
924
1115
|
* collection.firstOr(() => -1, item => item > 10);
|
|
925
1116
|
* // -1
|
|
926
|
-
*
|
|
1117
|
+
* ```
|
|
927
1118
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
928
1119
|
*/
|
|
929
1120
|
firstOr<TOutput extends TInput, TExtended = TInput>(defaultValue: Lazyable<TExtended>, predicateFn?: Predicate<TInput, ICollection<TInput>, TOutput>): TOutput | TExtended;
|
|
@@ -931,24 +1122,29 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
931
1122
|
* The <i>firstOrFail</i> method returns the first item in the collection that passes <i> predicateFn </i>.
|
|
932
1123
|
* By default it will get the first item. If the collection is empty or no items passes <i> predicateFn </i> than error is thrown.
|
|
933
1124
|
* @example
|
|
1125
|
+
* ```ts
|
|
934
1126
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
935
1127
|
*
|
|
936
1128
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
937
1129
|
* collection.firstOrFail();
|
|
938
1130
|
* // 1
|
|
1131
|
+
* ```
|
|
939
1132
|
* @example
|
|
1133
|
+
* ```ts
|
|
940
1134
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
941
1135
|
*
|
|
942
1136
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
943
1137
|
* collection.firstOrFail(item => item > 2);
|
|
944
1138
|
* // 3
|
|
1139
|
+
* ```
|
|
945
1140
|
* @example
|
|
1141
|
+
* ```ts
|
|
946
1142
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
947
1143
|
*
|
|
948
1144
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
949
1145
|
* collection.firstOrFail(item => item > 10);
|
|
950
1146
|
* // throws an error
|
|
951
|
-
*
|
|
1147
|
+
* ```
|
|
952
1148
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
953
1149
|
* @throws {ItemNotFoundCollectionError} {@link ItemNotFoundCollectionError}
|
|
954
1150
|
*/
|
|
@@ -957,24 +1153,29 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
957
1153
|
* The <i>last</i> method returns the last item in the collection that passes <i> predicateFn </i>.
|
|
958
1154
|
* By default it will get the last item. If the collection is empty or no items passes <i> predicateFn </i> than null i returned.
|
|
959
1155
|
* @example
|
|
1156
|
+
* ```ts
|
|
960
1157
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
961
1158
|
*
|
|
962
1159
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
963
1160
|
* collection.last();
|
|
964
1161
|
* // 4
|
|
1162
|
+
* ```
|
|
965
1163
|
* @example
|
|
1164
|
+
* ```ts
|
|
966
1165
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
967
1166
|
*
|
|
968
1167
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
969
1168
|
* collection.last(item => item < 4);
|
|
970
1169
|
* // 3
|
|
1170
|
+
* ```
|
|
971
1171
|
* @example
|
|
1172
|
+
* ```ts
|
|
972
1173
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
973
1174
|
*
|
|
974
1175
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
975
1176
|
* collection.last(item => item > 10);
|
|
976
1177
|
* // null
|
|
977
|
-
*
|
|
1178
|
+
* ```
|
|
978
1179
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
979
1180
|
* // 3
|
|
980
1181
|
*/
|
|
@@ -983,30 +1184,37 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
983
1184
|
* The <i>lastOr</i> method returns the last item in the collection that passes <i> predicateFn </i>.
|
|
984
1185
|
* By default it will get the last item. If the collection is empty or no items passes <i> predicateFn </i> than <i> defaultValue </i>.
|
|
985
1186
|
* @example
|
|
1187
|
+
* ```ts
|
|
986
1188
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
987
1189
|
*
|
|
988
1190
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
989
1191
|
* collection.lastOr(-1);
|
|
990
1192
|
* // 4
|
|
1193
|
+
* ```
|
|
991
1194
|
* @example
|
|
1195
|
+
* ```ts
|
|
992
1196
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
993
1197
|
*
|
|
994
1198
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
995
1199
|
* collection.lastOr(-1, item => item < 4);
|
|
996
1200
|
* // 3
|
|
1201
|
+
* ```
|
|
997
1202
|
* @example
|
|
1203
|
+
* ```ts
|
|
998
1204
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
999
1205
|
*
|
|
1000
1206
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1001
1207
|
* collection.lastOr(-1, item => item > 10);
|
|
1002
1208
|
* // -1
|
|
1209
|
+
* ```
|
|
1003
1210
|
* @example
|
|
1211
|
+
* ```ts
|
|
1004
1212
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1005
1213
|
*
|
|
1006
1214
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1007
1215
|
* collection.lastOr(() => -1, item => item > 10);
|
|
1008
1216
|
* // -1
|
|
1009
|
-
*
|
|
1217
|
+
* ```
|
|
1010
1218
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1011
1219
|
*/
|
|
1012
1220
|
lastOr<TOutput extends TInput, TExtended = TInput>(defaultValue: Lazyable<TExtended>, predicateFn?: Predicate<TInput, ICollection<TInput>, TOutput>): TOutput | TExtended;
|
|
@@ -1014,24 +1222,29 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1014
1222
|
* The <i>lastOrFail</i> method returns the last item in the collection that passes <i> predicateFn </i>.
|
|
1015
1223
|
* By default it will get the last item. If the collection is empty or no items passes <i> predicateFn </i> than error is thrown.
|
|
1016
1224
|
* @example
|
|
1225
|
+
* ```ts
|
|
1017
1226
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1018
1227
|
*
|
|
1019
1228
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1020
1229
|
* collection.lastOrFail();
|
|
1021
1230
|
* // 4
|
|
1231
|
+
* ```
|
|
1022
1232
|
* @example
|
|
1233
|
+
* ```ts
|
|
1023
1234
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1024
1235
|
*
|
|
1025
1236
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1026
1237
|
* collection.lastOrFail(item => item < 4);
|
|
1027
1238
|
* // 3
|
|
1239
|
+
* ```
|
|
1028
1240
|
* @example
|
|
1241
|
+
* ```ts
|
|
1029
1242
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1030
1243
|
*
|
|
1031
1244
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1032
1245
|
* collection.lastOrFail(item => item > 10);
|
|
1033
1246
|
* // throws an error
|
|
1034
|
-
*
|
|
1247
|
+
* ```
|
|
1035
1248
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1036
1249
|
* @throws {ItemNotFoundCollectionError} {@link ItemNotFoundCollectionError}
|
|
1037
1250
|
*/
|
|
@@ -1040,18 +1253,21 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1040
1253
|
* The <i>before</i> method returns the item that comes before the first item that matches <i>predicateFn</i>.
|
|
1041
1254
|
* If the <i>predicateFn</i> does not match or matches the first item then null is returned.
|
|
1042
1255
|
* @example
|
|
1256
|
+
* ```ts
|
|
1043
1257
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1044
1258
|
*
|
|
1045
1259
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1046
1260
|
* collection.before(item => item === 2);
|
|
1047
1261
|
* // 1
|
|
1262
|
+
* ```
|
|
1048
1263
|
* @example
|
|
1264
|
+
* ```ts
|
|
1049
1265
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1050
1266
|
*
|
|
1051
1267
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1052
1268
|
* collection.before(item => item === 1);
|
|
1053
1269
|
* // null
|
|
1054
|
-
*
|
|
1270
|
+
* ```
|
|
1055
1271
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1056
1272
|
*/
|
|
1057
1273
|
before(predicateFn: Predicate<TInput, ICollection<TInput>>): TInput | null;
|
|
@@ -1059,24 +1275,29 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1059
1275
|
* The <i>beforeOr</i> method returns the item that comes before the first item that matches <i>predicateFn</i>.
|
|
1060
1276
|
* If the collection is empty or the <i>predicateFn</i> does not match or matches the first item then <i>defaultValue</i> is returned.
|
|
1061
1277
|
* @example
|
|
1278
|
+
* ```ts
|
|
1062
1279
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1063
1280
|
*
|
|
1064
1281
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1065
1282
|
* collection.beforeOr(-1, item => item === 2);
|
|
1066
1283
|
* // 1
|
|
1284
|
+
* ```
|
|
1067
1285
|
* @example
|
|
1286
|
+
* ```ts
|
|
1068
1287
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1069
1288
|
*
|
|
1070
1289
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1071
1290
|
* collection.beforeOr(-1, item => item === 1);
|
|
1072
1291
|
* // -1
|
|
1292
|
+
* ```
|
|
1073
1293
|
* @example
|
|
1294
|
+
* ```ts
|
|
1074
1295
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1075
1296
|
*
|
|
1076
1297
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1077
1298
|
* collection.beforeOr(() => -1, item => item === 1);
|
|
1078
1299
|
* // -1
|
|
1079
|
-
*
|
|
1300
|
+
* ```
|
|
1080
1301
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1081
1302
|
*/
|
|
1082
1303
|
beforeOr<TExtended = TInput>(defaultValue: Lazyable<TExtended>, predicateFn: Predicate<TInput, ICollection<TInput>>): TInput | TExtended;
|
|
@@ -1084,18 +1305,21 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1084
1305
|
* The <i>beforeOrFail</i> method returns the item that comes before the first item that matches <i>predicateFn</i>.
|
|
1085
1306
|
* If the collection is empty or the <i>predicateFn</i> does not match or matches the first item then an error is thrown.
|
|
1086
1307
|
* @example
|
|
1308
|
+
* ```ts
|
|
1087
1309
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1088
1310
|
*
|
|
1089
1311
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1090
1312
|
* collection.beforeOrFail(item => item === 2);
|
|
1091
1313
|
* // 1
|
|
1314
|
+
* ```
|
|
1092
1315
|
* @example
|
|
1316
|
+
* ```ts
|
|
1093
1317
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1094
1318
|
*
|
|
1095
1319
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1096
1320
|
* collection.beforeOrFail(item => item === 1);
|
|
1097
1321
|
* // error is thrown
|
|
1098
|
-
*
|
|
1322
|
+
* ```
|
|
1099
1323
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1100
1324
|
* @throws {ItemNotFoundCollectionError} {@link ItemNotFoundCollectionError}
|
|
1101
1325
|
*/
|
|
@@ -1104,18 +1328,21 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1104
1328
|
* The <i>after</i> method returns the item that comes after the first item that matches <i>predicateFn</i>.
|
|
1105
1329
|
* If the collection is empty or the <i>predicateFn</i> does not match or matches the last item then null is returned.
|
|
1106
1330
|
* @example
|
|
1331
|
+
* ```ts
|
|
1107
1332
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1108
1333
|
*
|
|
1109
1334
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1110
1335
|
* collection.after(item => item === 2);
|
|
1111
1336
|
* // 3
|
|
1337
|
+
* ```
|
|
1112
1338
|
* @example
|
|
1339
|
+
* ```ts
|
|
1113
1340
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1114
1341
|
*
|
|
1115
1342
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1116
1343
|
* collection.after(item => item === 4);
|
|
1117
1344
|
* // null
|
|
1118
|
-
*
|
|
1345
|
+
* ```
|
|
1119
1346
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1120
1347
|
*/
|
|
1121
1348
|
after(predicateFn: Predicate<TInput, ICollection<TInput>>): TInput | null;
|
|
@@ -1123,24 +1350,29 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1123
1350
|
* The <i>afterOr</i> method returns the item that comes after the first item that matches <i>predicateFn</i>.
|
|
1124
1351
|
* If the collection is empty or the <i>predicateFn</i> does not match or matches the last item then <i>defaultValue</i> is returned.
|
|
1125
1352
|
* @example
|
|
1353
|
+
* ```ts
|
|
1126
1354
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1127
1355
|
*
|
|
1128
1356
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1129
1357
|
* collection.afterOr(-1, item => item === 2);
|
|
1130
1358
|
* // 3
|
|
1359
|
+
* ```
|
|
1131
1360
|
* @example
|
|
1361
|
+
* ```ts
|
|
1132
1362
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1133
1363
|
*
|
|
1134
1364
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1135
1365
|
* collection.afterOr(-1, item => item === 4);
|
|
1136
1366
|
* // -1
|
|
1367
|
+
* ```
|
|
1137
1368
|
* @example
|
|
1369
|
+
* ```ts
|
|
1138
1370
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1139
1371
|
*
|
|
1140
1372
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1141
1373
|
* collection.afterOr(() => -1, item => item === 4);
|
|
1142
1374
|
* // -1
|
|
1143
|
-
*
|
|
1375
|
+
* ```
|
|
1144
1376
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1145
1377
|
*/
|
|
1146
1378
|
afterOr<TExtended = TInput>(defaultValue: Lazyable<TExtended>, predicateFn: Predicate<TInput, ICollection<TInput>>): TInput | TExtended;
|
|
@@ -1148,18 +1380,21 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1148
1380
|
* The <i>afterOrFail</i> method returns the item that comes after the first item that matches <i>predicateFn</i>.
|
|
1149
1381
|
* If the collection is empty or the <i>predicateFn</i> does not match or matches the last item then an error is thrown.
|
|
1150
1382
|
* @example
|
|
1383
|
+
* ```ts
|
|
1151
1384
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1152
1385
|
*
|
|
1153
1386
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1154
1387
|
* collection.afterOrFail(item => item === 2);
|
|
1155
1388
|
* // 3
|
|
1389
|
+
* ```
|
|
1156
1390
|
* @example
|
|
1391
|
+
* ```ts
|
|
1157
1392
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1158
1393
|
*
|
|
1159
1394
|
* const collection = new ListCollection([1, 2, 3, 4]);
|
|
1160
1395
|
* collection.afterOrFail(item => item === 4);
|
|
1161
1396
|
* // error is thrown
|
|
1162
|
-
*
|
|
1397
|
+
* ```
|
|
1163
1398
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1164
1399
|
* @throws {ItemNotFoundCollectionError} {@link ItemNotFoundCollectionError}
|
|
1165
1400
|
*/
|
|
@@ -1168,24 +1403,29 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1168
1403
|
* The <i>sole</i> method returns the first item in the collection that passes <i>predicateFn</i>, but only if <i>predicateFn</i> matches exactly one item.
|
|
1169
1404
|
* If no items matches or multiple items are found an error will be thrown.
|
|
1170
1405
|
* @example
|
|
1406
|
+
* ```ts
|
|
1171
1407
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1172
1408
|
*
|
|
1173
1409
|
* const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1174
1410
|
* collection.sole(item => item === 4);
|
|
1175
1411
|
* // 4
|
|
1412
|
+
* ```
|
|
1176
1413
|
* @example
|
|
1414
|
+
* ```ts
|
|
1177
1415
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1178
1416
|
*
|
|
1179
1417
|
* const collection = new ListCollection([1, 2, 3, 4, 4, 5]);
|
|
1180
1418
|
* collection.sole(item => item === 4);
|
|
1181
1419
|
* // error is thrown
|
|
1420
|
+
* ```
|
|
1182
1421
|
* @example
|
|
1422
|
+
* ```ts
|
|
1183
1423
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1184
1424
|
*
|
|
1185
1425
|
* const collection = new ListCollection([1, 2, 3, 5]);
|
|
1186
1426
|
* collection.sole(item => item === 4);
|
|
1187
1427
|
* // error is thrown
|
|
1188
|
-
*
|
|
1428
|
+
* ```
|
|
1189
1429
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1190
1430
|
* @throws {ItemNotFoundCollectionError} {@link ItemNotFoundCollectionError}
|
|
1191
1431
|
* @throws {MultipleItemsFoundCollectionError} {@link MultipleItemsFoundCollectionError}
|
|
@@ -1194,76 +1434,76 @@ export type ICollection<TInput> = Iterable<TInput> & {
|
|
|
1194
1434
|
/**
|
|
1195
1435
|
* The <i>nth</i> method creates a new collection consisting of every n-th item.
|
|
1196
1436
|
* @example
|
|
1437
|
+
* ```ts
|
|
1197
1438
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1198
1439
|
*
|
|
1199
1440
|
* const collection = new ListCollection(["a", "b", "c", "d", "e", "f"]).nth(4);
|
|
1200
1441
|
* collection.toArray();
|
|
1201
1442
|
* // ["a", "e"]
|
|
1443
|
+
* ```
|
|
1202
1444
|
*/
|
|
1203
1445
|
nth(step: number): ICollection<TInput>;
|
|
1204
1446
|
/**
|
|
1205
1447
|
* The <i>count</i> method returns the total number of items in the collection that passes <i>predicateFn</i>.
|
|
1206
1448
|
* @example
|
|
1449
|
+
* ```ts
|
|
1207
1450
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1208
1451
|
*
|
|
1209
1452
|
* const collection = new ListCollection([1, 2, 3, 4, 5, 6]);
|
|
1210
1453
|
* collection.count(value => value % 2 === 0);
|
|
1211
1454
|
* // 3
|
|
1212
|
-
*
|
|
1455
|
+
* ```
|
|
1213
1456
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1214
1457
|
*/
|
|
1215
1458
|
count(predicateFn: Predicate<TInput, ICollection<TInput>>): number;
|
|
1216
1459
|
/**
|
|
1217
1460
|
* The <i>size</i> returns the size of the collection.
|
|
1218
|
-
* @throws {CollectionError} {@link CollectionError}
|
|
1219
1461
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1220
1462
|
*/
|
|
1221
1463
|
size(): number;
|
|
1222
1464
|
/**
|
|
1223
1465
|
* The <i>isEmpty</i> returns true if the collection is empty.
|
|
1224
|
-
* @throws {CollectionError} {@link CollectionError}
|
|
1225
1466
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1226
1467
|
*/
|
|
1227
1468
|
isEmpty(): boolean;
|
|
1228
1469
|
/**
|
|
1229
1470
|
* The <i>isNotEmpty</i> returns true if the collection is not empty.
|
|
1230
|
-
* @throws {CollectionError} {@link CollectionError}
|
|
1231
1471
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1232
1472
|
*/
|
|
1233
1473
|
isNotEmpty(): boolean;
|
|
1234
1474
|
/**
|
|
1235
1475
|
* The <i>searchFirst</i> return the index of the first item that matches <i>predicateFn</i>.
|
|
1236
1476
|
* @example
|
|
1477
|
+
* ```ts
|
|
1237
1478
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1238
1479
|
*
|
|
1239
1480
|
* const collection = new ListCollection(["a", "b", "b", "c"]);
|
|
1240
1481
|
* collection.searchFirst(item => item === "b");
|
|
1241
1482
|
* // 1
|
|
1242
|
-
*
|
|
1483
|
+
* ```
|
|
1243
1484
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1244
1485
|
*/
|
|
1245
1486
|
searchFirst(predicateFn: Predicate<TInput, ICollection<TInput>>): number;
|
|
1246
1487
|
/**
|
|
1247
1488
|
* The <i>searchLast</i> return the index of the last item that matches <i>predicateFn</i>.
|
|
1248
1489
|
* @example
|
|
1490
|
+
* ```ts
|
|
1249
1491
|
* import { ListCollection } from "@daiso-tech/core";;
|
|
1250
1492
|
*
|
|
1251
1493
|
* const collection = new ListCollection(["a", "b", "b", "c"]);
|
|
1252
1494
|
* collection.searchLast(item => item === "b");
|
|
1253
1495
|
* // 2
|
|
1254
|
-
*
|
|
1496
|
+
* ```
|
|
1255
1497
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1256
1498
|
*/
|
|
1257
1499
|
searchLast(predicateFn: Predicate<TInput, ICollection<TInput>>): number;
|
|
1258
1500
|
/**
|
|
1259
1501
|
* The <i>forEach</i> method iterates through all items in the collection.
|
|
1260
|
-
* @throws {CollectionError} {@link CollectionError}
|
|
1261
1502
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1262
1503
|
*/
|
|
1263
1504
|
forEach(callback: ForEach<TInput, ICollection<TInput>>): void;
|
|
1264
1505
|
/**
|
|
1265
1506
|
* The <i>toArray</i> method converts the collection to a new array.
|
|
1266
|
-
* @throws {CollectionError} {@link CollectionError}
|
|
1267
1507
|
* @throws {UnexpectedCollectionError} {@link UnexpectedCollectionError}
|
|
1268
1508
|
*/
|
|
1269
1509
|
toArray(): TInput[];
|