@everyonesoftware/common 2.0.0 → 3.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/.github/workflows/publish.yml +54 -36
- package/package.json +6 -2
- package/sources/asyncIterator.ts +436 -436
- package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +47 -47
- package/sources/asyncResult.ts +95 -95
- package/sources/byteList.ts +201 -201
- package/sources/byteListStream.ts +120 -120
- package/sources/byteReadStream.ts +23 -23
- package/sources/byteWriteStream.ts +15 -15
- package/sources/characterList.ts +194 -194
- package/sources/characterListStream.ts +150 -150
- package/sources/characterReadStream.ts +80 -80
- package/sources/characterReadStreamIterator.ts +127 -127
- package/sources/concatenateIterable.ts +118 -118
- package/sources/concatenateIterator.ts +164 -164
- package/sources/currentProcess.ts +157 -157
- package/sources/dateTime.ts +129 -129
- package/sources/depthFirstSearch.ts +229 -229
- package/sources/flatMapIterable.ts +103 -103
- package/sources/flatMapIterator.ts +151 -151
- package/sources/generator.ts +250 -250
- package/sources/index.ts +1 -1
- package/sources/iterator.ts +480 -480
- package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +123 -123
- package/sources/javascriptSetSet.ts +133 -133
- package/sources/listQueue.ts +61 -61
- package/sources/listStack.ts +61 -61
- package/sources/luxonDateTime.ts +108 -108
- package/sources/mapAsyncIterator.ts +140 -140
- package/sources/mutableMap.ts +291 -291
- package/sources/node.ts +36 -36
- package/sources/promiseAsyncResult.ts +173 -173
- package/sources/queue.ts +48 -48
- package/sources/recreationDotGovClient.ts +258 -258
- package/sources/searchControl.ts +41 -41
- package/sources/set.ts +243 -243
- package/sources/skipAsyncIterator.ts +144 -144
- package/sources/stack.ts +47 -47
- package/sources/syncResult.ts +299 -299
- package/sources/takeAsyncIterator.ts +140 -140
- package/sources/whereAsyncIterator.ts +142 -142
- package/sources/wonderlandTrailClient.ts +1502 -1502
- package/tests/assertTestTests.ts +74 -74
- package/tests/byteListStreamTests.ts +389 -389
- package/tests/byteListTests.ts +26 -26
- package/tests/characterListStreamTests.ts +390 -390
- package/tests/characterListTests.ts +249 -249
- package/tests/dateTimeTests.ts +29 -29
- package/tests/depthFirstSearchTests.ts +105 -105
- package/tests/generatorTests.ts +85 -85
- package/tests/mutableMapTests.ts +153 -153
- package/tests/promiseAsyncResultTests.ts +687 -687
- package/tests/queueTests.ts +28 -28
- package/tests/recreationDotGovClientTests.ts +190 -190
- package/tests/setTests.ts +139 -139
- package/tests/stackTests.ts +65 -65
- package/tests/syncResultTests.ts +1250 -1250
- package/tests/wonderlandTrailClientTests.ts +451 -451
- package/tsup.config.ts +12 -12
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
import { AsyncIterator } from "./asyncIterator";
|
|
2
|
-
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
3
|
-
import { JavascriptAsyncIterator } from "./javascript";
|
|
4
|
-
import { PreCondition } from "./preCondition";
|
|
5
|
-
import { Type } from "./types";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* An {@link AsyncIterator} that iterates over a maximum number of values.
|
|
9
|
-
*/
|
|
10
|
-
export class TakeAsyncIterator<T> implements AsyncIterator<T>
|
|
11
|
-
{
|
|
12
|
-
private readonly innerIterator: AsyncIterator<T>;
|
|
13
|
-
private started: boolean;
|
|
14
|
-
private readonly maximumToTake: number;
|
|
15
|
-
private taken: number;
|
|
16
|
-
|
|
17
|
-
private constructor(innerIterator: AsyncIterator<T>, maximumToTake: number)
|
|
18
|
-
{
|
|
19
|
-
PreCondition.assertGreaterThanOrEqualTo(maximumToTake, 0, "maximumToTake");
|
|
20
|
-
|
|
21
|
-
this.innerIterator = innerIterator;
|
|
22
|
-
this.started = false;
|
|
23
|
-
this.maximumToTake = maximumToTake;
|
|
24
|
-
this.taken = 0;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public static create<T>(innerIterator: AsyncIterator<T>, maximumToTake: number)
|
|
28
|
-
{
|
|
29
|
-
return new TakeAsyncIterator(innerIterator, maximumToTake);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public hasCurrent(): boolean
|
|
33
|
-
{
|
|
34
|
-
return this.hasStarted() && this.taken <= this.maximumToTake && this.innerIterator.hasCurrent();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public next(): PromiseAsyncResult<boolean>
|
|
38
|
-
{
|
|
39
|
-
return PromiseAsyncResult.create(async () =>
|
|
40
|
-
{
|
|
41
|
-
if (this.maximumToTake <= 0)
|
|
42
|
-
{
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
else if (!this.hasStarted())
|
|
46
|
-
{
|
|
47
|
-
this.started = true;
|
|
48
|
-
await this.innerIterator.start();
|
|
49
|
-
this.taken++;
|
|
50
|
-
}
|
|
51
|
-
else if (this.taken <= this.maximumToTake)
|
|
52
|
-
{
|
|
53
|
-
await this.innerIterator.next();
|
|
54
|
-
this.taken++;
|
|
55
|
-
}
|
|
56
|
-
return this.hasCurrent();
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public hasStarted(): boolean
|
|
61
|
-
{
|
|
62
|
-
return this.started;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public getCurrent(): T
|
|
66
|
-
{
|
|
67
|
-
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
68
|
-
|
|
69
|
-
return this.innerIterator.getCurrent();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public start(): PromiseAsyncResult<this>
|
|
73
|
-
{
|
|
74
|
-
return AsyncIterator.start<T, this>(this);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public takeCurrent(): PromiseAsyncResult<T>
|
|
78
|
-
{
|
|
79
|
-
return AsyncIterator.takeCurrent(this);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public any(): PromiseAsyncResult<boolean>
|
|
83
|
-
{
|
|
84
|
-
return AsyncIterator.any(this);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public getCount(): PromiseAsyncResult<number>
|
|
88
|
-
{
|
|
89
|
-
return AsyncIterator.getCount(this);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
public toArray(): PromiseAsyncResult<T[]>
|
|
93
|
-
{
|
|
94
|
-
return AsyncIterator.toArray(this);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public where(condition: (value: T) => boolean): AsyncIterator<T>
|
|
98
|
-
{
|
|
99
|
-
return AsyncIterator.where(this, condition);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): AsyncIterator<U>
|
|
103
|
-
{
|
|
104
|
-
return AsyncIterator.whereInstanceOf(this, typeCheck);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
public whereInstanceOfType<U extends T>(type: Type<U>): AsyncIterator<U>
|
|
108
|
-
{
|
|
109
|
-
return AsyncIterator.whereInstanceOfType(this, type);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
public map<TOutput>(mapping: (value: T) => (TOutput | PromiseLike<TOutput>)): AsyncIterator<TOutput>
|
|
113
|
-
{
|
|
114
|
-
return AsyncIterator.map(this, mapping);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public first(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
|
|
118
|
-
{
|
|
119
|
-
return AsyncIterator.first(this, condition);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public last(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
|
|
123
|
-
{
|
|
124
|
-
return AsyncIterator.last(this, condition);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public [Symbol.asyncIterator](): JavascriptAsyncIterator<T>
|
|
128
|
-
{
|
|
129
|
-
return AsyncIterator[Symbol.asyncIterator](this);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
public take(maximumToTake: number): AsyncIterator<T>
|
|
133
|
-
{
|
|
134
|
-
return AsyncIterator.take(this, maximumToTake);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
public skip(maximumToSkip: number): AsyncIterator<T>
|
|
138
|
-
{
|
|
139
|
-
return AsyncIterator.skip(this, maximumToSkip);
|
|
140
|
-
}
|
|
1
|
+
import { AsyncIterator } from "./asyncIterator";
|
|
2
|
+
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
3
|
+
import { JavascriptAsyncIterator } from "./javascript";
|
|
4
|
+
import { PreCondition } from "./preCondition";
|
|
5
|
+
import { Type } from "./types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An {@link AsyncIterator} that iterates over a maximum number of values.
|
|
9
|
+
*/
|
|
10
|
+
export class TakeAsyncIterator<T> implements AsyncIterator<T>
|
|
11
|
+
{
|
|
12
|
+
private readonly innerIterator: AsyncIterator<T>;
|
|
13
|
+
private started: boolean;
|
|
14
|
+
private readonly maximumToTake: number;
|
|
15
|
+
private taken: number;
|
|
16
|
+
|
|
17
|
+
private constructor(innerIterator: AsyncIterator<T>, maximumToTake: number)
|
|
18
|
+
{
|
|
19
|
+
PreCondition.assertGreaterThanOrEqualTo(maximumToTake, 0, "maximumToTake");
|
|
20
|
+
|
|
21
|
+
this.innerIterator = innerIterator;
|
|
22
|
+
this.started = false;
|
|
23
|
+
this.maximumToTake = maximumToTake;
|
|
24
|
+
this.taken = 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static create<T>(innerIterator: AsyncIterator<T>, maximumToTake: number)
|
|
28
|
+
{
|
|
29
|
+
return new TakeAsyncIterator(innerIterator, maximumToTake);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public hasCurrent(): boolean
|
|
33
|
+
{
|
|
34
|
+
return this.hasStarted() && this.taken <= this.maximumToTake && this.innerIterator.hasCurrent();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public next(): PromiseAsyncResult<boolean>
|
|
38
|
+
{
|
|
39
|
+
return PromiseAsyncResult.create(async () =>
|
|
40
|
+
{
|
|
41
|
+
if (this.maximumToTake <= 0)
|
|
42
|
+
{
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
else if (!this.hasStarted())
|
|
46
|
+
{
|
|
47
|
+
this.started = true;
|
|
48
|
+
await this.innerIterator.start();
|
|
49
|
+
this.taken++;
|
|
50
|
+
}
|
|
51
|
+
else if (this.taken <= this.maximumToTake)
|
|
52
|
+
{
|
|
53
|
+
await this.innerIterator.next();
|
|
54
|
+
this.taken++;
|
|
55
|
+
}
|
|
56
|
+
return this.hasCurrent();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public hasStarted(): boolean
|
|
61
|
+
{
|
|
62
|
+
return this.started;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public getCurrent(): T
|
|
66
|
+
{
|
|
67
|
+
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
68
|
+
|
|
69
|
+
return this.innerIterator.getCurrent();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public start(): PromiseAsyncResult<this>
|
|
73
|
+
{
|
|
74
|
+
return AsyncIterator.start<T, this>(this);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public takeCurrent(): PromiseAsyncResult<T>
|
|
78
|
+
{
|
|
79
|
+
return AsyncIterator.takeCurrent(this);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public any(): PromiseAsyncResult<boolean>
|
|
83
|
+
{
|
|
84
|
+
return AsyncIterator.any(this);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public getCount(): PromiseAsyncResult<number>
|
|
88
|
+
{
|
|
89
|
+
return AsyncIterator.getCount(this);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public toArray(): PromiseAsyncResult<T[]>
|
|
93
|
+
{
|
|
94
|
+
return AsyncIterator.toArray(this);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public where(condition: (value: T) => boolean): AsyncIterator<T>
|
|
98
|
+
{
|
|
99
|
+
return AsyncIterator.where(this, condition);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): AsyncIterator<U>
|
|
103
|
+
{
|
|
104
|
+
return AsyncIterator.whereInstanceOf(this, typeCheck);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public whereInstanceOfType<U extends T>(type: Type<U>): AsyncIterator<U>
|
|
108
|
+
{
|
|
109
|
+
return AsyncIterator.whereInstanceOfType(this, type);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public map<TOutput>(mapping: (value: T) => (TOutput | PromiseLike<TOutput>)): AsyncIterator<TOutput>
|
|
113
|
+
{
|
|
114
|
+
return AsyncIterator.map(this, mapping);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public first(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
|
|
118
|
+
{
|
|
119
|
+
return AsyncIterator.first(this, condition);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public last(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
|
|
123
|
+
{
|
|
124
|
+
return AsyncIterator.last(this, condition);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public [Symbol.asyncIterator](): JavascriptAsyncIterator<T>
|
|
128
|
+
{
|
|
129
|
+
return AsyncIterator[Symbol.asyncIterator](this);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public take(maximumToTake: number): AsyncIterator<T>
|
|
133
|
+
{
|
|
134
|
+
return AsyncIterator.take(this, maximumToTake);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public skip(maximumToSkip: number): AsyncIterator<T>
|
|
138
|
+
{
|
|
139
|
+
return AsyncIterator.skip(this, maximumToSkip);
|
|
140
|
+
}
|
|
141
141
|
}
|
|
@@ -1,143 +1,143 @@
|
|
|
1
|
-
import { AsyncIterator } from "./asyncIterator";
|
|
2
|
-
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
3
|
-
import { JavascriptAsyncIterator } from "./javascript";
|
|
4
|
-
import { PreCondition } from "./preCondition";
|
|
5
|
-
import { Type } from "./types";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* An {@link Iterator} that only returns values that match a condition.
|
|
9
|
-
*/
|
|
10
|
-
export class WhereAsyncIterator<T> implements AsyncIterator<T>
|
|
11
|
-
{
|
|
12
|
-
private readonly innerIterator: AsyncIterator<T>;
|
|
13
|
-
private started: boolean;
|
|
14
|
-
private readonly condition: (value: T) => (boolean | PromiseLike<boolean>);
|
|
15
|
-
|
|
16
|
-
private constructor(innerIterator: AsyncIterator<T>, condition: (value: T) => (boolean | PromiseLike<boolean>))
|
|
17
|
-
{
|
|
18
|
-
PreCondition.assertNotUndefinedAndNotNull(innerIterator, "innerIterator");
|
|
19
|
-
PreCondition.assertNotUndefinedAndNotNull(condition, "condition");
|
|
20
|
-
|
|
21
|
-
this.innerIterator = innerIterator;
|
|
22
|
-
this.started = false;
|
|
23
|
-
this.condition = condition;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public hasStarted(): boolean
|
|
27
|
-
{
|
|
28
|
-
return this.started;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public hasCurrent(): boolean
|
|
32
|
-
{
|
|
33
|
-
return this.hasStarted() && this.innerIterator.hasCurrent();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public getCurrent(): T
|
|
37
|
-
{
|
|
38
|
-
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
39
|
-
|
|
40
|
-
return this.innerIterator.getCurrent();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public static create<T>(innerIterator: AsyncIterator<T>, condition: (value: T) => (boolean | PromiseLike<boolean>)): WhereAsyncIterator<T>
|
|
44
|
-
{
|
|
45
|
-
return new WhereAsyncIterator(innerIterator, condition);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public next(): PromiseAsyncResult<boolean>
|
|
49
|
-
{
|
|
50
|
-
return PromiseAsyncResult.create(async () =>
|
|
51
|
-
{
|
|
52
|
-
if (!this.hasStarted())
|
|
53
|
-
{
|
|
54
|
-
await this.innerIterator.start();
|
|
55
|
-
this.started = true;
|
|
56
|
-
|
|
57
|
-
while (this.hasCurrent() && !await this.condition(this.getCurrent()))
|
|
58
|
-
{
|
|
59
|
-
await this.innerIterator.next();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else
|
|
63
|
-
{
|
|
64
|
-
do
|
|
65
|
-
{
|
|
66
|
-
await this.innerIterator.next();
|
|
67
|
-
}
|
|
68
|
-
while (this.hasCurrent() && !await this.condition(this.getCurrent()));
|
|
69
|
-
}
|
|
70
|
-
return this.hasCurrent();
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
public start(): PromiseAsyncResult<this>
|
|
75
|
-
{
|
|
76
|
-
return AsyncIterator.start<T, this>(this);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public takeCurrent(): PromiseAsyncResult<T>
|
|
80
|
-
{
|
|
81
|
-
return AsyncIterator.takeCurrent(this);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public any(): PromiseAsyncResult<boolean>
|
|
85
|
-
{
|
|
86
|
-
return AsyncIterator.any(this);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public getCount(): PromiseAsyncResult<number>
|
|
90
|
-
{
|
|
91
|
-
return AsyncIterator.getCount(this);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public toArray(): PromiseAsyncResult<T[]>
|
|
95
|
-
{
|
|
96
|
-
return AsyncIterator.toArray(this);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public where(condition: (value: T) => (boolean | PromiseLike<boolean>)): AsyncIterator<T>
|
|
100
|
-
{
|
|
101
|
-
return AsyncIterator.where(this, condition);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public map<TOutput>(mapping: (value: T) => (TOutput | PromiseLike<TOutput>)): AsyncIterator<TOutput>
|
|
105
|
-
{
|
|
106
|
-
return AsyncIterator.map(this, mapping);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): AsyncIterator<U>
|
|
110
|
-
{
|
|
111
|
-
return AsyncIterator.whereInstanceOf(this, typeCheck);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
public whereInstanceOfType<U extends T>(type: Type<U>): AsyncIterator<U>
|
|
115
|
-
{
|
|
116
|
-
return AsyncIterator.whereInstanceOfType(this, type);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public first(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
|
|
120
|
-
{
|
|
121
|
-
return AsyncIterator.first(this, condition);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
public last(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
|
|
125
|
-
{
|
|
126
|
-
return AsyncIterator.last(this, condition);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
public take(maximumToTake: number): AsyncIterator<T>
|
|
130
|
-
{
|
|
131
|
-
return AsyncIterator.take(this, maximumToTake);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
public skip(maximumToSkip: number): AsyncIterator<T>
|
|
135
|
-
{
|
|
136
|
-
return AsyncIterator.skip(this, maximumToSkip);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
public [Symbol.asyncIterator](): JavascriptAsyncIterator<T>
|
|
140
|
-
{
|
|
141
|
-
return AsyncIterator[Symbol.asyncIterator](this);
|
|
142
|
-
}
|
|
1
|
+
import { AsyncIterator } from "./asyncIterator";
|
|
2
|
+
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
3
|
+
import { JavascriptAsyncIterator } from "./javascript";
|
|
4
|
+
import { PreCondition } from "./preCondition";
|
|
5
|
+
import { Type } from "./types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An {@link Iterator} that only returns values that match a condition.
|
|
9
|
+
*/
|
|
10
|
+
export class WhereAsyncIterator<T> implements AsyncIterator<T>
|
|
11
|
+
{
|
|
12
|
+
private readonly innerIterator: AsyncIterator<T>;
|
|
13
|
+
private started: boolean;
|
|
14
|
+
private readonly condition: (value: T) => (boolean | PromiseLike<boolean>);
|
|
15
|
+
|
|
16
|
+
private constructor(innerIterator: AsyncIterator<T>, condition: (value: T) => (boolean | PromiseLike<boolean>))
|
|
17
|
+
{
|
|
18
|
+
PreCondition.assertNotUndefinedAndNotNull(innerIterator, "innerIterator");
|
|
19
|
+
PreCondition.assertNotUndefinedAndNotNull(condition, "condition");
|
|
20
|
+
|
|
21
|
+
this.innerIterator = innerIterator;
|
|
22
|
+
this.started = false;
|
|
23
|
+
this.condition = condition;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public hasStarted(): boolean
|
|
27
|
+
{
|
|
28
|
+
return this.started;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public hasCurrent(): boolean
|
|
32
|
+
{
|
|
33
|
+
return this.hasStarted() && this.innerIterator.hasCurrent();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public getCurrent(): T
|
|
37
|
+
{
|
|
38
|
+
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
39
|
+
|
|
40
|
+
return this.innerIterator.getCurrent();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public static create<T>(innerIterator: AsyncIterator<T>, condition: (value: T) => (boolean | PromiseLike<boolean>)): WhereAsyncIterator<T>
|
|
44
|
+
{
|
|
45
|
+
return new WhereAsyncIterator(innerIterator, condition);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public next(): PromiseAsyncResult<boolean>
|
|
49
|
+
{
|
|
50
|
+
return PromiseAsyncResult.create(async () =>
|
|
51
|
+
{
|
|
52
|
+
if (!this.hasStarted())
|
|
53
|
+
{
|
|
54
|
+
await this.innerIterator.start();
|
|
55
|
+
this.started = true;
|
|
56
|
+
|
|
57
|
+
while (this.hasCurrent() && !await this.condition(this.getCurrent()))
|
|
58
|
+
{
|
|
59
|
+
await this.innerIterator.next();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else
|
|
63
|
+
{
|
|
64
|
+
do
|
|
65
|
+
{
|
|
66
|
+
await this.innerIterator.next();
|
|
67
|
+
}
|
|
68
|
+
while (this.hasCurrent() && !await this.condition(this.getCurrent()));
|
|
69
|
+
}
|
|
70
|
+
return this.hasCurrent();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public start(): PromiseAsyncResult<this>
|
|
75
|
+
{
|
|
76
|
+
return AsyncIterator.start<T, this>(this);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public takeCurrent(): PromiseAsyncResult<T>
|
|
80
|
+
{
|
|
81
|
+
return AsyncIterator.takeCurrent(this);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public any(): PromiseAsyncResult<boolean>
|
|
85
|
+
{
|
|
86
|
+
return AsyncIterator.any(this);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public getCount(): PromiseAsyncResult<number>
|
|
90
|
+
{
|
|
91
|
+
return AsyncIterator.getCount(this);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public toArray(): PromiseAsyncResult<T[]>
|
|
95
|
+
{
|
|
96
|
+
return AsyncIterator.toArray(this);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public where(condition: (value: T) => (boolean | PromiseLike<boolean>)): AsyncIterator<T>
|
|
100
|
+
{
|
|
101
|
+
return AsyncIterator.where(this, condition);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public map<TOutput>(mapping: (value: T) => (TOutput | PromiseLike<TOutput>)): AsyncIterator<TOutput>
|
|
105
|
+
{
|
|
106
|
+
return AsyncIterator.map(this, mapping);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): AsyncIterator<U>
|
|
110
|
+
{
|
|
111
|
+
return AsyncIterator.whereInstanceOf(this, typeCheck);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public whereInstanceOfType<U extends T>(type: Type<U>): AsyncIterator<U>
|
|
115
|
+
{
|
|
116
|
+
return AsyncIterator.whereInstanceOfType(this, type);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public first(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
|
|
120
|
+
{
|
|
121
|
+
return AsyncIterator.first(this, condition);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public last(condition?: (value: T) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<T>
|
|
125
|
+
{
|
|
126
|
+
return AsyncIterator.last(this, condition);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public take(maximumToTake: number): AsyncIterator<T>
|
|
130
|
+
{
|
|
131
|
+
return AsyncIterator.take(this, maximumToTake);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public skip(maximumToSkip: number): AsyncIterator<T>
|
|
135
|
+
{
|
|
136
|
+
return AsyncIterator.skip(this, maximumToSkip);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public [Symbol.asyncIterator](): JavascriptAsyncIterator<T>
|
|
140
|
+
{
|
|
141
|
+
return AsyncIterator[Symbol.asyncIterator](this);
|
|
142
|
+
}
|
|
143
143
|
}
|