@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
package/.c8rc.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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
|
|
37
|
+
env:
|
|
38
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/.mocharc.json
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Everything
|
|
2
|
+
|
|
3
|
+
## Concept
|
|
4
|
+
|
|
5
|
+
The **everything** folder contains all of my TypeScript source code. The intention is that all source code for a specific language could be in the same folder and that different applications would just pull in the source files that they need. This would eliminate the need for libraries, which should make bundling easier and more efficient. It should also improve build and test times since making changes (changing source code, building, and then testing) would immediately appear in other projects.
|
|
6
|
+
|
|
7
|
+
## Challenges
|
|
8
|
+
|
|
9
|
+
The biggest challenge I can think of is that the current TypeScript tooling doesn't support this approach. All of the existing tooling (such as npm, yarn, pnpm) only support single-folder packages. Some support mono-repo packages, but what I'm describing isn't a mono-repo. Technically, yes, my idea is that multiple projects would exist in the same folder, but mono-repos have a root directory where common configuration goes and then sub-folders for each different project. My approach is different from this in that I want a single source code folder. Project configuration files would go into the root of the repository. Source code files would be compiled and tested whenever they're changed. Finally project outputs (such as executable files or bundled websites) could either automatically be packaged or they could be packaged on demand later.
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@everyonesoftware/common",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A package containing common code for everyonesoftware projects.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./outputs/sources.d.ts",
|
|
9
|
+
"import": "./outputs/sources.js",
|
|
10
|
+
"require": "./outputs/sources.cjs"
|
|
11
|
+
},
|
|
12
|
+
"./tests": {
|
|
13
|
+
"types": "./outputs/tests.d.ts",
|
|
14
|
+
"import": "./outputs/tests.js",
|
|
15
|
+
"require": "./outputs/tests.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "barrelsby --directory sources --delete && tsup",
|
|
20
|
+
"clean": "rimraf coverage outputs",
|
|
21
|
+
"test": "npm run build && c8 node -r source-map-support/register outputs/tests.js"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"luxon": "3.7.2"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "24.8.1",
|
|
28
|
+
"@types/luxon": "3.7.1",
|
|
29
|
+
"barrelsby": "2.8.1",
|
|
30
|
+
"c8": "^11.0.0",
|
|
31
|
+
"rimraf": "6.0.1",
|
|
32
|
+
"source-map-support": "0.5.21",
|
|
33
|
+
"tsup": "8.5.1",
|
|
34
|
+
"typescript": "6.0.3"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,437 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
}
|