@everyonesoftware/common 1.0.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/.c8rc.json +12 -0
- package/.github/workflows/publish.yml +38 -0
- package/.mocharc.json +9 -0
- package/README.md +9 -0
- package/package.json +36 -0
- package/sources/assertMessageParameters.ts +22 -0
- package/sources/asyncIterator.ts +437 -0
- package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +48 -0
- package/sources/asyncResult.ts +95 -0
- package/sources/basicDisposable.ts +57 -0
- package/sources/byteList.ts +202 -0
- package/sources/byteListStream.ts +121 -0
- package/sources/byteReadStream.ts +24 -0
- package/sources/byteWriteStream.ts +16 -0
- package/sources/bytes.ts +25 -0
- package/sources/characterList.ts +195 -0
- package/sources/characterListStream.ts +151 -0
- package/sources/characterReadStream.ts +81 -0
- package/sources/characterReadStreamIterator.ts +128 -0
- package/sources/characterWriteStream.ts +45 -0
- package/sources/commandLineParameter.ts +45 -0
- package/sources/commandLineParameters.ts +21 -0
- package/sources/comparable.ts +144 -0
- package/sources/comparer.ts +133 -0
- package/sources/comparison.ts +20 -0
- package/sources/concatenateIterable.ts +119 -0
- package/sources/concatenateIterator.ts +165 -0
- package/sources/condition.ts +329 -0
- package/sources/currentProcess.ts +158 -0
- package/sources/dateTime.ts +130 -0
- package/sources/depthFirstSearch.ts +230 -0
- package/sources/disposable.ts +31 -0
- package/sources/emptyError.ts +10 -0
- package/sources/english.ts +45 -0
- package/sources/equalFunctions.ts +123 -0
- package/sources/fetchHttpClient.ts +89 -0
- package/sources/fetchHttpResponse.ts +106 -0
- package/sources/flatMapIterable.ts +104 -0
- package/sources/flatMapIterator.ts +152 -0
- package/sources/generator.ts +251 -0
- package/sources/httpClient.ts +36 -0
- package/sources/httpHeader.ts +37 -0
- package/sources/httpHeaders.ts +216 -0
- package/sources/httpIncomingRequest.ts +30 -0
- package/sources/httpIncomingResponse.ts +19 -0
- package/sources/httpMethod.ts +164 -0
- package/sources/httpOutgoingRequest.ts +119 -0
- package/sources/httpOutgoingResponse.ts +113 -0
- package/sources/httpServer.ts +34 -0
- package/sources/inMemoryCharacterWriteStream.ts +78 -0
- package/sources/index.ts +101 -0
- package/sources/iterable.ts +345 -0
- package/sources/iterator.ts +481 -0
- package/sources/iteratorToJavascriptIteratorAdapter.ts +48 -0
- package/sources/javascript.ts +59 -0
- package/sources/javascriptArrayList.ts +175 -0
- package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +124 -0
- package/sources/javascriptIteratorToIteratorAdapter.ts +133 -0
- package/sources/javascriptMapMap.ts +143 -0
- package/sources/javascriptSetSet.ts +134 -0
- package/sources/list.ts +330 -0
- package/sources/listQueue.ts +62 -0
- package/sources/listStack.ts +62 -0
- package/sources/luxonDateTime.ts +109 -0
- package/sources/map.ts +302 -0
- package/sources/mapAsyncIterator.ts +141 -0
- package/sources/mapIterable.ts +105 -0
- package/sources/mapIterator.ts +145 -0
- package/sources/mutableCondition.ts +451 -0
- package/sources/mutableHttpHeaders.ts +204 -0
- package/sources/mutableMap.ts +292 -0
- package/sources/network.ts +18 -0
- package/sources/node.ts +37 -0
- package/sources/nodeJSCharacterWriteStream.ts +42 -0
- package/sources/nodeJSHttpIncomingRequest.ts +132 -0
- package/sources/nodeJSHttpServer.ts +134 -0
- package/sources/notFoundError.ts +12 -0
- package/sources/postCondition.ts +284 -0
- package/sources/postConditionError.ts +12 -0
- package/sources/preCondition.ts +284 -0
- package/sources/preConditionError.ts +12 -0
- package/sources/promiseAsyncResult.ts +174 -0
- package/sources/property.ts +63 -0
- package/sources/queue.ts +49 -0
- package/sources/realNetwork.ts +28 -0
- package/sources/recreationDotGovClient.ts +259 -0
- package/sources/searchControl.ts +42 -0
- package/sources/set.ts +244 -0
- package/sources/skipAsyncIterator.ts +145 -0
- package/sources/skipIterator.ts +155 -0
- package/sources/stack.ts +48 -0
- package/sources/stringComparer.ts +33 -0
- package/sources/stringIterator.ts +149 -0
- package/sources/strings.ts +322 -0
- package/sources/syncResult.ts +300 -0
- package/sources/takeAsyncIterator.ts +141 -0
- package/sources/takeIterator.ts +151 -0
- package/sources/toStringFunctions.ts +185 -0
- package/sources/types.ts +371 -0
- package/sources/whereAsyncIterator.ts +143 -0
- package/sources/whereIterable.ts +108 -0
- package/sources/whereIterator.ts +157 -0
- package/sources/wonderlandTrailClient.ts +1503 -0
- package/tests/assertTest.ts +113 -0
- package/tests/assertTestTests.ts +75 -0
- package/tests/basicTestSkip.ts +51 -0
- package/tests/byteListStreamTests.ts +390 -0
- package/tests/byteListTests.ts +27 -0
- package/tests/bytesTests.ts +43 -0
- package/tests/characterListStreamTests.ts +391 -0
- package/tests/characterListTests.ts +250 -0
- package/tests/characterWriteStreamTests.ts +12 -0
- package/tests/comparerTests.ts +92 -0
- package/tests/conditionTests.ts +877 -0
- package/tests/consoleTestRunner.ts +404 -0
- package/tests/consoleTestRunnerTests.ts +651 -0
- package/tests/dateTimeTests.ts +30 -0
- package/tests/depthFirstSearchTests.ts +106 -0
- package/tests/disposableTests.ts +121 -0
- package/tests/englishTests.ts +103 -0
- package/tests/equalFunctionsTests.ts +223 -0
- package/tests/failedTest.ts +43 -0
- package/tests/fetchHttpClientTests.ts +33 -0
- package/tests/generatorTests.ts +86 -0
- package/tests/httpClientTests.ts +18 -0
- package/tests/inMemoryCharacterWriteStreamTests.ts +117 -0
- package/tests/iterableTests.ts +141 -0
- package/tests/iteratorTests.ts +1086 -0
- package/tests/javascriptMapMapTests.ts +21 -0
- package/tests/listTests.ts +338 -0
- package/tests/mapIteratorTests.ts +55 -0
- package/tests/mapTests.ts +104 -0
- package/tests/mutableConditionTests.ts +273 -0
- package/tests/mutableMapTests.ts +154 -0
- package/tests/nodeJSHttpServerTests.ts +75 -0
- package/tests/notFoundErrorTests.ts +24 -0
- package/tests/postConditionErrorTests.ts +24 -0
- package/tests/preConditionErrorTests.ts +24 -0
- package/tests/promiseAsyncResultTests.ts +688 -0
- package/tests/propertyTests.ts +63 -0
- package/tests/queueTests.ts +29 -0
- package/tests/recreationDotGovClientTests.ts +191 -0
- package/tests/setTests.ts +140 -0
- package/tests/skippedTest.ts +39 -0
- package/tests/stackTests.ts +66 -0
- package/tests/stringComparerTests.ts +60 -0
- package/tests/stringIteratorTests.ts +156 -0
- package/tests/stringsTests.ts +516 -0
- package/tests/syncResultTests.ts +1251 -0
- package/tests/test.ts +228 -0
- package/tests/testAction.ts +75 -0
- package/tests/testActionTests.ts +93 -0
- package/tests/testFailureTests.ts +12 -0
- package/tests/testRunner.ts +267 -0
- package/tests/testRunnerTests.ts +895 -0
- package/tests/testSkip.ts +34 -0
- package/tests/tests.ts +103 -0
- package/tests/toStringFunctionsTests.ts +55 -0
- package/tests/typesTests.ts +257 -0
- package/tests/whereIteratorTests.ts +77 -0
- package/tests/wonderlandTrailClientTests.ts +452 -0
- package/tsconfig.json +17 -0
- package/tsup.config.ts +13 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import { Comparable } from "./comparable";
|
|
2
|
+
import { ConcatenateIterator } from "./concatenateIterator";
|
|
3
|
+
import { EmptyError } from "./emptyError";
|
|
4
|
+
import { FlatMapIterator } from "./flatMapIterator";
|
|
5
|
+
import { IteratorToJavascriptIteratorAdapter } from "./iteratorToJavascriptIteratorAdapter";
|
|
6
|
+
import { JavascriptIterable, JavascriptIterator } from "./javascript";
|
|
7
|
+
import { JavascriptIteratorToIteratorAdapter } from "./javascriptIteratorToIteratorAdapter";
|
|
8
|
+
import { MapIterator } from "./mapIterator";
|
|
9
|
+
import { NotFoundError } from "./notFoundError";
|
|
10
|
+
import { PreCondition } from "./preCondition";
|
|
11
|
+
import { SkipIterator } from "./skipIterator";
|
|
12
|
+
import { SyncResult } from "./syncResult";
|
|
13
|
+
import { TakeIterator } from "./takeIterator";
|
|
14
|
+
import { instanceOfType, isJavascriptIterator, isUndefinedOrNull, Type } from "./types";
|
|
15
|
+
import { WhereIterator } from "./whereIterator";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A type that can be used to iterate over a collection.
|
|
19
|
+
*/
|
|
20
|
+
export abstract class Iterator<T> implements JavascriptIterable<T>
|
|
21
|
+
{
|
|
22
|
+
/**
|
|
23
|
+
* Create a new {@link Iterator} that contains the provided values.
|
|
24
|
+
* @param values The values that the new {@link Iterator} will iterate over.
|
|
25
|
+
*/
|
|
26
|
+
public static create<T>(values: JavascriptIterator<T> | JavascriptIterable<T>): Iterator<T>
|
|
27
|
+
{
|
|
28
|
+
PreCondition.assertNotUndefinedAndNotNull(values, "values");
|
|
29
|
+
|
|
30
|
+
return JavascriptIteratorToIteratorAdapter.create(values);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Move to the next value in the collection. Return whether this {@link Iterator} points to a
|
|
35
|
+
* value after the move.
|
|
36
|
+
*/
|
|
37
|
+
public abstract next(): SyncResult<boolean>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Get whether this {@link Iterator} has started iterating over the values in the collection.
|
|
41
|
+
*/
|
|
42
|
+
public abstract hasStarted(): boolean;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get whether this {@link Iterator} currently points at a value in the collection.
|
|
46
|
+
*/
|
|
47
|
+
public abstract hasCurrent(): boolean;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get the value that this {@link Iterator} points to.
|
|
51
|
+
*/
|
|
52
|
+
public abstract getCurrent(): T;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Move to the first value if this {@link Iterator} hasn't started yet.
|
|
56
|
+
* @returns This object for method chaining.
|
|
57
|
+
*/
|
|
58
|
+
public start(): SyncResult<this>
|
|
59
|
+
{
|
|
60
|
+
return Iterator.start(this);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Move the provided {@link Iterator} to its first value if it hasn't started yet.
|
|
65
|
+
*/
|
|
66
|
+
public static start<T, TIterator extends Iterator<T>>(iterator: TIterator): SyncResult<TIterator>
|
|
67
|
+
{
|
|
68
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
69
|
+
|
|
70
|
+
return SyncResult.create(() =>
|
|
71
|
+
{
|
|
72
|
+
if (!iterator.hasStarted())
|
|
73
|
+
{
|
|
74
|
+
iterator.next().await();
|
|
75
|
+
}
|
|
76
|
+
return iterator;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the current value from this {@link Iterator} and advance this {@link Iterator} to the
|
|
82
|
+
* next value.
|
|
83
|
+
*/
|
|
84
|
+
public takeCurrent(): SyncResult<T>
|
|
85
|
+
{
|
|
86
|
+
return Iterator.takeCurrent(this);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public static takeCurrent<T>(iterator: Iterator<T>): SyncResult<T>
|
|
90
|
+
{
|
|
91
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
92
|
+
PreCondition.assertTrue(iterator.hasCurrent(), "iterator.hasCurrent()");
|
|
93
|
+
|
|
94
|
+
return SyncResult.create(() =>
|
|
95
|
+
{
|
|
96
|
+
const result: T = iterator.getCurrent();
|
|
97
|
+
iterator.next().await();
|
|
98
|
+
return result;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public [Symbol.iterator](): JavascriptIterator<T>
|
|
103
|
+
{
|
|
104
|
+
return Iterator[Symbol.iterator](this);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Convert the provided {@link Iterator} to a {@link IteratorToJavascriptIteratorAdapter}.
|
|
109
|
+
* @param iterator The {@link Iterator} to convert.
|
|
110
|
+
*/
|
|
111
|
+
public static [Symbol.iterator]<T>(iterator: Iterator<T>): JavascriptIterator<T>
|
|
112
|
+
{
|
|
113
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
114
|
+
|
|
115
|
+
return IteratorToJavascriptIteratorAdapter.create(iterator);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Get whether this {@link Iterator} contains any values.
|
|
120
|
+
* Note: This may advance the {@link Iterator} to the first value if it hasn't been
|
|
121
|
+
* started yet.
|
|
122
|
+
*/
|
|
123
|
+
public any(): SyncResult<boolean>
|
|
124
|
+
{
|
|
125
|
+
return Iterator.any(this);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Get whether this {@link Iterator} contains any values.
|
|
130
|
+
* Note: This may advance the {@link Iterator} to the first value if it hasn't been
|
|
131
|
+
* started yet.
|
|
132
|
+
*/
|
|
133
|
+
public static any<T>(iterator: Iterator<T>): SyncResult<boolean>
|
|
134
|
+
{
|
|
135
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
136
|
+
|
|
137
|
+
return SyncResult.create(() =>
|
|
138
|
+
{
|
|
139
|
+
iterator.start().await();
|
|
140
|
+
return iterator.hasCurrent();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Get the number of values in this {@link Iterator}.
|
|
147
|
+
* Note: This will consume all of the values in this {@link Iterator}.
|
|
148
|
+
*/
|
|
149
|
+
public getCount(): SyncResult<number>
|
|
150
|
+
{
|
|
151
|
+
return Iterator.getCount(this);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Get the number of values in the provided {@link Iterator}.
|
|
156
|
+
* Note: This will consume all of the values in the provided {@link Iterator}.
|
|
157
|
+
*/
|
|
158
|
+
public static getCount<T>(iterator: Iterator<T>): SyncResult<number>
|
|
159
|
+
{
|
|
160
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
161
|
+
|
|
162
|
+
return SyncResult.create(() =>
|
|
163
|
+
{
|
|
164
|
+
let result: number = 0;
|
|
165
|
+
if (iterator.hasCurrent())
|
|
166
|
+
{
|
|
167
|
+
result++;
|
|
168
|
+
}
|
|
169
|
+
while (iterator.next().await())
|
|
170
|
+
{
|
|
171
|
+
result++;
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Get all of the remaining values in this {@link Iterator} in a {@link T} {@link Array}.
|
|
179
|
+
*/
|
|
180
|
+
public toArray(): SyncResult<T[]>
|
|
181
|
+
{
|
|
182
|
+
return Iterator.toArray(this);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Get all of the remaining values in the provided {@link Iterator} in a {@link T}
|
|
187
|
+
* {@link Array}.
|
|
188
|
+
*/
|
|
189
|
+
public static toArray<T>(iterator: Iterator<T>): SyncResult<T[]>
|
|
190
|
+
{
|
|
191
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
192
|
+
|
|
193
|
+
return SyncResult.create(() =>
|
|
194
|
+
{
|
|
195
|
+
const result: T[] = [];
|
|
196
|
+
|
|
197
|
+
if (iterator.hasCurrent())
|
|
198
|
+
{
|
|
199
|
+
result.push(iterator.getCurrent());
|
|
200
|
+
}
|
|
201
|
+
while (iterator.next().await())
|
|
202
|
+
{
|
|
203
|
+
result.push(iterator.getCurrent());
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return result;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
public concatenate(...toConcatenate: JavascriptIterable<T>[]): Iterator<T>
|
|
211
|
+
{
|
|
212
|
+
return Iterator.concatenate(this, ...toConcatenate);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
public static concatenate<T>(iterator: Iterator<T>, ...toConcatenate: JavascriptIterable<T>[]): Iterator<T>
|
|
216
|
+
{
|
|
217
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
218
|
+
PreCondition.assertNotUndefinedAndNotNull(toConcatenate, "toConcatenate");
|
|
219
|
+
|
|
220
|
+
return ConcatenateIterator.create(iterator, ...toConcatenate);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Get an {@link Iterator} that will only return values that match the provided condition.
|
|
225
|
+
* @param condition The condition to run against each of the values in this {@link Iterator}.
|
|
226
|
+
*/
|
|
227
|
+
public where(condition: (value: T) => (boolean | SyncResult<boolean>)): Iterator<T>
|
|
228
|
+
{
|
|
229
|
+
return Iterator.where(this, condition);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
public static where<T>(iterator: Iterator<T>, condition: (value: T) => (boolean | SyncResult<boolean>)): Iterator<T>
|
|
233
|
+
{
|
|
234
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
235
|
+
PreCondition.assertNotUndefinedAndNotNull(condition, "condition");
|
|
236
|
+
|
|
237
|
+
return WhereIterator.create(iterator, condition);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Get a {@link MapIterator} that will map all {@link T} values from this {@link Iterator} to
|
|
242
|
+
* {@link TOutput} values.
|
|
243
|
+
* @param mapping The mapping that maps {@link T} values to {@link TOutput} values.
|
|
244
|
+
*/
|
|
245
|
+
public map<TOutput>(mapping: (value: T) => (TOutput | SyncResult<TOutput>)): Iterator<TOutput>
|
|
246
|
+
{
|
|
247
|
+
return Iterator.map(this, mapping);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public static map<T, TOutput>(iterator: Iterator<T>, mapping: (value: T) => (TOutput | SyncResult<TOutput>)): Iterator<TOutput>
|
|
251
|
+
{
|
|
252
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
253
|
+
PreCondition.assertNotUndefinedAndNotNull(mapping, "mapping");
|
|
254
|
+
|
|
255
|
+
return MapIterator.create(iterator, mapping);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
public flatMap<TOutput>(mapping: (value: T) => JavascriptIterable<TOutput>): Iterator<TOutput>
|
|
259
|
+
{
|
|
260
|
+
return Iterator.flatMap(this, mapping);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
public static flatMap<T,TOutput>(iterator: Iterator<T>, mapping: (value: T) => JavascriptIterable<TOutput>): Iterator<TOutput>
|
|
264
|
+
{
|
|
265
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
266
|
+
PreCondition.assertNotUndefinedAndNotNull(mapping, "mapping");
|
|
267
|
+
|
|
268
|
+
return FlatMapIterator.create(iterator, mapping);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Get an {@link Iterator} that will filter this {@link Iterator} to only the values that are
|
|
273
|
+
* instances of {@link U} based on the provided type check {@link Function}.
|
|
274
|
+
* @param typeCheck The {@link Function} that will be used to determine if values are of type
|
|
275
|
+
* {@link U}.
|
|
276
|
+
*/
|
|
277
|
+
public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): Iterator<U>
|
|
278
|
+
{
|
|
279
|
+
return Iterator.whereInstanceOf(this, typeCheck);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
public static whereInstanceOf<T, U extends T>(iterator: Iterator<T>, typeCheck: (value: T) => value is U): Iterator<U>
|
|
283
|
+
{
|
|
284
|
+
PreCondition.assertNotUndefinedAndNotNull(typeCheck, "typeCheck");
|
|
285
|
+
|
|
286
|
+
return iterator.where(typeCheck)
|
|
287
|
+
.map((value: T) => value as U);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Get an {@link Iterator} that will filter this {@link Iterator} to only the values that are
|
|
292
|
+
* instances of the provided {@link Type}.
|
|
293
|
+
* @param type The type of values to return from the new {@link Iterator}.
|
|
294
|
+
*/
|
|
295
|
+
public whereInstanceOfType<U extends T>(type: Type<U>): Iterator<U>
|
|
296
|
+
{
|
|
297
|
+
return Iterator.whereInstanceOfType(this, type);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
public static whereInstanceOfType<T, U extends T>(iterator: Iterator<T>, type: Type<U>): Iterator<U>
|
|
301
|
+
{
|
|
302
|
+
PreCondition.assertNotUndefinedAndNotNull(type, "type");
|
|
303
|
+
|
|
304
|
+
return iterator.whereInstanceOf((value: T) => instanceOfType(value, type));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* If the condition function is undefined, then this function will return the first value in
|
|
309
|
+
* this {@link Iterator}. If this condition function is provided, then this function will return
|
|
310
|
+
* the first value that matches the provided condition.
|
|
311
|
+
* @param condition The condition that the returned value must satisfy.
|
|
312
|
+
*/
|
|
313
|
+
public first(condition?: (value: T) => (boolean | SyncResult<boolean>)): SyncResult<T>
|
|
314
|
+
{
|
|
315
|
+
return Iterator.first(this, condition);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* If the condition function is undefined, then this function will return the first value in
|
|
320
|
+
* the {@link Iterator}. If this condition function is provided, then this function will return
|
|
321
|
+
* the first value that matches the provided condition.
|
|
322
|
+
* @param iterator The {@link Iterator} to get the first value from.
|
|
323
|
+
*/
|
|
324
|
+
public static first<T>(iterator: Iterator<T>, condition?: (value: T) => (boolean | SyncResult<boolean>)): SyncResult<T>
|
|
325
|
+
{
|
|
326
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
327
|
+
|
|
328
|
+
return SyncResult.create(() =>
|
|
329
|
+
{
|
|
330
|
+
iterator.start().await();
|
|
331
|
+
if (isUndefinedOrNull(condition))
|
|
332
|
+
{
|
|
333
|
+
if (!iterator.hasCurrent())
|
|
334
|
+
{
|
|
335
|
+
throw new EmptyError();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
else
|
|
339
|
+
{
|
|
340
|
+
while (iterator.hasCurrent() && !condition(iterator.getCurrent()))
|
|
341
|
+
{
|
|
342
|
+
const conditionResult: boolean | SyncResult<boolean> = condition(iterator.getCurrent());
|
|
343
|
+
const conditionBoolean: boolean = conditionResult instanceof SyncResult ? conditionResult.await() : conditionResult;
|
|
344
|
+
if (conditionBoolean)
|
|
345
|
+
{
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
else
|
|
349
|
+
{
|
|
350
|
+
iterator.next().await();
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (!iterator.hasCurrent())
|
|
355
|
+
{
|
|
356
|
+
throw new NotFoundError("No value was found in the Iterator that matched the provided condition.");
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return iterator.getCurrent();
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* If the condition function is undefined, then this function will return the last value in this
|
|
365
|
+
* {@link Iterator}. If this condition function is provided, then this function will return the
|
|
366
|
+
* last value that matches the provided condition.
|
|
367
|
+
* @param condition The condition that the returned value must satisfy.
|
|
368
|
+
*/
|
|
369
|
+
public last(condition?: (value: T) => (boolean | SyncResult<boolean>)): SyncResult<T>
|
|
370
|
+
{
|
|
371
|
+
return Iterator.last(this, condition);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* If the condition function is undefined, then this function will return the last value in the
|
|
376
|
+
* {@link Iterator}. If this condition function is provided, then this function will return the
|
|
377
|
+
* last value that matches the provided condition.
|
|
378
|
+
* @param iterator The {@link Iterator} to get the last value from.
|
|
379
|
+
*/
|
|
380
|
+
public static last<T>(iterator: Iterator<T>, condition?: (value: T) => (boolean | SyncResult<boolean>)): SyncResult<T>
|
|
381
|
+
{
|
|
382
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
383
|
+
|
|
384
|
+
return SyncResult.create(() =>
|
|
385
|
+
{
|
|
386
|
+
iterator.start().await();
|
|
387
|
+
|
|
388
|
+
if (!iterator.hasCurrent())
|
|
389
|
+
{
|
|
390
|
+
throw new EmptyError();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
let result: T;
|
|
394
|
+
let found: boolean = false;
|
|
395
|
+
do
|
|
396
|
+
{
|
|
397
|
+
if (!condition || condition(iterator.getCurrent()))
|
|
398
|
+
{
|
|
399
|
+
result = iterator.getCurrent();
|
|
400
|
+
found = true;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
while (iterator.next().await());
|
|
404
|
+
|
|
405
|
+
if (!found)
|
|
406
|
+
{
|
|
407
|
+
if (!condition)
|
|
408
|
+
{
|
|
409
|
+
throw new EmptyError();
|
|
410
|
+
}
|
|
411
|
+
else
|
|
412
|
+
{
|
|
413
|
+
throw new NotFoundError("No value was found in the Iterator that matched the provided condition.");
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return result!;
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Find the maximum value in the provided {@link Iterator}.
|
|
423
|
+
* @param iterator The values to find the maximum of.
|
|
424
|
+
*/
|
|
425
|
+
public static findMaximum<T extends Comparable<T>>(iterator: JavascriptIterator<T> | Iterator<T>): SyncResult<T>
|
|
426
|
+
{
|
|
427
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterable");
|
|
428
|
+
|
|
429
|
+
return SyncResult.create(() =>
|
|
430
|
+
{
|
|
431
|
+
if (isJavascriptIterator(iterator))
|
|
432
|
+
{
|
|
433
|
+
iterator = Iterator.create(iterator);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
let result: T = iterator.first()
|
|
437
|
+
.convertError(EmptyError, () => new EmptyError("Can't find the maximum of an empty Iterator."))
|
|
438
|
+
.await();
|
|
439
|
+
while (iterator.next().await())
|
|
440
|
+
{
|
|
441
|
+
const currentValue: T = iterator.getCurrent();
|
|
442
|
+
if (result.lessThan(currentValue))
|
|
443
|
+
{
|
|
444
|
+
result = currentValue;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return result;
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Get a new {@link Iterator} that wraps around this {@link Iterator} and only
|
|
454
|
+
* returns a maximum number of values from this {@link Iterator}.
|
|
455
|
+
* @param maximumToTake The maximum number of values to take from this {@link Iterator}.
|
|
456
|
+
*/
|
|
457
|
+
public take(maximumToTake: number): Iterator<T>
|
|
458
|
+
{
|
|
459
|
+
return Iterator.take(this, maximumToTake);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
public static take<T>(iterator: Iterator<T>, maximumToTake: number): Iterator<T>
|
|
463
|
+
{
|
|
464
|
+
return TakeIterator.create(iterator, maximumToTake);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Get a new {@link Iterator} that wraps around this {@link Iterator} and will skip the initial
|
|
469
|
+
* provided number of values before beginning to return values.
|
|
470
|
+
* @param maximumToSkip The maximum number of values to skip from this {@link Iterator}.
|
|
471
|
+
*/
|
|
472
|
+
public skip(maximumToSkip: number): Iterator<T>
|
|
473
|
+
{
|
|
474
|
+
return Iterator.skip(this, maximumToSkip);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
public static skip<T>(iterator: Iterator<T>, maximumToSkip: number): Iterator<T>
|
|
478
|
+
{
|
|
479
|
+
return SkipIterator.create(iterator, maximumToSkip);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Iterator } from "./iterator";
|
|
2
|
+
import { JavascriptIterator, JavascriptIteratorResult } from "./javascript";
|
|
3
|
+
import { PreCondition } from "./preCondition";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A JavaScript/TypeScript object that is used to iterate over a collection of values.
|
|
7
|
+
*/
|
|
8
|
+
export class IteratorToJavascriptIteratorAdapter<T> implements JavascriptIterator<T>
|
|
9
|
+
{
|
|
10
|
+
private readonly iterator: Iterator<T>;
|
|
11
|
+
private hasStarted: boolean;
|
|
12
|
+
|
|
13
|
+
private constructor(iterator: Iterator<T>)
|
|
14
|
+
{
|
|
15
|
+
PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
|
|
16
|
+
|
|
17
|
+
this.iterator = iterator;
|
|
18
|
+
this.hasStarted = false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public static create<T>(iterator: Iterator<T>): IteratorToJavascriptIteratorAdapter<T>
|
|
22
|
+
{
|
|
23
|
+
return new IteratorToJavascriptIteratorAdapter<T>(iterator);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public next(): JavascriptIteratorResult<T>
|
|
27
|
+
{
|
|
28
|
+
if (!this.hasStarted)
|
|
29
|
+
{
|
|
30
|
+
this.hasStarted = true;
|
|
31
|
+
this.iterator.start();
|
|
32
|
+
}
|
|
33
|
+
else
|
|
34
|
+
{
|
|
35
|
+
this.iterator.next();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const result: JavascriptIteratorResult<T> = {
|
|
39
|
+
done: !this.iterator.hasCurrent(),
|
|
40
|
+
value: undefined!,
|
|
41
|
+
};
|
|
42
|
+
if (!result.done)
|
|
43
|
+
{
|
|
44
|
+
result.value = this.iterator.getCurrent();
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The built-in {@link Array} type.
|
|
3
|
+
*
|
|
4
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
5
|
+
*/
|
|
6
|
+
export type JavascriptArray<T> = Array<T>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The built-in {@link Iterator} type.
|
|
10
|
+
*
|
|
11
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#iterators
|
|
12
|
+
*/
|
|
13
|
+
export type JavascriptIterator<T> = Iterator<T>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The built-in {@link IteratorResult} type.
|
|
17
|
+
*/
|
|
18
|
+
export type JavascriptIteratorResult<T> = IteratorResult<T,T>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The built-in {@link Iterable} type.
|
|
22
|
+
*
|
|
23
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#iterables
|
|
24
|
+
*/
|
|
25
|
+
export type JavascriptIterable<T> = Iterable<T>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The built-in {@link Map} type.
|
|
29
|
+
*
|
|
30
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
|
|
31
|
+
*/
|
|
32
|
+
export type JavascriptMap<TKey,TValue> = Map<TKey,TValue>;
|
|
33
|
+
/**
|
|
34
|
+
* The built-in {@link Map} type.
|
|
35
|
+
*
|
|
36
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
|
|
37
|
+
*/
|
|
38
|
+
export const JavascriptMap = Map;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The built-in {@link AsyncIterator} type.
|
|
42
|
+
*
|
|
43
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator
|
|
44
|
+
*/
|
|
45
|
+
export type JavascriptAsyncIterator<T> = AsyncIterator<T>;
|
|
46
|
+
|
|
47
|
+
export interface JavascriptAsyncIterable<T>
|
|
48
|
+
{
|
|
49
|
+
[Symbol.asyncIterator](): JavascriptAsyncIterator<T>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The built-in {@link Set} type.
|
|
54
|
+
*
|
|
55
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
|
|
56
|
+
*/
|
|
57
|
+
export type JavascriptSet<T> = Set<T>;
|
|
58
|
+
|
|
59
|
+
export const JavascriptSet = Set;
|