@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.
Files changed (163) hide show
  1. package/.c8rc.json +12 -0
  2. package/.github/workflows/publish.yml +38 -0
  3. package/.mocharc.json +9 -0
  4. package/README.md +9 -0
  5. package/package.json +36 -0
  6. package/sources/assertMessageParameters.ts +22 -0
  7. package/sources/asyncIterator.ts +437 -0
  8. package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +48 -0
  9. package/sources/asyncResult.ts +95 -0
  10. package/sources/basicDisposable.ts +57 -0
  11. package/sources/byteList.ts +202 -0
  12. package/sources/byteListStream.ts +121 -0
  13. package/sources/byteReadStream.ts +24 -0
  14. package/sources/byteWriteStream.ts +16 -0
  15. package/sources/bytes.ts +25 -0
  16. package/sources/characterList.ts +195 -0
  17. package/sources/characterListStream.ts +151 -0
  18. package/sources/characterReadStream.ts +81 -0
  19. package/sources/characterReadStreamIterator.ts +128 -0
  20. package/sources/characterWriteStream.ts +45 -0
  21. package/sources/commandLineParameter.ts +45 -0
  22. package/sources/commandLineParameters.ts +21 -0
  23. package/sources/comparable.ts +144 -0
  24. package/sources/comparer.ts +133 -0
  25. package/sources/comparison.ts +20 -0
  26. package/sources/concatenateIterable.ts +119 -0
  27. package/sources/concatenateIterator.ts +165 -0
  28. package/sources/condition.ts +329 -0
  29. package/sources/currentProcess.ts +158 -0
  30. package/sources/dateTime.ts +130 -0
  31. package/sources/depthFirstSearch.ts +230 -0
  32. package/sources/disposable.ts +31 -0
  33. package/sources/emptyError.ts +10 -0
  34. package/sources/english.ts +45 -0
  35. package/sources/equalFunctions.ts +123 -0
  36. package/sources/fetchHttpClient.ts +89 -0
  37. package/sources/fetchHttpResponse.ts +106 -0
  38. package/sources/flatMapIterable.ts +104 -0
  39. package/sources/flatMapIterator.ts +152 -0
  40. package/sources/generator.ts +251 -0
  41. package/sources/httpClient.ts +36 -0
  42. package/sources/httpHeader.ts +37 -0
  43. package/sources/httpHeaders.ts +216 -0
  44. package/sources/httpIncomingRequest.ts +30 -0
  45. package/sources/httpIncomingResponse.ts +19 -0
  46. package/sources/httpMethod.ts +164 -0
  47. package/sources/httpOutgoingRequest.ts +119 -0
  48. package/sources/httpOutgoingResponse.ts +113 -0
  49. package/sources/httpServer.ts +34 -0
  50. package/sources/inMemoryCharacterWriteStream.ts +78 -0
  51. package/sources/index.ts +101 -0
  52. package/sources/iterable.ts +345 -0
  53. package/sources/iterator.ts +481 -0
  54. package/sources/iteratorToJavascriptIteratorAdapter.ts +48 -0
  55. package/sources/javascript.ts +59 -0
  56. package/sources/javascriptArrayList.ts +175 -0
  57. package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +124 -0
  58. package/sources/javascriptIteratorToIteratorAdapter.ts +133 -0
  59. package/sources/javascriptMapMap.ts +143 -0
  60. package/sources/javascriptSetSet.ts +134 -0
  61. package/sources/list.ts +330 -0
  62. package/sources/listQueue.ts +62 -0
  63. package/sources/listStack.ts +62 -0
  64. package/sources/luxonDateTime.ts +109 -0
  65. package/sources/map.ts +302 -0
  66. package/sources/mapAsyncIterator.ts +141 -0
  67. package/sources/mapIterable.ts +105 -0
  68. package/sources/mapIterator.ts +145 -0
  69. package/sources/mutableCondition.ts +451 -0
  70. package/sources/mutableHttpHeaders.ts +204 -0
  71. package/sources/mutableMap.ts +292 -0
  72. package/sources/network.ts +18 -0
  73. package/sources/node.ts +37 -0
  74. package/sources/nodeJSCharacterWriteStream.ts +42 -0
  75. package/sources/nodeJSHttpIncomingRequest.ts +132 -0
  76. package/sources/nodeJSHttpServer.ts +134 -0
  77. package/sources/notFoundError.ts +12 -0
  78. package/sources/postCondition.ts +284 -0
  79. package/sources/postConditionError.ts +12 -0
  80. package/sources/preCondition.ts +284 -0
  81. package/sources/preConditionError.ts +12 -0
  82. package/sources/promiseAsyncResult.ts +174 -0
  83. package/sources/property.ts +63 -0
  84. package/sources/queue.ts +49 -0
  85. package/sources/realNetwork.ts +28 -0
  86. package/sources/recreationDotGovClient.ts +259 -0
  87. package/sources/searchControl.ts +42 -0
  88. package/sources/set.ts +244 -0
  89. package/sources/skipAsyncIterator.ts +145 -0
  90. package/sources/skipIterator.ts +155 -0
  91. package/sources/stack.ts +48 -0
  92. package/sources/stringComparer.ts +33 -0
  93. package/sources/stringIterator.ts +149 -0
  94. package/sources/strings.ts +322 -0
  95. package/sources/syncResult.ts +300 -0
  96. package/sources/takeAsyncIterator.ts +141 -0
  97. package/sources/takeIterator.ts +151 -0
  98. package/sources/toStringFunctions.ts +185 -0
  99. package/sources/types.ts +371 -0
  100. package/sources/whereAsyncIterator.ts +143 -0
  101. package/sources/whereIterable.ts +108 -0
  102. package/sources/whereIterator.ts +157 -0
  103. package/sources/wonderlandTrailClient.ts +1503 -0
  104. package/tests/assertTest.ts +113 -0
  105. package/tests/assertTestTests.ts +75 -0
  106. package/tests/basicTestSkip.ts +51 -0
  107. package/tests/byteListStreamTests.ts +390 -0
  108. package/tests/byteListTests.ts +27 -0
  109. package/tests/bytesTests.ts +43 -0
  110. package/tests/characterListStreamTests.ts +391 -0
  111. package/tests/characterListTests.ts +250 -0
  112. package/tests/characterWriteStreamTests.ts +12 -0
  113. package/tests/comparerTests.ts +92 -0
  114. package/tests/conditionTests.ts +877 -0
  115. package/tests/consoleTestRunner.ts +404 -0
  116. package/tests/consoleTestRunnerTests.ts +651 -0
  117. package/tests/dateTimeTests.ts +30 -0
  118. package/tests/depthFirstSearchTests.ts +106 -0
  119. package/tests/disposableTests.ts +121 -0
  120. package/tests/englishTests.ts +103 -0
  121. package/tests/equalFunctionsTests.ts +223 -0
  122. package/tests/failedTest.ts +43 -0
  123. package/tests/fetchHttpClientTests.ts +33 -0
  124. package/tests/generatorTests.ts +86 -0
  125. package/tests/httpClientTests.ts +18 -0
  126. package/tests/inMemoryCharacterWriteStreamTests.ts +117 -0
  127. package/tests/iterableTests.ts +141 -0
  128. package/tests/iteratorTests.ts +1086 -0
  129. package/tests/javascriptMapMapTests.ts +21 -0
  130. package/tests/listTests.ts +338 -0
  131. package/tests/mapIteratorTests.ts +55 -0
  132. package/tests/mapTests.ts +104 -0
  133. package/tests/mutableConditionTests.ts +273 -0
  134. package/tests/mutableMapTests.ts +154 -0
  135. package/tests/nodeJSHttpServerTests.ts +75 -0
  136. package/tests/notFoundErrorTests.ts +24 -0
  137. package/tests/postConditionErrorTests.ts +24 -0
  138. package/tests/preConditionErrorTests.ts +24 -0
  139. package/tests/promiseAsyncResultTests.ts +688 -0
  140. package/tests/propertyTests.ts +63 -0
  141. package/tests/queueTests.ts +29 -0
  142. package/tests/recreationDotGovClientTests.ts +191 -0
  143. package/tests/setTests.ts +140 -0
  144. package/tests/skippedTest.ts +39 -0
  145. package/tests/stackTests.ts +66 -0
  146. package/tests/stringComparerTests.ts +60 -0
  147. package/tests/stringIteratorTests.ts +156 -0
  148. package/tests/stringsTests.ts +516 -0
  149. package/tests/syncResultTests.ts +1251 -0
  150. package/tests/test.ts +228 -0
  151. package/tests/testAction.ts +75 -0
  152. package/tests/testActionTests.ts +93 -0
  153. package/tests/testFailureTests.ts +12 -0
  154. package/tests/testRunner.ts +267 -0
  155. package/tests/testRunnerTests.ts +895 -0
  156. package/tests/testSkip.ts +34 -0
  157. package/tests/tests.ts +103 -0
  158. package/tests/toStringFunctionsTests.ts +55 -0
  159. package/tests/typesTests.ts +257 -0
  160. package/tests/whereIteratorTests.ts +77 -0
  161. package/tests/wonderlandTrailClientTests.ts +452 -0
  162. package/tsconfig.json +17 -0
  163. package/tsup.config.ts +13 -0
@@ -0,0 +1,300 @@
1
+ import { PreCondition } from "./preCondition";
2
+ import { PromiseAsyncResult } from "./promiseAsyncResult";
3
+ import { instanceOfType, isPromise, isPromiseLike, isUndefinedOrNull, Type } from "./types";
4
+ import { AsyncResult } from "./asyncResult";
5
+
6
+ export class SyncResult<T> implements AsyncResult<T>
7
+ {
8
+ private value: T | undefined;
9
+ private error: unknown | undefined;
10
+
11
+ private constructor(value: T | undefined, error: unknown | undefined)
12
+ {
13
+ PreCondition.assertTrue(value === undefined || error === undefined, "value === undefined || error === undefined", "Either value or error must be undefined.");
14
+
15
+ this.value = value;
16
+ this.error = error;
17
+ }
18
+
19
+ public static create<T>(action: () => T): SyncResult<T>
20
+ {
21
+ PreCondition.assertNotUndefinedAndNotNull(action, "action");
22
+
23
+ let value: T | undefined;
24
+ let error: unknown | undefined;
25
+ try
26
+ {
27
+ value = action();
28
+ }
29
+ catch (e)
30
+ {
31
+ error = e;
32
+ }
33
+ return new SyncResult(value, error);
34
+ }
35
+
36
+ public static value<T>(value: T): SyncResult<T>
37
+ {
38
+ return new SyncResult(value, undefined);
39
+ }
40
+
41
+ public static error<T>(error: unknown): SyncResult<T>
42
+ {
43
+ return new SyncResult<T>(undefined, error);
44
+ }
45
+
46
+ public await(): T
47
+ {
48
+ if (this.error)
49
+ {
50
+ throw this.error;
51
+ }
52
+ return this.value!;
53
+ }
54
+
55
+ public then<TResult1 = T, TResult2 = never>(onfullfilled?: ((value: T) => TResult1) | null, onrejected?: ((reason: unknown) => TResult2) | null): SyncResult<TResult1 | TResult2>;
56
+ public then<TResult1 = T, TResult2 = never>(onfullfilled?: ((value: T) => (TResult1 | PromiseLike<TResult1>)) | null, onrejected?: ((reason: unknown) => (TResult2 | PromiseLike<TResult2>)) | null): SyncResult<TResult1 | TResult2>;
57
+ then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => (TResult1 | PromiseLike<TResult1>)) | null, onrejected?: ((reason: unknown) => (TResult2 | PromiseLike<TResult2>)) | null): SyncResult<TResult1 | TResult2> | PromiseAsyncResult<TResult1 | TResult2>
58
+ {
59
+ let result: SyncResult<TResult1 | TResult2> | PromiseAsyncResult<TResult1 | TResult2>;
60
+ if (this.error)
61
+ {
62
+ if (onrejected)
63
+ {
64
+ try
65
+ {
66
+ const onRejectedResult: TResult2 | PromiseLike<TResult2> = onrejected(this.error);
67
+ if (onRejectedResult instanceof SyncResult || onRejectedResult instanceof PromiseAsyncResult)
68
+ {
69
+ result = onRejectedResult;
70
+ }
71
+ else if (isPromise<TResult2>(onRejectedResult))
72
+ {
73
+ result = PromiseAsyncResult.create(onRejectedResult);
74
+ }
75
+ else
76
+ {
77
+ result = SyncResult.value(onRejectedResult as TResult2);
78
+ }
79
+ }
80
+ catch (error)
81
+ {
82
+ result = SyncResult.error(error);
83
+ }
84
+ }
85
+ else
86
+ {
87
+ result = SyncResult.error<TResult2>(this.error);
88
+ }
89
+ }
90
+ else
91
+ {
92
+ if (onfulfilled)
93
+ {
94
+ try
95
+ {
96
+ const onFullfilledResult: TResult1 | PromiseLike<TResult1> = onfulfilled(this.value!);
97
+ if (onFullfilledResult instanceof SyncResult || onFullfilledResult instanceof PromiseAsyncResult)
98
+ {
99
+ result = onFullfilledResult;
100
+ }
101
+ else if (isPromise<TResult1>(onFullfilledResult))
102
+ {
103
+ result = PromiseAsyncResult.create(onFullfilledResult);
104
+ }
105
+ else
106
+ {
107
+ result = SyncResult.value(onFullfilledResult as TResult1);
108
+ }
109
+ }
110
+ catch (error)
111
+ {
112
+ result = SyncResult.error(error);
113
+ }
114
+ }
115
+ else
116
+ {
117
+ result = SyncResult.value<TResult1>(this.value as TResult1);
118
+ }
119
+ }
120
+ return result;
121
+ }
122
+
123
+ public onValue(onValueFunction: (value: T) => void): SyncResult<T>;
124
+ public onValue(onValueFunction: (value: T) => Promise<void>): PromiseAsyncResult<T>;
125
+ onValue(onValueFunction: (value: T) => (void | Promise<void>)): SyncResult<T> | PromiseAsyncResult<T>
126
+ {
127
+ return this.then<T>((value: T) =>
128
+ {
129
+ let result: SyncResult<T> | PromiseAsyncResult<T>;
130
+ try
131
+ {
132
+ const onValueFunctionResult: (void | Promise<void>) = onValueFunction(value);
133
+ if (isPromise(onValueFunctionResult))
134
+ {
135
+ result = PromiseAsyncResult.create(onValueFunctionResult.then(() => value));
136
+ }
137
+ else
138
+ {
139
+ result = this;
140
+ }
141
+ }
142
+ catch (error)
143
+ {
144
+ result = SyncResult.error(error);
145
+ }
146
+ return result;
147
+ })
148
+ }
149
+
150
+ public catch<TResult = never>(onrejected: (reason: unknown) => TResult): SyncResult<T | TResult>;
151
+ public catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): SyncResult<T | TResult> | PromiseAsyncResult<T | TResult>
152
+ public catch<TError, TResult = never>(errorType: Type<TError>, onrejected: (reason: TError) => TResult): SyncResult<T | TResult>;
153
+ public catch<TError, TResult = never>(errorType: Type<TError>, onrejected: (reason: TError) => PromiseLike<TResult>): PromiseAsyncResult<T | TResult>;
154
+ catch<TResult = never>(errorTypeOrOnRejected?: Type<unknown> | ((reason: unknown) => TResult | PromiseLike<TResult>) | null, onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): SyncResult<T | TResult> | PromiseAsyncResult<T | TResult>
155
+ {
156
+ let errorType: Type<TypeError> | undefined;
157
+ if (!isUndefinedOrNull(onrejected))
158
+ {
159
+ errorType = errorTypeOrOnRejected as Type<unknown>;
160
+
161
+ PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
162
+ }
163
+ else
164
+ {
165
+ onrejected = errorTypeOrOnRejected as ((reason: any) => TResult | PromiseLike<TResult>) | null;
166
+ }
167
+ PreCondition.assertNotUndefinedAndNotNull(onrejected, "onrejected");
168
+
169
+ let result: SyncResult<T | TResult> | PromiseAsyncResult<T | TResult> = this;
170
+ if (this.error && (!errorType || instanceOfType(this.error, errorType)))
171
+ {
172
+ try
173
+ {
174
+ const onRejectedResult: TResult | PromiseLike<TResult> = onrejected(this.error);
175
+ if (onRejectedResult instanceof SyncResult || onRejectedResult instanceof PromiseAsyncResult)
176
+ {
177
+ result = onRejectedResult;
178
+ }
179
+ else if (isPromise<TResult>(onRejectedResult))
180
+ {
181
+ result = PromiseAsyncResult.create(onRejectedResult);
182
+ }
183
+ else
184
+ {
185
+ result = SyncResult.value(onRejectedResult as TResult);
186
+ }
187
+ }
188
+ catch (error)
189
+ {
190
+ result = SyncResult.error(error);
191
+ }
192
+ }
193
+ return result;
194
+ }
195
+
196
+ public onError(onErrorFunction: (reason: unknown) => void): SyncResult<T>;
197
+ public onError(onErrorFunction: (reason: unknown) => PromiseLike<void>): PromiseAsyncResult<T>;
198
+ public onError<TError>(errorType: Type<TError>, onErrorFunction: (reason: TError) => void): SyncResult<T>;
199
+ public onError<TError>(errorType: Type<TError>, onErrorFunction: (reason: TError) => PromiseLike<void>): PromiseAsyncResult<T>;
200
+ onError(errorTypeOrOnErrorFunction: Type<unknown> | ((reason: unknown) => (void | PromiseLike<void>)), onErrorFunction?: (reason: unknown) => (void | PromiseLike<void>)): SyncResult<T> | PromiseAsyncResult<T>
201
+ {
202
+ let errorType: Type<TypeError> | undefined;
203
+ if (!isUndefinedOrNull(onErrorFunction))
204
+ {
205
+ errorType = errorTypeOrOnErrorFunction as Type<unknown>;
206
+
207
+ PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
208
+ }
209
+ else
210
+ {
211
+ onErrorFunction = errorTypeOrOnErrorFunction as ((reason: any) => void | PromiseLike<void>);
212
+ }
213
+ PreCondition.assertNotUndefinedAndNotNull(onErrorFunction, "onErrorFunction");
214
+
215
+ let result: SyncResult<T> | PromiseAsyncResult<T> = this;
216
+ if (this.error && (!errorType || instanceOfType(this.error, errorType)))
217
+ {
218
+ try
219
+ {
220
+ const onErrorResult: void | PromiseLike<void> = onErrorFunction(this.error);
221
+ if (isPromise(onErrorResult))
222
+ {
223
+ result = PromiseAsyncResult.create(onErrorResult).then(() => this);
224
+ }
225
+ }
226
+ catch (error)
227
+ {
228
+ result = SyncResult.error(error);
229
+ }
230
+ }
231
+ return result;
232
+ }
233
+
234
+ public convertError(convertErrorFunction: (reason: unknown) => unknown): SyncResult<T>;
235
+ public convertError(onErrorFunction: (reason: unknown) => PromiseLike<unknown>): PromiseAsyncResult<T>;
236
+ public convertError<TError>(errorType: Type<TError>, convertErrorFunction: (reason: TError) => unknown): SyncResult<T>;
237
+ public convertError<TError>(errorType: Type<TError>, convertErrorFunction: (reason: TError) => PromiseLike<unknown>): PromiseAsyncResult<T>;
238
+ convertError(errorTypeOrConvertErrorFunction: Type<unknown> | ((reason: unknown) => (unknown | PromiseLike<unknown>)), convertErrorFunction?: (reason: unknown) => (unknown | PromiseLike<unknown>)): SyncResult<T> | PromiseAsyncResult<T>
239
+ {
240
+ let errorType: Type<TypeError> | undefined;
241
+ if (!isUndefinedOrNull(convertErrorFunction))
242
+ {
243
+ errorType = errorTypeOrConvertErrorFunction as Type<unknown>;
244
+
245
+ PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
246
+ }
247
+ else
248
+ {
249
+ convertErrorFunction = errorTypeOrConvertErrorFunction as ((reason: any) => void | PromiseLike<void>);
250
+ }
251
+ PreCondition.assertNotUndefinedAndNotNull(convertErrorFunction, "convertErrorFunction");
252
+
253
+ let result: SyncResult<T> | PromiseAsyncResult<T> = this;
254
+ if (this.error && (!errorType || instanceOfType(this.error, errorType)))
255
+ {
256
+ try
257
+ {
258
+ const convertErrorResult: unknown | PromiseLike<unknown> = convertErrorFunction(this.error);
259
+ if (isPromise(convertErrorResult))
260
+ {
261
+ result = PromiseAsyncResult.error(convertErrorResult);
262
+ }
263
+ else
264
+ {
265
+ result = SyncResult.error(convertErrorResult);
266
+ }
267
+ }
268
+ catch (error)
269
+ {
270
+ result = SyncResult.error(error);
271
+ }
272
+ }
273
+ return result;
274
+ }
275
+
276
+ public finally(onfinally?: (() => void) | null): SyncResult<T>
277
+ public finally(onfinally?: (() => Promise<void>) | null): PromiseAsyncResult<T>
278
+ finally(onfinally?: (() => (void | Promise<void>)) | null | undefined): PromiseAsyncResult<T> | SyncResult<T>
279
+ {
280
+ let result: PromiseAsyncResult<T> | SyncResult<T> = this;
281
+ if (onfinally)
282
+ {
283
+ try
284
+ {
285
+ const onfinallyResult: void | Promise<void> = onfinally();
286
+ if (isPromiseLike(onfinallyResult))
287
+ {
288
+ result = PromiseAsyncResult.create(onfinallyResult).then(() => this);
289
+ }
290
+ }
291
+ catch (error)
292
+ {
293
+ result = SyncResult.error(error);
294
+ }
295
+ }
296
+ return result;
297
+ }
298
+
299
+ readonly [Symbol.toStringTag]: string = "SyncResult2";
300
+ }
@@ -0,0 +1,141 @@
1
+ import { AsyncIterator } from "./asyncIterator";
2
+ import { PromiseAsyncResult } from "./promiseAsyncResult";
3
+ import { JavascriptAsyncIterator } from "./javascript";
4
+ import { PreCondition } from "./preCondition";
5
+ import { Type } from "./types";
6
+
7
+ /**
8
+ * An {@link AsyncIterator} that iterates over a maximum number of values.
9
+ */
10
+ export class TakeAsyncIterator<T> implements AsyncIterator<T>
11
+ {
12
+ private readonly innerIterator: AsyncIterator<T>;
13
+ private started: boolean;
14
+ private readonly maximumToTake: number;
15
+ private taken: number;
16
+
17
+ private constructor(innerIterator: AsyncIterator<T>, maximumToTake: number)
18
+ {
19
+ PreCondition.assertGreaterThanOrEqualTo(maximumToTake, 0, "maximumToTake");
20
+
21
+ this.innerIterator = innerIterator;
22
+ this.started = false;
23
+ this.maximumToTake = maximumToTake;
24
+ this.taken = 0;
25
+ }
26
+
27
+ public static create<T>(innerIterator: AsyncIterator<T>, maximumToTake: number)
28
+ {
29
+ return new TakeAsyncIterator(innerIterator, maximumToTake);
30
+ }
31
+
32
+ public hasCurrent(): boolean
33
+ {
34
+ return this.hasStarted() && this.taken <= this.maximumToTake && this.innerIterator.hasCurrent();
35
+ }
36
+
37
+ public next(): PromiseAsyncResult<boolean>
38
+ {
39
+ return PromiseAsyncResult.create(async () =>
40
+ {
41
+ if (this.maximumToTake <= 0)
42
+ {
43
+ return false;
44
+ }
45
+ else if (!this.hasStarted())
46
+ {
47
+ this.started = true;
48
+ await this.innerIterator.start();
49
+ this.taken++;
50
+ }
51
+ else if (this.taken <= this.maximumToTake)
52
+ {
53
+ await this.innerIterator.next();
54
+ this.taken++;
55
+ }
56
+ return this.hasCurrent();
57
+ });
58
+ }
59
+
60
+ public hasStarted(): boolean
61
+ {
62
+ return this.started;
63
+ }
64
+
65
+ public getCurrent(): T
66
+ {
67
+ PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
68
+
69
+ return this.innerIterator.getCurrent();
70
+ }
71
+
72
+ public start(): PromiseAsyncResult<this>
73
+ {
74
+ return AsyncIterator.start<T, this>(this);
75
+ }
76
+
77
+ public takeCurrent(): PromiseAsyncResult<T>
78
+ {
79
+ return AsyncIterator.takeCurrent(this);
80
+ }
81
+
82
+ public any(): PromiseAsyncResult<boolean>
83
+ {
84
+ return AsyncIterator.any(this);
85
+ }
86
+
87
+ public getCount(): PromiseAsyncResult<number>
88
+ {
89
+ return AsyncIterator.getCount(this);
90
+ }
91
+
92
+ public toArray(): PromiseAsyncResult<T[]>
93
+ {
94
+ return AsyncIterator.toArray(this);
95
+ }
96
+
97
+ public where(condition: (value: T) => boolean): AsyncIterator<T>
98
+ {
99
+ return AsyncIterator.where(this, condition);
100
+ }
101
+
102
+ public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): AsyncIterator<U>
103
+ {
104
+ return AsyncIterator.whereInstanceOf(this, typeCheck);
105
+ }
106
+
107
+ public whereInstanceOfType<U extends T>(type: Type<U>): AsyncIterator<U>
108
+ {
109
+ return AsyncIterator.whereInstanceOfType(this, type);
110
+ }
111
+
112
+ public map<TOutput>(mapping: (value: T) => (TOutput | PromiseLike<TOutput>)): AsyncIterator<TOutput>
113
+ {
114
+ return AsyncIterator.map(this, mapping);
115
+ }
116
+
117
+ public first(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
118
+ {
119
+ return AsyncIterator.first(this, condition);
120
+ }
121
+
122
+ public last(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
123
+ {
124
+ return AsyncIterator.last(this, condition);
125
+ }
126
+
127
+ public [Symbol.asyncIterator](): JavascriptAsyncIterator<T>
128
+ {
129
+ return AsyncIterator[Symbol.asyncIterator](this);
130
+ }
131
+
132
+ public take(maximumToTake: number): AsyncIterator<T>
133
+ {
134
+ return AsyncIterator.take(this, maximumToTake);
135
+ }
136
+
137
+ public skip(maximumToSkip: number): AsyncIterator<T>
138
+ {
139
+ return AsyncIterator.skip(this, maximumToSkip);
140
+ }
141
+ }
@@ -0,0 +1,151 @@
1
+ import { Iterator } from "./iterator";
2
+ import { JavascriptIterable, JavascriptIterator } from "./javascript";
3
+ import { PreCondition } from "./preCondition";
4
+ import { SyncResult } from "./syncResult";
5
+ import { Type } from "./types";
6
+
7
+ /**
8
+ * An {@link Iterator} that iterates over a maximum number of values.
9
+ */
10
+ export class TakeIterator<T> implements Iterator<T>
11
+ {
12
+ private readonly innerIterator: Iterator<T>;
13
+ private started: boolean;
14
+ private readonly maximumToTake: number;
15
+ private taken: number;
16
+
17
+ private constructor(innerIterator: Iterator<T>, maximumToTake: number)
18
+ {
19
+ PreCondition.assertGreaterThanOrEqualTo(maximumToTake, 0, "maximumToTake");
20
+
21
+ this.innerIterator = innerIterator;
22
+ this.started = false;
23
+ this.maximumToTake = maximumToTake;
24
+ this.taken = 0;
25
+ }
26
+
27
+ public static create<T>(innerIterator: Iterator<T>, maximumToTake: number)
28
+ {
29
+ return new TakeIterator(innerIterator, maximumToTake);
30
+ }
31
+
32
+ public hasCurrent(): boolean
33
+ {
34
+ return this.hasStarted() && this.taken <= this.maximumToTake && this.innerIterator.hasCurrent();
35
+ }
36
+
37
+ public next(): SyncResult<boolean>
38
+ {
39
+ return SyncResult.create(() =>
40
+ {
41
+ if (this.maximumToTake <= 0)
42
+ {
43
+ return false;
44
+ }
45
+ else if (!this.hasStarted())
46
+ {
47
+ this.started = true;
48
+ this.innerIterator.start().await();
49
+ this.taken++;
50
+ }
51
+ else if (this.taken <= this.maximumToTake)
52
+ {
53
+ this.innerIterator.next().await();
54
+ this.taken++;
55
+ }
56
+ return this.hasCurrent();
57
+ });
58
+ }
59
+
60
+ public hasStarted(): boolean
61
+ {
62
+ return this.started;
63
+ }
64
+
65
+ public getCurrent(): T
66
+ {
67
+ PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
68
+
69
+ return this.innerIterator.getCurrent();
70
+ }
71
+
72
+ public start(): SyncResult<this>
73
+ {
74
+ return Iterator.start<T, this>(this);
75
+ }
76
+
77
+ public takeCurrent(): SyncResult<T>
78
+ {
79
+ return Iterator.takeCurrent(this);
80
+ }
81
+
82
+ public any(): SyncResult<boolean>
83
+ {
84
+ return Iterator.any(this);
85
+ }
86
+
87
+ public getCount(): SyncResult<number>
88
+ {
89
+ return Iterator.getCount(this);
90
+ }
91
+
92
+ public toArray(): SyncResult<T[]>
93
+ {
94
+ return Iterator.toArray(this);
95
+ }
96
+
97
+ public concatenate(...toConcatenate: JavascriptIterable<T>[]): Iterator<T>
98
+ {
99
+ return Iterator.concatenate(this, ...toConcatenate);
100
+ }
101
+
102
+ public where(condition: (value: T) => boolean): Iterator<T>
103
+ {
104
+ return Iterator.where(this, condition);
105
+ }
106
+
107
+ public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): Iterator<U>
108
+ {
109
+ return Iterator.whereInstanceOf(this, typeCheck);
110
+ }
111
+
112
+ public whereInstanceOfType<U extends T>(type: Type<U>): Iterator<U>
113
+ {
114
+ return Iterator.whereInstanceOfType(this, type);
115
+ }
116
+
117
+ public map<TOutput>(mapping: (value: T) => (TOutput | SyncResult<TOutput>)): Iterator<TOutput>
118
+ {
119
+ return Iterator.map(this, mapping);
120
+ }
121
+
122
+ public flatMap<TOutput>(mapping: (value: T) => JavascriptIterable<TOutput>): Iterator<TOutput>
123
+ {
124
+ return Iterator.flatMap(this, mapping);
125
+ }
126
+
127
+ public first(condition?: (value: T) => boolean): SyncResult<T>
128
+ {
129
+ return Iterator.first(this, condition);
130
+ }
131
+
132
+ public last(condition?: (value: T) => boolean): SyncResult<T>
133
+ {
134
+ return Iterator.last(this, condition);
135
+ }
136
+
137
+ public [Symbol.iterator](): JavascriptIterator<T>
138
+ {
139
+ return Iterator[Symbol.iterator](this);
140
+ }
141
+
142
+ public take(maximumToTake: number): Iterator<T>
143
+ {
144
+ return Iterator.take(this, maximumToTake);
145
+ }
146
+
147
+ public skip(maximumToSkip: number): Iterator<T>
148
+ {
149
+ return Iterator.skip(this, maximumToSkip);
150
+ }
151
+ }