@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,230 @@
|
|
|
1
|
+
import { JavascriptIterable, JavascriptIterator } from "./javascript";
|
|
2
|
+
import { List } from "./list";
|
|
3
|
+
import { ListStack } from "./listStack";
|
|
4
|
+
import { PreCondition } from "./preCondition";
|
|
5
|
+
import { SearchControl } from "./searchControl";
|
|
6
|
+
import { Stack } from "./stack";
|
|
7
|
+
import { Set } from "./set";
|
|
8
|
+
import { Iterator } from "./iterator";
|
|
9
|
+
import { SyncResult } from "./syncResult";
|
|
10
|
+
import { isJavascriptIterable, Type } from "./types";
|
|
11
|
+
|
|
12
|
+
class SearchBreakError extends Error
|
|
13
|
+
{
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class DepthFirstSearch<TVisit,TResult> implements SearchControl<TVisit,TResult>, Iterator<TResult>
|
|
17
|
+
{
|
|
18
|
+
private readonly searchAction: (searchControl: SearchControl<TVisit,TResult>, visiting: TVisit) => void;
|
|
19
|
+
private readonly toVisit: ListStack<TVisit>;
|
|
20
|
+
private readonly visited: Set<TVisit>;
|
|
21
|
+
private readonly results: List<TResult>;
|
|
22
|
+
private started: boolean;
|
|
23
|
+
private done: boolean;
|
|
24
|
+
|
|
25
|
+
private constructor(initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, visiting: TVisit) => void)
|
|
26
|
+
{
|
|
27
|
+
PreCondition.assertNotEmpty(initialToVisit, "initialToVisit");
|
|
28
|
+
PreCondition.assertNotUndefinedAndNotNull(searchAction, "searchAction");
|
|
29
|
+
|
|
30
|
+
this.searchAction = searchAction;
|
|
31
|
+
this.toVisit = Stack.create();
|
|
32
|
+
this.toVisit.addAll(initialToVisit).await();
|
|
33
|
+
this.visited = Set.create();
|
|
34
|
+
this.results = List.create();
|
|
35
|
+
this.started = false;
|
|
36
|
+
this.done = false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public static create<TVisit,TResult>(initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, visiting: TVisit) => void): DepthFirstSearch<TVisit,TResult>
|
|
40
|
+
{
|
|
41
|
+
return new DepthFirstSearch(initialToVisit, searchAction);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public addToVisit(toVisit: TVisit): void
|
|
45
|
+
{
|
|
46
|
+
if (!this.hasVisited(toVisit))
|
|
47
|
+
{
|
|
48
|
+
this.toVisit.add(toVisit);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public addAllToVisit(values: JavascriptIterable<TVisit>): void
|
|
53
|
+
{
|
|
54
|
+
for (const value of values)
|
|
55
|
+
{
|
|
56
|
+
this.addToVisit(value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public hasVisited(toVisit: TVisit): boolean
|
|
61
|
+
{
|
|
62
|
+
return this.visited.contains(toVisit).await();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public addResult(result: TResult): void
|
|
66
|
+
{
|
|
67
|
+
this.results.add(result);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public addResults(results: JavascriptIterable<TResult>): void
|
|
71
|
+
{
|
|
72
|
+
this.results.addAll(results);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public break(): never
|
|
76
|
+
{
|
|
77
|
+
throw new SearchBreakError();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public next(): SyncResult<boolean>
|
|
81
|
+
{
|
|
82
|
+
return SyncResult.create(() =>
|
|
83
|
+
{
|
|
84
|
+
let result: boolean = false;
|
|
85
|
+
if (!this.done)
|
|
86
|
+
{
|
|
87
|
+
if (!this.started)
|
|
88
|
+
{
|
|
89
|
+
this.started = true;
|
|
90
|
+
}
|
|
91
|
+
else
|
|
92
|
+
{
|
|
93
|
+
this.results.removeFirst().await();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
while (!this.hasCurrent() && this.toVisit.any().await())
|
|
97
|
+
{
|
|
98
|
+
const current: TVisit = this.toVisit.remove().await();
|
|
99
|
+
this.visited.add(current);
|
|
100
|
+
this.searchAction(this, current);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
result = this.hasCurrent();
|
|
104
|
+
this.done = !result;
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public hasStarted(): boolean
|
|
111
|
+
{
|
|
112
|
+
return this.started;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public hasCurrent(): boolean
|
|
116
|
+
{
|
|
117
|
+
return this.results.any().await();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public getCurrent(): TResult
|
|
121
|
+
{
|
|
122
|
+
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
123
|
+
|
|
124
|
+
return this.results.first().await();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public start(): SyncResult<this>
|
|
128
|
+
{
|
|
129
|
+
return Iterator.start<TResult,this>(this);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public takeCurrent(): SyncResult<TResult>
|
|
133
|
+
{
|
|
134
|
+
return Iterator.takeCurrent(this);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public any(): SyncResult<boolean>
|
|
138
|
+
{
|
|
139
|
+
return Iterator.any(this);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public getCount(): SyncResult<number>
|
|
143
|
+
{
|
|
144
|
+
return Iterator.getCount(this);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public toArray(): SyncResult<TResult[]>
|
|
148
|
+
{
|
|
149
|
+
return Iterator.toArray(this);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public concatenate(...toConcatenate: JavascriptIterable<TResult>[]): Iterator<TResult>
|
|
153
|
+
{
|
|
154
|
+
return Iterator.concatenate(this, ...toConcatenate);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
public where(condition: (value: TResult) => (boolean | SyncResult<boolean>)): Iterator<TResult>
|
|
158
|
+
{
|
|
159
|
+
return Iterator.where(this, condition);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public map<TOutput>(mapping: (value: TResult) => TOutput | SyncResult<TOutput>): Iterator<TOutput>
|
|
163
|
+
{
|
|
164
|
+
return Iterator.map(this, mapping);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public flatMap<TOutput>(mapping: (value: TResult) => JavascriptIterable<TOutput>): Iterator<TOutput>
|
|
168
|
+
{
|
|
169
|
+
return Iterator.flatMap(this, mapping);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public whereInstanceOf<U extends TResult>(typeCheck: (value: TResult) => value is U): Iterator<U>
|
|
173
|
+
{
|
|
174
|
+
return Iterator.whereInstanceOf(this, typeCheck);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public whereInstanceOfType<U extends TResult>(type: Type<U>): Iterator<U>
|
|
178
|
+
{
|
|
179
|
+
return Iterator.whereInstanceOfType(this, type);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public first(condition?: ((value: TResult) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<TResult>
|
|
183
|
+
{
|
|
184
|
+
return Iterator.first(this, condition);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public last(condition?: ((value: TResult) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<TResult>
|
|
188
|
+
{
|
|
189
|
+
return Iterator.last(this, condition);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public take(maximumToTake: number): Iterator<TResult>
|
|
193
|
+
{
|
|
194
|
+
return Iterator.take(this, maximumToTake);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
public skip(maximumToSkip: number): Iterator<TResult>
|
|
198
|
+
{
|
|
199
|
+
return Iterator.skip(this, maximumToSkip);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public [Symbol.iterator](): JavascriptIterator<TResult>
|
|
203
|
+
{
|
|
204
|
+
return Iterator[Symbol.iterator](this);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function depthFirstSearch<TVisit,TResult>(initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, current: TVisit) => void): Iterator<TResult>;
|
|
209
|
+
export function depthFirstSearch<TVisit,TResult>(parameters: { initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, current: TVisit) => void }): Iterator<TResult>;
|
|
210
|
+
export function depthFirstSearch<TVisit,TResult>(parametersOrInitialToVisit: JavascriptIterable<TVisit> | { initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, current: TVisit) => void }, searchAction?: (searchControl: SearchControl<TVisit,TResult>, current: TVisit) => void): Iterator<TResult>
|
|
211
|
+
{
|
|
212
|
+
let initialToVisit: JavascriptIterable<TVisit>;
|
|
213
|
+
if (isJavascriptIterable(parametersOrInitialToVisit))
|
|
214
|
+
{
|
|
215
|
+
initialToVisit = parametersOrInitialToVisit;
|
|
216
|
+
searchAction = searchAction!;
|
|
217
|
+
}
|
|
218
|
+
else
|
|
219
|
+
{
|
|
220
|
+
PreCondition.assertNotUndefinedAndNotNull(parametersOrInitialToVisit, "parameters");
|
|
221
|
+
|
|
222
|
+
initialToVisit = parametersOrInitialToVisit.initialToVisit;
|
|
223
|
+
searchAction = parametersOrInitialToVisit.searchAction;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
PreCondition.assertNotUndefinedAndNotNull(initialToVisit, "initialToVisit");
|
|
227
|
+
PreCondition.assertNotUndefinedAndNotNull(searchAction, "searchAction");
|
|
228
|
+
|
|
229
|
+
return DepthFirstSearch.create(initialToVisit, searchAction);
|
|
230
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SyncDisposable } from "./basicDisposable";
|
|
2
|
+
import { AsyncResult } from "./asyncResult";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An object that can be disposed.
|
|
6
|
+
*/
|
|
7
|
+
export abstract class Disposable
|
|
8
|
+
{
|
|
9
|
+
/**
|
|
10
|
+
* Create a new {@link Disposable} that will invoke the provided {@link Function} when it is
|
|
11
|
+
* disposed.
|
|
12
|
+
* @param disposedFunction The function to invoke when the returned {@link Disposable} is
|
|
13
|
+
* disposed.
|
|
14
|
+
*/
|
|
15
|
+
public static create(disposedFunction: () => void): SyncDisposable
|
|
16
|
+
{
|
|
17
|
+
return SyncDisposable.create(disposedFunction);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Synchronously clean up any resources that this object is using. This function will return
|
|
22
|
+
* true if this invocation disposed of the object. Subsequent calls to dispose() will return
|
|
23
|
+
* false.
|
|
24
|
+
*/
|
|
25
|
+
public abstract dispose(): AsyncResult<boolean>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Get whether this {@link Disposable} has been disposed yet.
|
|
29
|
+
*/
|
|
30
|
+
public abstract isDisposed(): boolean;
|
|
31
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Iterator } from "./iterator";
|
|
2
|
+
import { JavascriptIterable } from "./javascript";
|
|
3
|
+
import { PreCondition } from "./preCondition";
|
|
4
|
+
|
|
5
|
+
export function andList(values: JavascriptIterable<string>): string
|
|
6
|
+
{
|
|
7
|
+
return list("and", values);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function orList(values: JavascriptIterable<string>): string
|
|
11
|
+
{
|
|
12
|
+
return list("or", values);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function list(conjunction: string, values: JavascriptIterable<string>): string
|
|
16
|
+
{
|
|
17
|
+
PreCondition.assertNotEmpty(conjunction, "conjunction");
|
|
18
|
+
PreCondition.assertNotUndefinedAndNotNull(values, "values");
|
|
19
|
+
|
|
20
|
+
let result: string = "";
|
|
21
|
+
let index = 0;
|
|
22
|
+
const iterator: Iterator<string> = Iterator.create(values).start().await();
|
|
23
|
+
while (iterator.hasCurrent())
|
|
24
|
+
{
|
|
25
|
+
const currentValue: string = iterator.takeCurrent().await();
|
|
26
|
+
if (index >= 1)
|
|
27
|
+
{
|
|
28
|
+
if (iterator.hasCurrent())
|
|
29
|
+
{
|
|
30
|
+
result += `, `;
|
|
31
|
+
}
|
|
32
|
+
else
|
|
33
|
+
{
|
|
34
|
+
if (index >= 2)
|
|
35
|
+
{
|
|
36
|
+
result += `,`;
|
|
37
|
+
}
|
|
38
|
+
result += ` ${conjunction} `;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
result += currentValue;
|
|
42
|
+
index++;
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Comparer } from "./comparer";
|
|
2
|
+
import { Iterable } from "./iterable";
|
|
3
|
+
import { isMap, Map } from "./map";
|
|
4
|
+
import { PreCondition } from "./preCondition";
|
|
5
|
+
import { SyncResult } from "./syncResult";
|
|
6
|
+
import { getPropertyNames, hasProperty, isBoolean, isJavascriptIterable, isObject, isString } from "./types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A collection of {@link Function}s that can be used to determine if two values are equal.
|
|
10
|
+
*/
|
|
11
|
+
export class EqualFunctions
|
|
12
|
+
{
|
|
13
|
+
private readonly equalFunctions: ((left: unknown, right: unknown) => (boolean | SyncResult<boolean> | undefined))[];
|
|
14
|
+
|
|
15
|
+
private constructor()
|
|
16
|
+
{
|
|
17
|
+
this.equalFunctions = [];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static create(): EqualFunctions
|
|
21
|
+
{
|
|
22
|
+
return new EqualFunctions();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private defaultEqualFunction(left: unknown, right: unknown): SyncResult<boolean>
|
|
26
|
+
{
|
|
27
|
+
return SyncResult.create(() =>
|
|
28
|
+
{
|
|
29
|
+
let result: boolean | undefined = Comparer.equalSameUndefinedNull(left, right);
|
|
30
|
+
if (result === undefined)
|
|
31
|
+
{
|
|
32
|
+
result = false;
|
|
33
|
+
|
|
34
|
+
if (isMap(left))
|
|
35
|
+
{
|
|
36
|
+
if (isMap(right))
|
|
37
|
+
{
|
|
38
|
+
result = Map.equals(left, right, this).await();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else if (isJavascriptIterable(left) && !isString(left))
|
|
42
|
+
{
|
|
43
|
+
if (isJavascriptIterable(right) && !isString(right) && !isMap(right))
|
|
44
|
+
{
|
|
45
|
+
result = Iterable.equals(left, right, this).await();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else if (isObject(left))
|
|
49
|
+
{
|
|
50
|
+
if (isObject(right))
|
|
51
|
+
{
|
|
52
|
+
result = true;
|
|
53
|
+
|
|
54
|
+
for (const leftPropertyName of getPropertyNames(left))
|
|
55
|
+
{
|
|
56
|
+
if (!hasProperty(left, leftPropertyName) || !hasProperty(right, leftPropertyName))
|
|
57
|
+
{
|
|
58
|
+
result = false;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
else
|
|
62
|
+
{
|
|
63
|
+
result = this.areEqual(left[leftPropertyName], right[leftPropertyName]).await();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (result)
|
|
68
|
+
{
|
|
69
|
+
for (const rightPropertyName of getPropertyNames(right))
|
|
70
|
+
{
|
|
71
|
+
if (!hasProperty(left, rightPropertyName) || !hasProperty(right, rightPropertyName))
|
|
72
|
+
{
|
|
73
|
+
result = false;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
else
|
|
77
|
+
{
|
|
78
|
+
result = this.areEqual(left[rightPropertyName], right[rightPropertyName]).await();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get whether the provided values are equal based on the registered equal {@link Function}s.
|
|
91
|
+
* @param left The left value in the comparison.
|
|
92
|
+
* @param right The right value in the comparison.
|
|
93
|
+
*/
|
|
94
|
+
public areEqual(left: unknown, right: unknown): SyncResult<boolean>
|
|
95
|
+
{
|
|
96
|
+
return SyncResult.create(() =>
|
|
97
|
+
{
|
|
98
|
+
let result: boolean | SyncResult<boolean> | undefined;
|
|
99
|
+
for (const equalFunction of this.equalFunctions)
|
|
100
|
+
{
|
|
101
|
+
result = equalFunction(left, right);
|
|
102
|
+
if (result !== undefined)
|
|
103
|
+
{
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (result === undefined)
|
|
108
|
+
{
|
|
109
|
+
result = this.defaultEqualFunction(left, right);
|
|
110
|
+
}
|
|
111
|
+
return isBoolean(result) ? result : result.await();
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public add(equalFunction: (left: unknown, right: unknown) => (boolean | undefined)): this
|
|
116
|
+
{
|
|
117
|
+
PreCondition.assertNotUndefinedAndNotNull(equalFunction, "equalFunction");
|
|
118
|
+
|
|
119
|
+
this.equalFunctions.unshift(equalFunction);
|
|
120
|
+
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { FetchHttpIncomingResponse } from "./fetchHttpResponse";
|
|
2
|
+
import { HttpClient } from "./httpClient";
|
|
3
|
+
import { HttpOutgoingRequest } from "./httpOutgoingRequest";
|
|
4
|
+
import { HttpHeader } from "./httpHeader";
|
|
5
|
+
import { HttpMethod } from "./httpMethod";
|
|
6
|
+
import { PostCondition } from "./postCondition";
|
|
7
|
+
import { PreCondition } from "./preCondition";
|
|
8
|
+
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A {@link HttpClient} that uses {@link fetch}() to make network requests.
|
|
12
|
+
*/
|
|
13
|
+
export class FetchHttpClient implements HttpClient
|
|
14
|
+
{
|
|
15
|
+
protected constructor()
|
|
16
|
+
{
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static create(): FetchHttpClient
|
|
20
|
+
{
|
|
21
|
+
return new FetchHttpClient();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public sendRequest(request: HttpOutgoingRequest): PromiseAsyncResult<FetchHttpIncomingResponse>
|
|
25
|
+
{
|
|
26
|
+
PreCondition.assertNotUndefinedAndNotNull(request, "request");
|
|
27
|
+
|
|
28
|
+
return PromiseAsyncResult.create(async () =>
|
|
29
|
+
{
|
|
30
|
+
const requestInit: RequestInit = {
|
|
31
|
+
method: FetchHttpClient.convertMethod(request.getMethod()),
|
|
32
|
+
headers: request.getHeaders()
|
|
33
|
+
.map<[string, string]>((header: HttpHeader) => [header.getName(), header.getValue()])
|
|
34
|
+
.toArray()
|
|
35
|
+
.await(),
|
|
36
|
+
body: request.getBody() || undefined,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const fetchResponse: Response = await fetch(request.getURL(), requestInit);
|
|
40
|
+
return FetchHttpIncomingResponse.create(fetchResponse);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public sendGetRequest(url: string): PromiseAsyncResult<FetchHttpIncomingResponse>
|
|
45
|
+
{
|
|
46
|
+
return this.sendRequest(HttpOutgoingRequest.create(HttpMethod.GET, url));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public static convertMethod(method: HttpMethod): string
|
|
50
|
+
{
|
|
51
|
+
PreCondition.assertNotUndefinedAndNotNull(method, "method");
|
|
52
|
+
|
|
53
|
+
let result: string;
|
|
54
|
+
switch (method)
|
|
55
|
+
{
|
|
56
|
+
case HttpMethod.CONNECT:
|
|
57
|
+
result = "CONNECT";
|
|
58
|
+
break;
|
|
59
|
+
case HttpMethod.DELETE:
|
|
60
|
+
result = "DELETE";
|
|
61
|
+
break;
|
|
62
|
+
case HttpMethod.GET:
|
|
63
|
+
result = "GET";
|
|
64
|
+
break;
|
|
65
|
+
case HttpMethod.HEAD:
|
|
66
|
+
result = "HEAD";
|
|
67
|
+
break;
|
|
68
|
+
case HttpMethod.OPTIONS:
|
|
69
|
+
result = "OPTIONS";
|
|
70
|
+
break;
|
|
71
|
+
case HttpMethod.PATCH:
|
|
72
|
+
result = "PATCH";
|
|
73
|
+
break;
|
|
74
|
+
case HttpMethod.POST:
|
|
75
|
+
result = "POST";
|
|
76
|
+
break;
|
|
77
|
+
case HttpMethod.PUT:
|
|
78
|
+
result = "PUT";
|
|
79
|
+
break;
|
|
80
|
+
case HttpMethod.TRACE:
|
|
81
|
+
result = "TRACE";
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
PostCondition.assertNotEmpty(result, "result");
|
|
86
|
+
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
2
|
+
import { HttpHeader } from "./httpHeader";
|
|
3
|
+
import { HttpHeaders } from "./httpHeaders";
|
|
4
|
+
import { HttpIncomingResponse } from "./httpIncomingResponse";
|
|
5
|
+
import { MutableHttpHeaders } from "./mutableHttpHeaders";
|
|
6
|
+
import { NotFoundError } from "./notFoundError";
|
|
7
|
+
import { PreCondition } from "./preCondition";
|
|
8
|
+
import { escapeAndQuote } from "./strings";
|
|
9
|
+
import { SyncResult } from "./syncResult";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* An {@link HttpIncomingResponse} that comes from a {@link FetchHttpClient}.
|
|
13
|
+
*/
|
|
14
|
+
export class FetchHttpIncomingResponse extends HttpIncomingResponse
|
|
15
|
+
{
|
|
16
|
+
private readonly response: Response;
|
|
17
|
+
|
|
18
|
+
private constructor(response: Response)
|
|
19
|
+
{
|
|
20
|
+
PreCondition.assertNotUndefinedAndNotNull(response, "response");
|
|
21
|
+
|
|
22
|
+
super();
|
|
23
|
+
|
|
24
|
+
this.response = response;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static create(response: Response): FetchHttpIncomingResponse
|
|
28
|
+
{
|
|
29
|
+
return new FetchHttpIncomingResponse(response);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public getStatusCode(): number
|
|
33
|
+
{
|
|
34
|
+
return this.response.status;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public getHeaders(): SyncResult<HttpHeaders>
|
|
38
|
+
{
|
|
39
|
+
return SyncResult.create(() =>
|
|
40
|
+
{
|
|
41
|
+
const result: MutableHttpHeaders = HttpHeaders.create();
|
|
42
|
+
for (const header of this.response.headers)
|
|
43
|
+
{
|
|
44
|
+
result.set(header[0], header[1]);
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public getHeader(headerName: string): SyncResult<HttpHeader>
|
|
51
|
+
{
|
|
52
|
+
PreCondition.assertNotEmpty(headerName, "headerName");
|
|
53
|
+
|
|
54
|
+
return SyncResult.create(() =>
|
|
55
|
+
{
|
|
56
|
+
let result: HttpHeader | undefined;
|
|
57
|
+
|
|
58
|
+
const lowerHeaderName: string = headerName.toLowerCase();
|
|
59
|
+
for (const header of this.response.headers)
|
|
60
|
+
{
|
|
61
|
+
if (lowerHeaderName === header[0].toLowerCase())
|
|
62
|
+
{
|
|
63
|
+
result = HttpHeader.create(header[0], header[1]);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (result === undefined)
|
|
68
|
+
{
|
|
69
|
+
throw new NotFoundError(`Could not find a header with the name ${escapeAndQuote(headerName)}.`)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public getHeaderValue(headerName: string): SyncResult<string>
|
|
77
|
+
{
|
|
78
|
+
PreCondition.assertNotEmpty(headerName, "headerName");
|
|
79
|
+
|
|
80
|
+
return SyncResult.create(() =>
|
|
81
|
+
{
|
|
82
|
+
let result: string | undefined;
|
|
83
|
+
|
|
84
|
+
const lowerHeaderName: string = headerName.toLowerCase();
|
|
85
|
+
for (const header of this.response.headers)
|
|
86
|
+
{
|
|
87
|
+
if (lowerHeaderName === header[0].toLowerCase())
|
|
88
|
+
{
|
|
89
|
+
result = header[1];
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (result === undefined)
|
|
94
|
+
{
|
|
95
|
+
throw new NotFoundError(`Could not find a header with the name ${escapeAndQuote(headerName)}.`)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return result;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public getBody(): PromiseAsyncResult<string>
|
|
103
|
+
{
|
|
104
|
+
return PromiseAsyncResult.create(this.response.text());
|
|
105
|
+
}
|
|
106
|
+
}
|