@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
package/sources/listStack.ts
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import { EmptyError } from "./emptyError";
|
|
2
|
-
import { EqualFunctions } from "./equalFunctions";
|
|
3
|
-
import { JavascriptIterable } from "./javascript";
|
|
4
|
-
import { List } from "./list";
|
|
5
|
-
import { PreCondition } from "./preCondition";
|
|
6
|
-
import { Stack } from "./stack";
|
|
7
|
-
import { SyncResult } from "./syncResult";
|
|
8
|
-
|
|
9
|
-
export class ListStack<T> implements Stack<T>
|
|
10
|
-
{
|
|
11
|
-
private readonly list: List<T>;
|
|
12
|
-
|
|
13
|
-
private constructor(list?: List<T>)
|
|
14
|
-
{
|
|
15
|
-
this.list = list ?? List.create();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public static create<T>(list?: List<T>): ListStack<T>
|
|
19
|
-
{
|
|
20
|
-
return new ListStack<T>(list);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public any(): SyncResult<boolean>
|
|
24
|
-
{
|
|
25
|
-
return this.list.any();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public add(value: T): SyncResult<void>
|
|
29
|
-
{
|
|
30
|
-
return SyncResult.create(() =>
|
|
31
|
-
{
|
|
32
|
-
this.list.add(value);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public addAll(values: JavascriptIterable<T>): SyncResult<void>
|
|
37
|
-
{
|
|
38
|
-
PreCondition.assertNotUndefinedAndNotNull(values, "values");
|
|
39
|
-
|
|
40
|
-
return SyncResult.create(() =>
|
|
41
|
-
{
|
|
42
|
-
this.list.addAll(values);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public remove(): SyncResult<T>
|
|
47
|
-
{
|
|
48
|
-
return SyncResult.create(() =>
|
|
49
|
-
{
|
|
50
|
-
if (!this.any().await())
|
|
51
|
-
{
|
|
52
|
-
throw new EmptyError();
|
|
53
|
-
}
|
|
54
|
-
return this.list.removeLast().await();
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public contains(value: T, equalFunctions?: EqualFunctions): SyncResult<boolean>
|
|
59
|
-
{
|
|
60
|
-
return this.list.contains(value, equalFunctions);
|
|
61
|
-
}
|
|
1
|
+
import { EmptyError } from "./emptyError";
|
|
2
|
+
import { EqualFunctions } from "./equalFunctions";
|
|
3
|
+
import { JavascriptIterable } from "./javascript";
|
|
4
|
+
import { List } from "./list";
|
|
5
|
+
import { PreCondition } from "./preCondition";
|
|
6
|
+
import { Stack } from "./stack";
|
|
7
|
+
import { SyncResult } from "./syncResult";
|
|
8
|
+
|
|
9
|
+
export class ListStack<T> implements Stack<T>
|
|
10
|
+
{
|
|
11
|
+
private readonly list: List<T>;
|
|
12
|
+
|
|
13
|
+
private constructor(list?: List<T>)
|
|
14
|
+
{
|
|
15
|
+
this.list = list ?? List.create();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public static create<T>(list?: List<T>): ListStack<T>
|
|
19
|
+
{
|
|
20
|
+
return new ListStack<T>(list);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public any(): SyncResult<boolean>
|
|
24
|
+
{
|
|
25
|
+
return this.list.any();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public add(value: T): SyncResult<void>
|
|
29
|
+
{
|
|
30
|
+
return SyncResult.create(() =>
|
|
31
|
+
{
|
|
32
|
+
this.list.add(value);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public addAll(values: JavascriptIterable<T>): SyncResult<void>
|
|
37
|
+
{
|
|
38
|
+
PreCondition.assertNotUndefinedAndNotNull(values, "values");
|
|
39
|
+
|
|
40
|
+
return SyncResult.create(() =>
|
|
41
|
+
{
|
|
42
|
+
this.list.addAll(values);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public remove(): SyncResult<T>
|
|
47
|
+
{
|
|
48
|
+
return SyncResult.create(() =>
|
|
49
|
+
{
|
|
50
|
+
if (!this.any().await())
|
|
51
|
+
{
|
|
52
|
+
throw new EmptyError();
|
|
53
|
+
}
|
|
54
|
+
return this.list.removeLast().await();
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public contains(value: T, equalFunctions?: EqualFunctions): SyncResult<boolean>
|
|
59
|
+
{
|
|
60
|
+
return this.list.contains(value, equalFunctions);
|
|
61
|
+
}
|
|
62
62
|
}
|
package/sources/luxonDateTime.ts
CHANGED
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
import * as luxon from "luxon";
|
|
2
|
-
import { DateTime } from "./dateTime";
|
|
3
|
-
|
|
4
|
-
const pctTimeZone: string = "America/Los_Angeles";
|
|
5
|
-
|
|
6
|
-
export class LuxonDateTime implements DateTime
|
|
7
|
-
{
|
|
8
|
-
private readonly dateTime: luxon.DateTime;
|
|
9
|
-
|
|
10
|
-
private constructor(dateTime: luxon.DateTime)
|
|
11
|
-
{
|
|
12
|
-
this.dateTime = dateTime;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
private static create(dateTime: luxon.DateTime): LuxonDateTime
|
|
16
|
-
{
|
|
17
|
-
return new LuxonDateTime(dateTime);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
public static parse(text: string): LuxonDateTime
|
|
21
|
-
{
|
|
22
|
-
return LuxonDateTime.create(luxon.DateTime.fromISO(text, { zone: pctTimeZone }));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public static now(): LuxonDateTime
|
|
26
|
-
{
|
|
27
|
-
return LuxonDateTime.create(luxon.DateTime.now().setZone(pctTimeZone));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public getYear(): number
|
|
31
|
-
{
|
|
32
|
-
return this.dateTime.year;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public getMonth(): number
|
|
36
|
-
{
|
|
37
|
-
return this.dateTime.month;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public getDay(): number
|
|
41
|
-
{
|
|
42
|
-
return this.dateTime.day;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public getHour(): number
|
|
46
|
-
{
|
|
47
|
-
return this.dateTime.hour;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public getMinute(): number
|
|
51
|
-
{
|
|
52
|
-
return this.dateTime.minute;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public getSecond(): number
|
|
56
|
-
{
|
|
57
|
-
return this.dateTime.second;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public addDays(days: number): LuxonDateTime
|
|
61
|
-
{
|
|
62
|
-
return LuxonDateTime.create(this.dateTime.plus({ days: days }));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public toString(): string
|
|
66
|
-
{
|
|
67
|
-
return this.dateTime.toISO()!;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
public toDateString(): string
|
|
71
|
-
{
|
|
72
|
-
return this.dateTime.toISODate()!;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public compareTo(dateTime: DateTime, compareTimes: boolean): number
|
|
76
|
-
{
|
|
77
|
-
return DateTime.compareTo(this, dateTime, compareTimes);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public lessThan(dateTime: DateTime, compareTimes: boolean): boolean
|
|
81
|
-
{
|
|
82
|
-
return DateTime.lessThan(this, dateTime, compareTimes);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public lessThanOrEqualTo(dateTime: DateTime, compareTimes: boolean): boolean
|
|
86
|
-
{
|
|
87
|
-
return DateTime.lessThanOrEqualTo(this, dateTime, compareTimes);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public equals(dateTime: DateTime, compareTimes: boolean): boolean
|
|
91
|
-
{
|
|
92
|
-
return DateTime.equals(this, dateTime, compareTimes);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
public greaterThanOrEqualTo(dateTime: DateTime, compareTimes: boolean): boolean
|
|
96
|
-
{
|
|
97
|
-
return DateTime.greaterThanOrEqualTo(this, dateTime, compareTimes);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
public greaterThan(dateTime: DateTime, compareTimes: boolean): boolean
|
|
101
|
-
{
|
|
102
|
-
return DateTime.greaterThan(this, dateTime, compareTimes);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public get debug(): string
|
|
106
|
-
{
|
|
107
|
-
return DateTime.debug(this);
|
|
108
|
-
}
|
|
1
|
+
import * as luxon from "luxon";
|
|
2
|
+
import { DateTime } from "./dateTime";
|
|
3
|
+
|
|
4
|
+
const pctTimeZone: string = "America/Los_Angeles";
|
|
5
|
+
|
|
6
|
+
export class LuxonDateTime implements DateTime
|
|
7
|
+
{
|
|
8
|
+
private readonly dateTime: luxon.DateTime;
|
|
9
|
+
|
|
10
|
+
private constructor(dateTime: luxon.DateTime)
|
|
11
|
+
{
|
|
12
|
+
this.dateTime = dateTime;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
private static create(dateTime: luxon.DateTime): LuxonDateTime
|
|
16
|
+
{
|
|
17
|
+
return new LuxonDateTime(dateTime);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static parse(text: string): LuxonDateTime
|
|
21
|
+
{
|
|
22
|
+
return LuxonDateTime.create(luxon.DateTime.fromISO(text, { zone: pctTimeZone }));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static now(): LuxonDateTime
|
|
26
|
+
{
|
|
27
|
+
return LuxonDateTime.create(luxon.DateTime.now().setZone(pctTimeZone));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public getYear(): number
|
|
31
|
+
{
|
|
32
|
+
return this.dateTime.year;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public getMonth(): number
|
|
36
|
+
{
|
|
37
|
+
return this.dateTime.month;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public getDay(): number
|
|
41
|
+
{
|
|
42
|
+
return this.dateTime.day;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public getHour(): number
|
|
46
|
+
{
|
|
47
|
+
return this.dateTime.hour;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public getMinute(): number
|
|
51
|
+
{
|
|
52
|
+
return this.dateTime.minute;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public getSecond(): number
|
|
56
|
+
{
|
|
57
|
+
return this.dateTime.second;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public addDays(days: number): LuxonDateTime
|
|
61
|
+
{
|
|
62
|
+
return LuxonDateTime.create(this.dateTime.plus({ days: days }));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public toString(): string
|
|
66
|
+
{
|
|
67
|
+
return this.dateTime.toISO()!;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public toDateString(): string
|
|
71
|
+
{
|
|
72
|
+
return this.dateTime.toISODate()!;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public compareTo(dateTime: DateTime, compareTimes: boolean): number
|
|
76
|
+
{
|
|
77
|
+
return DateTime.compareTo(this, dateTime, compareTimes);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public lessThan(dateTime: DateTime, compareTimes: boolean): boolean
|
|
81
|
+
{
|
|
82
|
+
return DateTime.lessThan(this, dateTime, compareTimes);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public lessThanOrEqualTo(dateTime: DateTime, compareTimes: boolean): boolean
|
|
86
|
+
{
|
|
87
|
+
return DateTime.lessThanOrEqualTo(this, dateTime, compareTimes);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public equals(dateTime: DateTime, compareTimes: boolean): boolean
|
|
91
|
+
{
|
|
92
|
+
return DateTime.equals(this, dateTime, compareTimes);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public greaterThanOrEqualTo(dateTime: DateTime, compareTimes: boolean): boolean
|
|
96
|
+
{
|
|
97
|
+
return DateTime.greaterThanOrEqualTo(this, dateTime, compareTimes);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public greaterThan(dateTime: DateTime, compareTimes: boolean): boolean
|
|
101
|
+
{
|
|
102
|
+
return DateTime.greaterThan(this, dateTime, compareTimes);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public get debug(): string
|
|
106
|
+
{
|
|
107
|
+
return DateTime.debug(this);
|
|
108
|
+
}
|
|
109
109
|
}
|
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
import { PreCondition } from "./preCondition";
|
|
2
|
-
import { Type } from "./types";
|
|
3
|
-
import { AsyncIterator } from "./asyncIterator";
|
|
4
|
-
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
5
|
-
import { JavascriptAsyncIterator } from "./javascript";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* An {@link AsyncIterator} that maps {@link TInput} values to {@link TOutput} values.
|
|
9
|
-
*/
|
|
10
|
-
export class MapAsyncIterator<TInput, TOutput> implements AsyncIterator<TOutput>
|
|
11
|
-
{
|
|
12
|
-
private readonly inputIterator: AsyncIterator<TInput>;
|
|
13
|
-
private readonly mapping: (value: TInput) => (TOutput | PromiseLike<TOutput>);
|
|
14
|
-
private current: TOutput | undefined;
|
|
15
|
-
private started: boolean;
|
|
16
|
-
|
|
17
|
-
protected constructor(inputIterator: AsyncIterator<TInput>, mapping: (value: TInput) => (TOutput | PromiseLike<TOutput>))
|
|
18
|
-
{
|
|
19
|
-
PreCondition.assertNotUndefinedAndNotNull(inputIterator, "inputIterator");
|
|
20
|
-
PreCondition.assertNotUndefinedAndNotNull(mapping, "mapping");
|
|
21
|
-
|
|
22
|
-
this.inputIterator = inputIterator;
|
|
23
|
-
this.mapping = mapping;
|
|
24
|
-
this.started = false;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public static create<TInput, TOutput>(inputIterator: AsyncIterator<TInput>, mapping: (value: TInput) => (TOutput | PromiseLike<TOutput>)): MapAsyncIterator<TInput, TOutput>
|
|
28
|
-
{
|
|
29
|
-
return new MapAsyncIterator(inputIterator, mapping);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public next(): PromiseAsyncResult<boolean>
|
|
33
|
-
{
|
|
34
|
-
return PromiseAsyncResult.create(async () =>
|
|
35
|
-
{
|
|
36
|
-
if (!this.hasStarted())
|
|
37
|
-
{
|
|
38
|
-
this.started = true;
|
|
39
|
-
await this.inputIterator.start();
|
|
40
|
-
}
|
|
41
|
-
else
|
|
42
|
-
{
|
|
43
|
-
await this.inputIterator.next();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const result: boolean = this.inputIterator.hasCurrent();
|
|
47
|
-
this.current = result
|
|
48
|
-
? await this.mapping(this.inputIterator.getCurrent())
|
|
49
|
-
: undefined;
|
|
50
|
-
|
|
51
|
-
return result;
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public hasStarted(): boolean
|
|
56
|
-
{
|
|
57
|
-
return this.started;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public hasCurrent(): boolean
|
|
61
|
-
{
|
|
62
|
-
return this.hasStarted() && this.inputIterator.hasCurrent();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public getCurrent(): TOutput
|
|
66
|
-
{
|
|
67
|
-
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
68
|
-
|
|
69
|
-
return this.current!;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public start(): PromiseAsyncResult<this>
|
|
73
|
-
{
|
|
74
|
-
return AsyncIterator.start<TOutput, this>(this);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public takeCurrent(): PromiseAsyncResult<TOutput>
|
|
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<TOutput[]>
|
|
93
|
-
{
|
|
94
|
-
return AsyncIterator.toArray(this);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public map<TOutput2>(mapping: (value: TOutput) => (TOutput2 | PromiseLike<TOutput2>)): AsyncIterator<TOutput2>
|
|
98
|
-
{
|
|
99
|
-
return AsyncIterator.map(this, mapping);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
public [Symbol.asyncIterator](): JavascriptAsyncIterator<TOutput>
|
|
103
|
-
{
|
|
104
|
-
return AsyncIterator[Symbol.asyncIterator](this);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
public first(condition?: (value: TOutput) => boolean): PromiseAsyncResult<TOutput>
|
|
108
|
-
{
|
|
109
|
-
return AsyncIterator.first(this, condition);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
public last(): PromiseAsyncResult<TOutput>
|
|
113
|
-
{
|
|
114
|
-
return AsyncIterator.last(this);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public where(condition: (value: TOutput) => (boolean | PromiseLike<boolean>)): AsyncIterator<TOutput>
|
|
118
|
-
{
|
|
119
|
-
return AsyncIterator.where(this, condition);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public whereInstanceOf<U extends TOutput>(typeCheck: (value: TOutput) => value is U): AsyncIterator<U>
|
|
123
|
-
{
|
|
124
|
-
return AsyncIterator.whereInstanceOf(this, typeCheck);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public whereInstanceOfType<U extends TOutput>(type: Type<U>): AsyncIterator<U>
|
|
128
|
-
{
|
|
129
|
-
return AsyncIterator.whereInstanceOfType(this, type);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
public take(maximumToTake: number): AsyncIterator<TOutput>
|
|
133
|
-
{
|
|
134
|
-
return AsyncIterator.take(this, maximumToTake);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
public skip(maximumToSkip: number): AsyncIterator<TOutput>
|
|
138
|
-
{
|
|
139
|
-
return AsyncIterator.skip(this, maximumToSkip);
|
|
140
|
-
}
|
|
1
|
+
import { PreCondition } from "./preCondition";
|
|
2
|
+
import { Type } from "./types";
|
|
3
|
+
import { AsyncIterator } from "./asyncIterator";
|
|
4
|
+
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
5
|
+
import { JavascriptAsyncIterator } from "./javascript";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An {@link AsyncIterator} that maps {@link TInput} values to {@link TOutput} values.
|
|
9
|
+
*/
|
|
10
|
+
export class MapAsyncIterator<TInput, TOutput> implements AsyncIterator<TOutput>
|
|
11
|
+
{
|
|
12
|
+
private readonly inputIterator: AsyncIterator<TInput>;
|
|
13
|
+
private readonly mapping: (value: TInput) => (TOutput | PromiseLike<TOutput>);
|
|
14
|
+
private current: TOutput | undefined;
|
|
15
|
+
private started: boolean;
|
|
16
|
+
|
|
17
|
+
protected constructor(inputIterator: AsyncIterator<TInput>, mapping: (value: TInput) => (TOutput | PromiseLike<TOutput>))
|
|
18
|
+
{
|
|
19
|
+
PreCondition.assertNotUndefinedAndNotNull(inputIterator, "inputIterator");
|
|
20
|
+
PreCondition.assertNotUndefinedAndNotNull(mapping, "mapping");
|
|
21
|
+
|
|
22
|
+
this.inputIterator = inputIterator;
|
|
23
|
+
this.mapping = mapping;
|
|
24
|
+
this.started = false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static create<TInput, TOutput>(inputIterator: AsyncIterator<TInput>, mapping: (value: TInput) => (TOutput | PromiseLike<TOutput>)): MapAsyncIterator<TInput, TOutput>
|
|
28
|
+
{
|
|
29
|
+
return new MapAsyncIterator(inputIterator, mapping);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public next(): PromiseAsyncResult<boolean>
|
|
33
|
+
{
|
|
34
|
+
return PromiseAsyncResult.create(async () =>
|
|
35
|
+
{
|
|
36
|
+
if (!this.hasStarted())
|
|
37
|
+
{
|
|
38
|
+
this.started = true;
|
|
39
|
+
await this.inputIterator.start();
|
|
40
|
+
}
|
|
41
|
+
else
|
|
42
|
+
{
|
|
43
|
+
await this.inputIterator.next();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const result: boolean = this.inputIterator.hasCurrent();
|
|
47
|
+
this.current = result
|
|
48
|
+
? await this.mapping(this.inputIterator.getCurrent())
|
|
49
|
+
: undefined;
|
|
50
|
+
|
|
51
|
+
return result;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public hasStarted(): boolean
|
|
56
|
+
{
|
|
57
|
+
return this.started;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public hasCurrent(): boolean
|
|
61
|
+
{
|
|
62
|
+
return this.hasStarted() && this.inputIterator.hasCurrent();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public getCurrent(): TOutput
|
|
66
|
+
{
|
|
67
|
+
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
68
|
+
|
|
69
|
+
return this.current!;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public start(): PromiseAsyncResult<this>
|
|
73
|
+
{
|
|
74
|
+
return AsyncIterator.start<TOutput, this>(this);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public takeCurrent(): PromiseAsyncResult<TOutput>
|
|
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<TOutput[]>
|
|
93
|
+
{
|
|
94
|
+
return AsyncIterator.toArray(this);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public map<TOutput2>(mapping: (value: TOutput) => (TOutput2 | PromiseLike<TOutput2>)): AsyncIterator<TOutput2>
|
|
98
|
+
{
|
|
99
|
+
return AsyncIterator.map(this, mapping);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public [Symbol.asyncIterator](): JavascriptAsyncIterator<TOutput>
|
|
103
|
+
{
|
|
104
|
+
return AsyncIterator[Symbol.asyncIterator](this);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public first(condition?: (value: TOutput) => boolean): PromiseAsyncResult<TOutput>
|
|
108
|
+
{
|
|
109
|
+
return AsyncIterator.first(this, condition);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public last(): PromiseAsyncResult<TOutput>
|
|
113
|
+
{
|
|
114
|
+
return AsyncIterator.last(this);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public where(condition: (value: TOutput) => (boolean | PromiseLike<boolean>)): AsyncIterator<TOutput>
|
|
118
|
+
{
|
|
119
|
+
return AsyncIterator.where(this, condition);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public whereInstanceOf<U extends TOutput>(typeCheck: (value: TOutput) => value is U): AsyncIterator<U>
|
|
123
|
+
{
|
|
124
|
+
return AsyncIterator.whereInstanceOf(this, typeCheck);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public whereInstanceOfType<U extends TOutput>(type: Type<U>): AsyncIterator<U>
|
|
128
|
+
{
|
|
129
|
+
return AsyncIterator.whereInstanceOfType(this, type);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public take(maximumToTake: number): AsyncIterator<TOutput>
|
|
133
|
+
{
|
|
134
|
+
return AsyncIterator.take(this, maximumToTake);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public skip(maximumToSkip: number): AsyncIterator<TOutput>
|
|
138
|
+
{
|
|
139
|
+
return AsyncIterator.skip(this, maximumToSkip);
|
|
140
|
+
}
|
|
141
141
|
}
|