@everyonesoftware/common 2.0.0 → 4.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 (176) hide show
  1. package/outputs/chunk-AY3YWKOM.js +7741 -0
  2. package/outputs/chunk-AY3YWKOM.js.map +1 -0
  3. package/outputs/sources.cjs +9007 -0
  4. package/outputs/sources.cjs.map +1 -0
  5. package/outputs/sources.d.cts +4082 -0
  6. package/outputs/sources.d.ts +4082 -0
  7. package/outputs/sources.js +1370 -0
  8. package/outputs/sources.js.map +1 -0
  9. package/outputs/tests.cjs +17208 -0
  10. package/outputs/tests.cjs.map +1 -0
  11. package/outputs/tests.d.cts +3 -0
  12. package/outputs/tests.d.ts +3 -0
  13. package/outputs/tests.js +9798 -0
  14. package/outputs/tests.js.map +1 -0
  15. package/package.json +11 -2
  16. package/.c8rc.json +0 -12
  17. package/.github/workflows/publish.yml +0 -36
  18. package/.mocharc.json +0 -9
  19. package/sources/assertMessageParameters.ts +0 -22
  20. package/sources/asyncIterator.ts +0 -437
  21. package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +0 -48
  22. package/sources/asyncResult.ts +0 -95
  23. package/sources/basicDisposable.ts +0 -57
  24. package/sources/byteList.ts +0 -202
  25. package/sources/byteListStream.ts +0 -121
  26. package/sources/byteReadStream.ts +0 -24
  27. package/sources/byteWriteStream.ts +0 -16
  28. package/sources/bytes.ts +0 -25
  29. package/sources/characterList.ts +0 -195
  30. package/sources/characterListStream.ts +0 -151
  31. package/sources/characterReadStream.ts +0 -81
  32. package/sources/characterReadStreamIterator.ts +0 -128
  33. package/sources/characterWriteStream.ts +0 -45
  34. package/sources/commandLineParameter.ts +0 -45
  35. package/sources/commandLineParameters.ts +0 -21
  36. package/sources/comparable.ts +0 -144
  37. package/sources/comparer.ts +0 -133
  38. package/sources/comparison.ts +0 -20
  39. package/sources/concatenateIterable.ts +0 -119
  40. package/sources/concatenateIterator.ts +0 -165
  41. package/sources/condition.ts +0 -329
  42. package/sources/currentProcess.ts +0 -158
  43. package/sources/dateTime.ts +0 -130
  44. package/sources/depthFirstSearch.ts +0 -230
  45. package/sources/disposable.ts +0 -31
  46. package/sources/emptyError.ts +0 -10
  47. package/sources/english.ts +0 -45
  48. package/sources/equalFunctions.ts +0 -123
  49. package/sources/fetchHttpClient.ts +0 -89
  50. package/sources/fetchHttpResponse.ts +0 -106
  51. package/sources/flatMapIterable.ts +0 -104
  52. package/sources/flatMapIterator.ts +0 -152
  53. package/sources/generator.ts +0 -251
  54. package/sources/httpClient.ts +0 -36
  55. package/sources/httpHeader.ts +0 -37
  56. package/sources/httpHeaders.ts +0 -216
  57. package/sources/httpIncomingRequest.ts +0 -30
  58. package/sources/httpIncomingResponse.ts +0 -19
  59. package/sources/httpMethod.ts +0 -164
  60. package/sources/httpOutgoingRequest.ts +0 -119
  61. package/sources/httpOutgoingResponse.ts +0 -113
  62. package/sources/httpServer.ts +0 -34
  63. package/sources/inMemoryCharacterWriteStream.ts +0 -78
  64. package/sources/index.ts +0 -101
  65. package/sources/iterable.ts +0 -345
  66. package/sources/iterator.ts +0 -481
  67. package/sources/iteratorToJavascriptIteratorAdapter.ts +0 -48
  68. package/sources/javascript.ts +0 -59
  69. package/sources/javascriptArrayList.ts +0 -175
  70. package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +0 -124
  71. package/sources/javascriptIteratorToIteratorAdapter.ts +0 -133
  72. package/sources/javascriptMapMap.ts +0 -143
  73. package/sources/javascriptSetSet.ts +0 -134
  74. package/sources/list.ts +0 -330
  75. package/sources/listQueue.ts +0 -62
  76. package/sources/listStack.ts +0 -62
  77. package/sources/luxonDateTime.ts +0 -109
  78. package/sources/map.ts +0 -302
  79. package/sources/mapAsyncIterator.ts +0 -141
  80. package/sources/mapIterable.ts +0 -105
  81. package/sources/mapIterator.ts +0 -145
  82. package/sources/mutableCondition.ts +0 -451
  83. package/sources/mutableHttpHeaders.ts +0 -204
  84. package/sources/mutableMap.ts +0 -292
  85. package/sources/network.ts +0 -18
  86. package/sources/node.ts +0 -37
  87. package/sources/nodeJSCharacterWriteStream.ts +0 -42
  88. package/sources/nodeJSHttpIncomingRequest.ts +0 -132
  89. package/sources/nodeJSHttpServer.ts +0 -134
  90. package/sources/notFoundError.ts +0 -12
  91. package/sources/postCondition.ts +0 -284
  92. package/sources/postConditionError.ts +0 -12
  93. package/sources/preCondition.ts +0 -284
  94. package/sources/preConditionError.ts +0 -12
  95. package/sources/promiseAsyncResult.ts +0 -174
  96. package/sources/property.ts +0 -63
  97. package/sources/queue.ts +0 -49
  98. package/sources/realNetwork.ts +0 -28
  99. package/sources/recreationDotGovClient.ts +0 -259
  100. package/sources/searchControl.ts +0 -42
  101. package/sources/set.ts +0 -244
  102. package/sources/skipAsyncIterator.ts +0 -145
  103. package/sources/skipIterator.ts +0 -155
  104. package/sources/stack.ts +0 -48
  105. package/sources/stringComparer.ts +0 -33
  106. package/sources/stringIterator.ts +0 -149
  107. package/sources/strings.ts +0 -322
  108. package/sources/syncResult.ts +0 -300
  109. package/sources/takeAsyncIterator.ts +0 -141
  110. package/sources/takeIterator.ts +0 -151
  111. package/sources/toStringFunctions.ts +0 -185
  112. package/sources/types.ts +0 -371
  113. package/sources/whereAsyncIterator.ts +0 -143
  114. package/sources/whereIterable.ts +0 -108
  115. package/sources/whereIterator.ts +0 -157
  116. package/sources/wonderlandTrailClient.ts +0 -1503
  117. package/tests/assertTest.ts +0 -113
  118. package/tests/assertTestTests.ts +0 -75
  119. package/tests/basicTestSkip.ts +0 -51
  120. package/tests/byteListStreamTests.ts +0 -390
  121. package/tests/byteListTests.ts +0 -27
  122. package/tests/bytesTests.ts +0 -43
  123. package/tests/characterListStreamTests.ts +0 -391
  124. package/tests/characterListTests.ts +0 -250
  125. package/tests/characterWriteStreamTests.ts +0 -12
  126. package/tests/comparerTests.ts +0 -92
  127. package/tests/conditionTests.ts +0 -877
  128. package/tests/consoleTestRunner.ts +0 -404
  129. package/tests/consoleTestRunnerTests.ts +0 -651
  130. package/tests/dateTimeTests.ts +0 -30
  131. package/tests/depthFirstSearchTests.ts +0 -106
  132. package/tests/disposableTests.ts +0 -121
  133. package/tests/englishTests.ts +0 -103
  134. package/tests/equalFunctionsTests.ts +0 -223
  135. package/tests/failedTest.ts +0 -43
  136. package/tests/fetchHttpClientTests.ts +0 -33
  137. package/tests/generatorTests.ts +0 -86
  138. package/tests/httpClientTests.ts +0 -18
  139. package/tests/inMemoryCharacterWriteStreamTests.ts +0 -117
  140. package/tests/iterableTests.ts +0 -141
  141. package/tests/iteratorTests.ts +0 -1086
  142. package/tests/javascriptMapMapTests.ts +0 -21
  143. package/tests/listTests.ts +0 -338
  144. package/tests/mapIteratorTests.ts +0 -55
  145. package/tests/mapTests.ts +0 -104
  146. package/tests/mutableConditionTests.ts +0 -273
  147. package/tests/mutableMapTests.ts +0 -154
  148. package/tests/nodeJSHttpServerTests.ts +0 -75
  149. package/tests/notFoundErrorTests.ts +0 -24
  150. package/tests/postConditionErrorTests.ts +0 -24
  151. package/tests/preConditionErrorTests.ts +0 -24
  152. package/tests/promiseAsyncResultTests.ts +0 -688
  153. package/tests/propertyTests.ts +0 -63
  154. package/tests/queueTests.ts +0 -29
  155. package/tests/recreationDotGovClientTests.ts +0 -191
  156. package/tests/setTests.ts +0 -140
  157. package/tests/skippedTest.ts +0 -39
  158. package/tests/stackTests.ts +0 -66
  159. package/tests/stringComparerTests.ts +0 -60
  160. package/tests/stringIteratorTests.ts +0 -156
  161. package/tests/stringsTests.ts +0 -516
  162. package/tests/syncResultTests.ts +0 -1251
  163. package/tests/test.ts +0 -228
  164. package/tests/testAction.ts +0 -75
  165. package/tests/testActionTests.ts +0 -93
  166. package/tests/testFailureTests.ts +0 -12
  167. package/tests/testRunner.ts +0 -267
  168. package/tests/testRunnerTests.ts +0 -895
  169. package/tests/testSkip.ts +0 -34
  170. package/tests/tests.ts +0 -103
  171. package/tests/toStringFunctionsTests.ts +0 -55
  172. package/tests/typesTests.ts +0 -257
  173. package/tests/whereIteratorTests.ts +0 -77
  174. package/tests/wonderlandTrailClientTests.ts +0 -452
  175. package/tsconfig.json +0 -20
  176. package/tsup.config.ts +0 -13
package/package.json CHANGED
@@ -1,11 +1,19 @@
1
1
  {
2
2
  "name": "@everyonesoftware/common",
3
- "version": "2.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "A package containing common code for everyonesoftware projects.",
5
+ "repository": {
6
+ "url": "https://github.com/everyonesoftware/common-typescript"
7
+ },
5
8
  "type": "module",
6
9
  "publishConfig": {
7
10
  "access": "public"
8
11
  },
12
+ "files": [
13
+ "outputs/",
14
+ "README.md",
15
+ "package.json"
16
+ ],
9
17
  "exports": {
10
18
  ".": {
11
19
  "types": "./outputs/sources.d.ts",
@@ -21,7 +29,8 @@
21
29
  "scripts": {
22
30
  "build": "barrelsby --directory sources --delete && tsup",
23
31
  "clean": "rimraf coverage outputs",
24
- "test": "npm run build && c8 node -r source-map-support/register outputs/tests.js"
32
+ "test-only": "c8 node -r source-map-support/register outputs/tests.js",
33
+ "test": "npm run build && npm run test-only"
25
34
  },
26
35
  "dependencies": {
27
36
  "luxon": "3.7.2"
package/.c8rc.json DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "all": true,
3
- "src": [
4
- "sources"
5
- ],
6
- "temp-directory": "coverage/temp",
7
- "report-dir": "coverage",
8
- "reporter": [
9
- "lcov",
10
- "html"
11
- ]
12
- }
@@ -1,36 +0,0 @@
1
- name: Build, Test, and Publish
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
-
8
- jobs:
9
- build-test-publish:
10
- runs-on: ubuntu-latest
11
-
12
- permissions:
13
- contents: read
14
-
15
- steps:
16
- - name: Checkout repository
17
- uses: actions/checkout@v5
18
-
19
- - name: Setup Node.js
20
- uses: actions/setup-node@v5
21
- with:
22
- node-version: 22
23
- registry-url: 'https://registry.npmjs.org'
24
- cache: 'npm'
25
-
26
- - name: Install dependencies
27
- run: npm ci
28
-
29
- - name: Build
30
- run: npm run build
31
-
32
- - name: Run tests
33
- run: npm test
34
-
35
- - name: Publish to NPM
36
- run: npm publish
package/.mocharc.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "color": true,
3
- "ui": "tdd",
4
- "extension": [
5
- "ts"
6
- ],
7
- "spec": "tests/**/*.ts",
8
- "require": "ts-node/register"
9
- }
@@ -1,22 +0,0 @@
1
- /**
2
- * A collection of parameters that can be passed to an assert error message function.
3
- */
4
- export interface AssertMessageParameters
5
- {
6
- /**
7
- * The expected state.
8
- */
9
- expected: string,
10
- /**
11
- * The actual state.
12
- */
13
- actual: string,
14
- /**
15
- * A string representation of the expression that produced the actual state.
16
- */
17
- expression?: string,
18
- /**
19
- * A message that describes the failure.
20
- */
21
- message?: string,
22
- }
@@ -1,437 +0,0 @@
1
- import { AsyncIteratorToJavascriptAsyncIteratorAdapter } from "./asyncIteratorToJavascriptAsyncIteratorAdapter";
2
- import { PromiseAsyncResult } from "./promiseAsyncResult";
3
- import { Comparable } from "./comparable";
4
- import { EmptyError } from "./emptyError";
5
- import { JavascriptAsyncIterator } from "./javascript";
6
- import { JavascriptAsyncIteratorToAsyncIteratorAdapter } from "./javascriptAsyncIteratorToAsyncIteratorAdapter";
7
- import { MapAsyncIterator } from "./mapAsyncIterator";
8
- import { NotFoundError } from "./notFoundError";
9
- import { PreCondition } from "./preCondition";
10
- import { SkipAsyncIterator } from "./skipAsyncIterator";
11
- import { TakeAsyncIterator } from "./takeAsyncIterator";
12
- import { instanceOfType, isJavascriptAsyncIterator, isUndefinedOrNull, Type } from "./types";
13
- import { WhereAsyncIterator } from "./whereAsyncIterator";
14
-
15
- /**
16
- * A type that can be used to asynchronously iterate over a collection.
17
- */
18
- export abstract class AsyncIterator<T>
19
- {
20
- public static create<T>(iterator: JavascriptAsyncIterator<T>): AsyncIterator<T>
21
- {
22
- return JavascriptAsyncIteratorToAsyncIteratorAdapter.create(iterator);
23
- }
24
-
25
- /**
26
- * Move to the next value in the collection. Return whether this {@link AsyncIterator} points to
27
- * a value after the move.
28
- */
29
- public abstract next(): PromiseAsyncResult<boolean>;
30
-
31
- /**
32
- * Get whether this {@link AsyncIterator} has started iterating over the values in the collection.
33
- */
34
- public abstract hasStarted(): boolean;
35
-
36
- /**
37
- * Get whether this {@link AsyncIterator} currently points at a value in the collection.
38
- */
39
- public abstract hasCurrent(): boolean;
40
-
41
- /**
42
- * Get the value that this {@link AsyncIterator} points to.
43
- */
44
- public abstract getCurrent(): T;
45
-
46
- /**
47
- * Move to the first value if this {@link AsyncIterator} hasn't started yet.
48
- * @returns This object for method chaining.
49
- */
50
- public start(): PromiseAsyncResult<this>
51
- {
52
- return AsyncIterator.start(this);
53
- }
54
-
55
- /**
56
- * Move the provided {@link AsyncIterator} to its first value if it hasn't started yet.
57
- */
58
- public static start<T, TIterator extends AsyncIterator<T>>(iterator: TIterator): PromiseAsyncResult<TIterator>
59
- {
60
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
61
-
62
- return PromiseAsyncResult.create(async () =>
63
- {
64
- if (!iterator.hasStarted())
65
- {
66
- await iterator.next();
67
- }
68
- return iterator;
69
- });
70
- }
71
-
72
- /**
73
- * Get the current value from this {@link AsyncIterator} and advance this {@link AsyncIterator} to the
74
- * next value.
75
- */
76
- public takeCurrent(): PromiseAsyncResult<T>
77
- {
78
- return AsyncIterator.takeCurrent(this);
79
- }
80
-
81
- public static takeCurrent<T>(iterator: AsyncIterator<T>): PromiseAsyncResult<T>
82
- {
83
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
84
- PreCondition.assertTrue(iterator.hasCurrent(), "iterator.hasCurrent()");
85
-
86
- return PromiseAsyncResult.create(async () =>
87
- {
88
- const result: T = iterator.getCurrent();
89
- await iterator.next();
90
- return result;
91
- });
92
- }
93
-
94
- public [Symbol.asyncIterator](): JavascriptAsyncIterator<T>
95
- {
96
- return AsyncIterator[Symbol.asyncIterator](this);
97
- }
98
-
99
- /**
100
- * Convert the provided {@link AsyncIterator} to a {@link IteratorToJavascriptIteratorAdapter}.
101
- * @param iterator The {@link AsyncIterator} to convert.
102
- */
103
- public static [Symbol.asyncIterator]<T>(iterator: AsyncIterator<T>): JavascriptAsyncIterator<T>
104
- {
105
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
106
-
107
- return AsyncIteratorToJavascriptAsyncIteratorAdapter.create(iterator);
108
- }
109
-
110
- /**
111
- * Get whether this {@link AsyncIterator} contains any values.
112
- * Note: This may advance the {@link AsyncIterator} to the first value if it hasn't been
113
- * started yet.
114
- */
115
- public any(): PromiseAsyncResult<boolean>
116
- {
117
- return AsyncIterator.any(this);
118
- }
119
-
120
- /**
121
- * Get whether this {@link AsyncIterator} contains any values.
122
- * Note: This may advance the {@link AsyncIterator} to the first value if it hasn't been
123
- * started yet.
124
- */
125
- public static any<T>(iterator: AsyncIterator<T>): PromiseAsyncResult<boolean>
126
- {
127
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
128
-
129
- return PromiseAsyncResult.create(async () =>
130
- {
131
- await iterator.start();
132
- return iterator.hasCurrent();
133
- });
134
-
135
- }
136
-
137
- /**
138
- * Get the number of values in this {@link AsyncIterator}.
139
- * Note: This will consume all of the values in this {@link AsyncIterator}.
140
- */
141
- public getCount(): PromiseAsyncResult<number>
142
- {
143
- return AsyncIterator.getCount(this);
144
- }
145
-
146
- /**
147
- * Get the number of values in the provided {@link AsyncIterator}.
148
- * Note: This will consume all of the values in the provided {@link AsyncIterator}.
149
- */
150
- public static getCount<T>(iterator: AsyncIterator<T>): PromiseAsyncResult<number>
151
- {
152
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
153
-
154
- return PromiseAsyncResult.create(async () =>
155
- {
156
- let result: number = 0;
157
- if (iterator.hasCurrent())
158
- {
159
- result++;
160
- }
161
- while (await iterator.next())
162
- {
163
- result++;
164
- }
165
- return result;
166
- });
167
- }
168
-
169
- /**
170
- * Get all of the remaining values in this {@link AsyncIterator} in a {@link T} {@link Array}.
171
- */
172
- public toArray(): PromiseAsyncResult<T[]>
173
- {
174
- return AsyncIterator.toArray(this);
175
- }
176
-
177
- /**
178
- * Get all of the remaining values in the provided {@link AsyncIterator} in a {@link T}
179
- * {@link Array}.
180
- */
181
- public static toArray<T>(iterator: AsyncIterator<T>): PromiseAsyncResult<T[]>
182
- {
183
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
184
-
185
- return PromiseAsyncResult.create(async () =>
186
- {
187
- const result: T[] = [];
188
-
189
- if (iterator.hasCurrent())
190
- {
191
- result.push(iterator.getCurrent());
192
- }
193
- while (await iterator.next())
194
- {
195
- result.push(iterator.getCurrent());
196
- }
197
-
198
- return result;
199
- });
200
- }
201
-
202
- /**
203
- * Get an {@link AsyncIterator} that will only return values that match the provided condition.
204
- * @param condition The condition to run against each of the values in this {@link AsyncIterator}.
205
- */
206
- public where(condition: (value: T) => (boolean | PromiseLike<boolean>)): AsyncIterator<T>
207
- {
208
- return AsyncIterator.where(this, condition);
209
- }
210
-
211
- public static where<T>(iterator: AsyncIterator<T>, condition: (value: T) => (boolean | PromiseLike<boolean>)): AsyncIterator<T>
212
- {
213
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
214
- PreCondition.assertNotUndefinedAndNotNull(condition, "condition");
215
-
216
- return WhereAsyncIterator.create(iterator, condition);
217
- }
218
-
219
- /**
220
- * Get a {@link MapIterator} that will map all {@link T} values from this {@link AsyncIterator} to
221
- * {@link TOutput} values.
222
- * @param mapping The mapping that maps {@link T} values to {@link TOutput} values.
223
- */
224
- public map<TOutput>(mapping: (value: T) => (TOutput | PromiseLike<TOutput>)): AsyncIterator<TOutput>
225
- {
226
- return AsyncIterator.map(this, mapping);
227
- }
228
-
229
- public static map<T, TOutput>(iterator: AsyncIterator<T>, mapping: (value: T) => (TOutput | PromiseLike<TOutput>)): AsyncIterator<TOutput>
230
- {
231
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
232
- PreCondition.assertNotUndefinedAndNotNull(mapping, "mapping");
233
-
234
- return MapAsyncIterator.create(iterator, mapping);
235
- }
236
-
237
- /**
238
- * Get an {@link AsyncIterator} that will filter this {@link AsyncIterator} to only the values that are
239
- * instances of {@link U} based on the provided type check {@link Function}.
240
- * @param typeCheck The {@link Function} that will be used to determine if values are of type
241
- * {@link U}.
242
- */
243
- public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): AsyncIterator<U>
244
- {
245
- return AsyncIterator.whereInstanceOf(this, typeCheck);
246
- }
247
-
248
- public static whereInstanceOf<T, U extends T>(iterator: AsyncIterator<T>, typeCheck: (value: T) => value is U): AsyncIterator<U>
249
- {
250
- PreCondition.assertNotUndefinedAndNotNull(typeCheck, "typeCheck");
251
-
252
- return iterator.where(typeCheck)
253
- .map((value: T) => value as U);
254
- }
255
-
256
- /**
257
- * Get an {@link AsyncIterator} that will filter this {@link AsyncIterator} to only the values that are
258
- * instances of the provided {@link Type}.
259
- * @param type The type of values to return from the new {@link AsyncIterator}.
260
- */
261
- public whereInstanceOfType<U extends T>(type: Type<U>): AsyncIterator<U>
262
- {
263
- return AsyncIterator.whereInstanceOfType(this, type);
264
- }
265
-
266
- public static whereInstanceOfType<T, U extends T>(iterator: AsyncIterator<T>, type: Type<U>): AsyncIterator<U>
267
- {
268
- PreCondition.assertNotUndefinedAndNotNull(type, "type");
269
-
270
- return iterator.whereInstanceOf((value: T) => instanceOfType(value, type));
271
- }
272
-
273
- /**
274
- * If the condition function is undefined, then this function will return the first value in
275
- * this {@link AsyncIterator}. If this condition function is provided, then this function will return
276
- * the first value that matches the provided condition.
277
- * @param condition The condition that the returned value must satisfy.
278
- */
279
- public first(condition?: (value: T) => boolean): PromiseAsyncResult<T>
280
- {
281
- return AsyncIterator.first(this, condition);
282
- }
283
-
284
- /**
285
- * If the condition function is undefined, then this function will return the first value in
286
- * the {@link AsyncIterator}. If this condition function is provided, then this function will return
287
- * the first value that matches the provided condition.
288
- * @param iterator The {@link AsyncIterator} to get the first value from.
289
- */
290
- public static first<T>(iterator: AsyncIterator<T>, condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
291
- {
292
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
293
-
294
- return PromiseAsyncResult.create(async () =>
295
- {
296
- await iterator.start();
297
- if (isUndefinedOrNull(condition))
298
- {
299
- if (!iterator.hasCurrent())
300
- {
301
- throw new EmptyError();
302
- }
303
- }
304
- else
305
- {
306
- while (iterator.hasCurrent() && !await condition(iterator.getCurrent()))
307
- {
308
- await iterator.next();
309
- }
310
-
311
- if (!iterator.hasCurrent())
312
- {
313
- throw new NotFoundError("No value was found in the AsyncIterator that matched the provided condition.");
314
- }
315
- }
316
- return iterator.getCurrent();
317
- });
318
- }
319
-
320
- /**
321
- * If the condition function is undefined, then this function will return the last value in this
322
- * {@link AsyncIterator}. If this condition function is provided, then this function will return the
323
- * last value that matches the provided condition.
324
- * @param condition The condition that the returned value must satisfy.
325
- */
326
- public last(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
327
- {
328
- return AsyncIterator.last(this, condition);
329
- }
330
-
331
- /**
332
- * If the condition function is undefined, then this function will return the last value in the
333
- * {@link AsyncIterator}. If this condition function is provided, then this function will return the
334
- * last value that matches the provided condition.
335
- * @param iterator The {@link AsyncIterator} to get the last value from.
336
- */
337
- public static last<T>(iterator: AsyncIterator<T>, condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
338
- {
339
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
340
-
341
- return PromiseAsyncResult.create(async () =>
342
- {
343
- await iterator.start();
344
-
345
- if (!iterator.hasCurrent())
346
- {
347
- throw new EmptyError();
348
- }
349
-
350
- let result: T;
351
- let found: boolean = false;
352
- do
353
- {
354
- if (!condition || await condition(iterator.getCurrent()))
355
- {
356
- result = iterator.getCurrent();
357
- found = true;
358
- }
359
- }
360
- while (await iterator.next());
361
-
362
- if (!found)
363
- {
364
- if (!condition)
365
- {
366
- throw new EmptyError();
367
- }
368
- else
369
- {
370
- throw new NotFoundError("No value was found in the AsyncIterator that matched the provided condition.");
371
- }
372
- }
373
-
374
- return result!;
375
- });
376
- }
377
-
378
- /**
379
- * Find the maximum value in the provided {@link AsyncIterator}.
380
- * @param iterator The values to find the maximum of.
381
- */
382
- public static findMaximum<T extends Comparable<T>>(iterator: JavascriptAsyncIterator<T> | AsyncIterator<T>): PromiseAsyncResult<T>
383
- {
384
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterable");
385
-
386
- return PromiseAsyncResult.create(async () =>
387
- {
388
- if (isJavascriptAsyncIterator(iterator))
389
- {
390
- iterator = AsyncIterator.create(iterator as JavascriptAsyncIterator<T>);
391
- }
392
-
393
- let result: T = await iterator.first()
394
- .convertError(EmptyError, () => new EmptyError("Can't find the maximum of an empty Iterator."));
395
- while (await iterator.next())
396
- {
397
- const currentValue: T = iterator.getCurrent();
398
- if (result.lessThan(currentValue))
399
- {
400
- result = currentValue;
401
- }
402
- }
403
-
404
- return result;
405
- });
406
- }
407
-
408
- /**
409
- * Get a new {@link AsyncIterator} that wraps around this {@link AsyncIterator} and only
410
- * returns a maximum number of values from this {@link AsyncIterator}.
411
- * @param maximumToTake The maximum number of values to take from this {@link AsyncIterator}.
412
- */
413
- public take(maximumToTake: number): AsyncIterator<T>
414
- {
415
- return AsyncIterator.take(this, maximumToTake);
416
- }
417
-
418
- public static take<T>(iterator: AsyncIterator<T>, maximumToTake: number): AsyncIterator<T>
419
- {
420
- return TakeAsyncIterator.create(iterator, maximumToTake);
421
- }
422
-
423
- /**
424
- * Get a new {@link AsyncIterator} that wraps around this {@link AsyncIterator} and will skip the initial
425
- * provided number of values before beginning to return values.
426
- * @param maximumToSkip The maximum number of values to skip from this {@link AsyncIterator}.
427
- */
428
- public skip(maximumToSkip: number): AsyncIterator<T>
429
- {
430
- return AsyncIterator.skip(this, maximumToSkip);
431
- }
432
-
433
- public static skip<T>(iterator: AsyncIterator<T>, maximumToSkip: number): AsyncIterator<T>
434
- {
435
- return SkipAsyncIterator.create(iterator, maximumToSkip);
436
- }
437
- }
@@ -1,48 +0,0 @@
1
- import { AsyncIterator } from "./asyncIterator";
2
- import { JavascriptAsyncIterator, JavascriptIteratorResult } from "./javascript";
3
- import { PreCondition } from "./preCondition";
4
-
5
- /**
6
- * A type that adapts an {@link AsyncIterator} to match a {@link JavascriptAsyncIterator}.
7
- */
8
- export class AsyncIteratorToJavascriptAsyncIteratorAdapter<T> implements JavascriptAsyncIterator<T>
9
- {
10
- private readonly iterator: AsyncIterator<T>;
11
- private hasStarted: boolean;
12
-
13
- private constructor(iterator: AsyncIterator<T>)
14
- {
15
- PreCondition.assertNotUndefinedAndNotNull(iterator, "iterator");
16
-
17
- this.iterator = iterator;
18
- this.hasStarted = false;
19
- }
20
-
21
- public static create<T>(iterator: AsyncIterator<T>): AsyncIteratorToJavascriptAsyncIteratorAdapter<T>
22
- {
23
- return new AsyncIteratorToJavascriptAsyncIteratorAdapter<T>(iterator);
24
- }
25
-
26
- public async next(): Promise<JavascriptIteratorResult<T>>
27
- {
28
- if (!this.hasStarted)
29
- {
30
- this.hasStarted = true;
31
- await this.iterator.start();
32
- }
33
- else
34
- {
35
- await 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
- }
@@ -1,95 +0,0 @@
1
- import { PromiseAsyncResult } from "./promiseAsyncResult";
2
- import { isPromise, Type } from "./types";
3
-
4
- /**
5
- * A result object that adds extra behavior beyond the standard {@link Promise}.
6
- */
7
- export abstract class AsyncResult<T> implements Promise<T>
8
- {
9
- public static create<T>(action: () => (T | Promise<T>)): AsyncResult<T>;
10
- public static create<T>(promise: Promise<T>): AsyncResult<T>;
11
- static create<T>(actionOrPromise: (() => (T | Promise<T>)) | Promise<T>): AsyncResult<T>
12
- {
13
- return PromiseAsyncResult.create<T>(isPromise<T>(actionOrPromise) ? actionOrPromise : new Promise<T>(actionOrPromise));
14
- }
15
-
16
- /**
17
- * Create a new {@link AsyncResult} that contains the provided value.
18
- * @param value The value to wrap in a {@link AsyncResult}.
19
- */
20
- public static value<T>(value: T): AsyncResult<T>
21
- {
22
- return PromiseAsyncResult.value(value);
23
- }
24
-
25
- /**
26
- * Create a new {@link AsyncResult} that contains the provided error.
27
- * @param error The error to wrap in a {@link AsyncResult}.
28
- */
29
- public static error<T>(error: Error): AsyncResult<T>
30
- {
31
- return PromiseAsyncResult.error<T>(error);
32
- }
33
-
34
- public static yield(): AsyncResult<void>
35
- {
36
- return PromiseAsyncResult.yield();
37
- }
38
-
39
- /**
40
- * Get a {@link AsyncResult} that runs the provided function if this {@link AsyncResult} is successful.
41
- * @param thenFunction The function to run if this {@link AsyncResult} is successful.
42
- */
43
- public abstract then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): AsyncResult<TResult1 | TResult2>
44
-
45
- /**
46
- * Run the provided onValueFunction if this {@link AsyncResult} is successful. The value or error
47
- * contained by this {@link AsyncResult} will be contained by the returned {@link AsyncResult}.
48
- * @param onValueFunction The function to run if this {@link AsyncResult} is successful.
49
- */
50
- public abstract onValue(onValueFunction: (value: T) => (void | Promise<void>)): AsyncResult<T>
51
-
52
- /**
53
- * Run the provided catchFunction if this {@link AsyncResult} contains an error.
54
- * @param catchFunction The function to run if an error is caught.
55
- */
56
- public abstract catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): AsyncResult<T | TResult>
57
- /**
58
- * Run the provided catchFunction if this {@link AsyncResult} contains an error of the provided type.
59
- * @param errorType The type of error to catch.
60
- * @param catchFunction The function to run if the error is caught.
61
- */
62
- public abstract catch<TError,TResult = never>(errorType: Type<TError>, onrejected: (reason: TError) => (TResult | PromiseLike<TResult>)): AsyncResult<T | TResult>
63
-
64
- /**
65
- * Run the provided onErrorFunction if this {@link AsyncResult} contains an error.
66
- * @param onErrorFunction The function to run if an error is found.
67
- */
68
- public abstract onError(onErrorFunction: (reason: unknown) => (void | PromiseLike<void>)): AsyncResult<T>;
69
- /**
70
- * Run the provided onErrorFunction if this {@link AsyncResult} contains an error of the provided
71
- * type.
72
- * @param errorType The type of error to respond to.
73
- * @param onErrorFunction The function to run if the error is found.
74
- */
75
- public abstract onError<TError>(errorType: Type<TError>, onErrorFunction: (reason: TError) => (void | PromiseLike<void>)): AsyncResult<T>;
76
-
77
- /**
78
- * Run the provided convertErrorFunction if this {@link AsyncResult} contains an error. The value
79
- * returned from the convertErrorFunction will be the error for the returned {@link AsyncResult}.
80
- * @param convertErrorFunction The function that will return the new error.
81
- */
82
- public abstract convertError(convertErrorFunction: (reason: unknown) => (unknown | PromiseLike<unknown>)): AsyncResult<T>;
83
- /**
84
- * Run the provided convertErrorFunction if this {@link AsyncResult} contains an error of the
85
- * provided type. The value returned from the convertErrorFunction will be the error for the
86
- * returned {@link AsyncResult}.
87
- * @param errorType The type of error to respond to.
88
- * @param convertErrorFunction The function that will return the new error.
89
- */
90
- public abstract convertError<TError>(errorType: Type<TError>, convertErrorFunction: (reason: TError) => (unknown | PromiseLike<unknown>)): AsyncResult<T>;
91
-
92
- public abstract finally(onfinally?: (() => (void | Promise<void>)) | null): AsyncResult<T>;
93
-
94
- readonly abstract [Symbol.toStringTag]: string;
95
- }