@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.
Files changed (163) hide show
  1. package/.c8rc.json +12 -0
  2. package/.github/workflows/publish.yml +38 -0
  3. package/.mocharc.json +9 -0
  4. package/README.md +9 -0
  5. package/package.json +36 -0
  6. package/sources/assertMessageParameters.ts +22 -0
  7. package/sources/asyncIterator.ts +437 -0
  8. package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +48 -0
  9. package/sources/asyncResult.ts +95 -0
  10. package/sources/basicDisposable.ts +57 -0
  11. package/sources/byteList.ts +202 -0
  12. package/sources/byteListStream.ts +121 -0
  13. package/sources/byteReadStream.ts +24 -0
  14. package/sources/byteWriteStream.ts +16 -0
  15. package/sources/bytes.ts +25 -0
  16. package/sources/characterList.ts +195 -0
  17. package/sources/characterListStream.ts +151 -0
  18. package/sources/characterReadStream.ts +81 -0
  19. package/sources/characterReadStreamIterator.ts +128 -0
  20. package/sources/characterWriteStream.ts +45 -0
  21. package/sources/commandLineParameter.ts +45 -0
  22. package/sources/commandLineParameters.ts +21 -0
  23. package/sources/comparable.ts +144 -0
  24. package/sources/comparer.ts +133 -0
  25. package/sources/comparison.ts +20 -0
  26. package/sources/concatenateIterable.ts +119 -0
  27. package/sources/concatenateIterator.ts +165 -0
  28. package/sources/condition.ts +329 -0
  29. package/sources/currentProcess.ts +158 -0
  30. package/sources/dateTime.ts +130 -0
  31. package/sources/depthFirstSearch.ts +230 -0
  32. package/sources/disposable.ts +31 -0
  33. package/sources/emptyError.ts +10 -0
  34. package/sources/english.ts +45 -0
  35. package/sources/equalFunctions.ts +123 -0
  36. package/sources/fetchHttpClient.ts +89 -0
  37. package/sources/fetchHttpResponse.ts +106 -0
  38. package/sources/flatMapIterable.ts +104 -0
  39. package/sources/flatMapIterator.ts +152 -0
  40. package/sources/generator.ts +251 -0
  41. package/sources/httpClient.ts +36 -0
  42. package/sources/httpHeader.ts +37 -0
  43. package/sources/httpHeaders.ts +216 -0
  44. package/sources/httpIncomingRequest.ts +30 -0
  45. package/sources/httpIncomingResponse.ts +19 -0
  46. package/sources/httpMethod.ts +164 -0
  47. package/sources/httpOutgoingRequest.ts +119 -0
  48. package/sources/httpOutgoingResponse.ts +113 -0
  49. package/sources/httpServer.ts +34 -0
  50. package/sources/inMemoryCharacterWriteStream.ts +78 -0
  51. package/sources/index.ts +101 -0
  52. package/sources/iterable.ts +345 -0
  53. package/sources/iterator.ts +481 -0
  54. package/sources/iteratorToJavascriptIteratorAdapter.ts +48 -0
  55. package/sources/javascript.ts +59 -0
  56. package/sources/javascriptArrayList.ts +175 -0
  57. package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +124 -0
  58. package/sources/javascriptIteratorToIteratorAdapter.ts +133 -0
  59. package/sources/javascriptMapMap.ts +143 -0
  60. package/sources/javascriptSetSet.ts +134 -0
  61. package/sources/list.ts +330 -0
  62. package/sources/listQueue.ts +62 -0
  63. package/sources/listStack.ts +62 -0
  64. package/sources/luxonDateTime.ts +109 -0
  65. package/sources/map.ts +302 -0
  66. package/sources/mapAsyncIterator.ts +141 -0
  67. package/sources/mapIterable.ts +105 -0
  68. package/sources/mapIterator.ts +145 -0
  69. package/sources/mutableCondition.ts +451 -0
  70. package/sources/mutableHttpHeaders.ts +204 -0
  71. package/sources/mutableMap.ts +292 -0
  72. package/sources/network.ts +18 -0
  73. package/sources/node.ts +37 -0
  74. package/sources/nodeJSCharacterWriteStream.ts +42 -0
  75. package/sources/nodeJSHttpIncomingRequest.ts +132 -0
  76. package/sources/nodeJSHttpServer.ts +134 -0
  77. package/sources/notFoundError.ts +12 -0
  78. package/sources/postCondition.ts +284 -0
  79. package/sources/postConditionError.ts +12 -0
  80. package/sources/preCondition.ts +284 -0
  81. package/sources/preConditionError.ts +12 -0
  82. package/sources/promiseAsyncResult.ts +174 -0
  83. package/sources/property.ts +63 -0
  84. package/sources/queue.ts +49 -0
  85. package/sources/realNetwork.ts +28 -0
  86. package/sources/recreationDotGovClient.ts +259 -0
  87. package/sources/searchControl.ts +42 -0
  88. package/sources/set.ts +244 -0
  89. package/sources/skipAsyncIterator.ts +145 -0
  90. package/sources/skipIterator.ts +155 -0
  91. package/sources/stack.ts +48 -0
  92. package/sources/stringComparer.ts +33 -0
  93. package/sources/stringIterator.ts +149 -0
  94. package/sources/strings.ts +322 -0
  95. package/sources/syncResult.ts +300 -0
  96. package/sources/takeAsyncIterator.ts +141 -0
  97. package/sources/takeIterator.ts +151 -0
  98. package/sources/toStringFunctions.ts +185 -0
  99. package/sources/types.ts +371 -0
  100. package/sources/whereAsyncIterator.ts +143 -0
  101. package/sources/whereIterable.ts +108 -0
  102. package/sources/whereIterator.ts +157 -0
  103. package/sources/wonderlandTrailClient.ts +1503 -0
  104. package/tests/assertTest.ts +113 -0
  105. package/tests/assertTestTests.ts +75 -0
  106. package/tests/basicTestSkip.ts +51 -0
  107. package/tests/byteListStreamTests.ts +390 -0
  108. package/tests/byteListTests.ts +27 -0
  109. package/tests/bytesTests.ts +43 -0
  110. package/tests/characterListStreamTests.ts +391 -0
  111. package/tests/characterListTests.ts +250 -0
  112. package/tests/characterWriteStreamTests.ts +12 -0
  113. package/tests/comparerTests.ts +92 -0
  114. package/tests/conditionTests.ts +877 -0
  115. package/tests/consoleTestRunner.ts +404 -0
  116. package/tests/consoleTestRunnerTests.ts +651 -0
  117. package/tests/dateTimeTests.ts +30 -0
  118. package/tests/depthFirstSearchTests.ts +106 -0
  119. package/tests/disposableTests.ts +121 -0
  120. package/tests/englishTests.ts +103 -0
  121. package/tests/equalFunctionsTests.ts +223 -0
  122. package/tests/failedTest.ts +43 -0
  123. package/tests/fetchHttpClientTests.ts +33 -0
  124. package/tests/generatorTests.ts +86 -0
  125. package/tests/httpClientTests.ts +18 -0
  126. package/tests/inMemoryCharacterWriteStreamTests.ts +117 -0
  127. package/tests/iterableTests.ts +141 -0
  128. package/tests/iteratorTests.ts +1086 -0
  129. package/tests/javascriptMapMapTests.ts +21 -0
  130. package/tests/listTests.ts +338 -0
  131. package/tests/mapIteratorTests.ts +55 -0
  132. package/tests/mapTests.ts +104 -0
  133. package/tests/mutableConditionTests.ts +273 -0
  134. package/tests/mutableMapTests.ts +154 -0
  135. package/tests/nodeJSHttpServerTests.ts +75 -0
  136. package/tests/notFoundErrorTests.ts +24 -0
  137. package/tests/postConditionErrorTests.ts +24 -0
  138. package/tests/preConditionErrorTests.ts +24 -0
  139. package/tests/promiseAsyncResultTests.ts +688 -0
  140. package/tests/propertyTests.ts +63 -0
  141. package/tests/queueTests.ts +29 -0
  142. package/tests/recreationDotGovClientTests.ts +191 -0
  143. package/tests/setTests.ts +140 -0
  144. package/tests/skippedTest.ts +39 -0
  145. package/tests/stackTests.ts +66 -0
  146. package/tests/stringComparerTests.ts +60 -0
  147. package/tests/stringIteratorTests.ts +156 -0
  148. package/tests/stringsTests.ts +516 -0
  149. package/tests/syncResultTests.ts +1251 -0
  150. package/tests/test.ts +228 -0
  151. package/tests/testAction.ts +75 -0
  152. package/tests/testActionTests.ts +93 -0
  153. package/tests/testFailureTests.ts +12 -0
  154. package/tests/testRunner.ts +267 -0
  155. package/tests/testRunnerTests.ts +895 -0
  156. package/tests/testSkip.ts +34 -0
  157. package/tests/tests.ts +103 -0
  158. package/tests/toStringFunctionsTests.ts +55 -0
  159. package/tests/typesTests.ts +257 -0
  160. package/tests/whereIteratorTests.ts +77 -0
  161. package/tests/wonderlandTrailClientTests.ts +452 -0
  162. package/tsconfig.json +17 -0
  163. package/tsup.config.ts +13 -0
@@ -0,0 +1,195 @@
1
+ import { EqualFunctions } from "./equalFunctions";
2
+ import { Iterable } from "./iterable";
3
+ import { Iterator } from "./iterator";
4
+ import { JavascriptIterable, JavascriptIterator } from "./javascript";
5
+ import { List } from "./list";
6
+ import { PreCondition } from "./preCondition";
7
+ import { StringIterator } from "./stringIterator";
8
+ import { join } from "./strings";
9
+ import { SyncResult } from "./syncResult";
10
+ import { ToStringFunctions } from "./toStringFunctions";
11
+ import { isString, Type } from "./types";
12
+
13
+ export class CharacterList implements List<string>
14
+ {
15
+ private characters: string;
16
+
17
+ private constructor(values?: JavascriptIterable<string> | string)
18
+ {
19
+ if (isString(values))
20
+ {
21
+ this.characters = values;
22
+ }
23
+ else if (values)
24
+ {
25
+ this.characters = join("", values);
26
+ }
27
+ else
28
+ {
29
+ this.characters = "";
30
+ }
31
+ }
32
+
33
+ public static create(values?: JavascriptIterable<string>): CharacterList
34
+ {
35
+ return new CharacterList(values);
36
+ }
37
+
38
+ public getCount(): SyncResult<number>
39
+ {
40
+ return SyncResult.value(this.characters.length);
41
+ }
42
+
43
+ public insert(index: number, value: string): this
44
+ {
45
+ PreCondition.assertInsertIndex(index, this.getCount().await(), "index");
46
+ PreCondition.assertCharacter(value, "value");
47
+
48
+ if (index === 0)
49
+ {
50
+ this.characters = value + this.characters;
51
+ }
52
+ else if (index === this.getCount().await())
53
+ {
54
+ this.characters += value;
55
+ }
56
+ else
57
+ {
58
+ this.characters = this.characters.slice(0, index) + value + this.characters.slice(index);
59
+ }
60
+
61
+ return this;
62
+ }
63
+
64
+ public removeAt(index: number): SyncResult<string>
65
+ {
66
+ PreCondition.assertAccessIndex(index, this.getCount().await(), "index");
67
+
68
+ const result: string = this.get(index).await();
69
+ this.characters =
70
+ (index === 0 ? "" : this.characters.slice(0, index)) +
71
+ (index === this.getCount().await() - 1 ? "" : this.characters.slice(index + 1));
72
+
73
+ return SyncResult.value(result);
74
+ }
75
+
76
+ public set(index: number, value: string): this
77
+ {
78
+ PreCondition.assertAccessIndex(index, this.getCount().await(), "index");
79
+ PreCondition.assertCharacter(value, "value");
80
+
81
+ this.characters =
82
+ (index === 0 ? "" : this.characters.slice(0, index)) +
83
+ value +
84
+ (index === this.getCount().await() - 1 ? "" : this.characters.slice(index + 1));
85
+
86
+ return this;
87
+ }
88
+
89
+ public iterate(): Iterator<string>
90
+ {
91
+ return StringIterator.create(this.characters);
92
+ }
93
+
94
+ public get(index: number): SyncResult<string>
95
+ {
96
+ PreCondition.assertAccessIndex(index, this.getCount().await(), "index");
97
+
98
+ return SyncResult.value(this.characters.charAt(index));
99
+ }
100
+
101
+ public add(value: string): this
102
+ {
103
+ return List.add(this, value);
104
+ }
105
+
106
+ public addAll(values: JavascriptIterable<string>): this
107
+ {
108
+ return List.addAll(this, values);
109
+ }
110
+
111
+ public insertAll(index: number, values: JavascriptIterable<string>): this
112
+ {
113
+ return List.insertAll(this, index, values);
114
+ }
115
+
116
+ public remove(value: string, equalFunctions?: EqualFunctions): SyncResult<string>
117
+ {
118
+ return List.remove(this, value, equalFunctions);
119
+ }
120
+
121
+ public removeFirst(): SyncResult<string>
122
+ {
123
+ return List.removeFirst(this);
124
+ }
125
+
126
+ public removeLast(): SyncResult<string>
127
+ {
128
+ return List.removeLast(this);
129
+ }
130
+
131
+ public toArray(): SyncResult<string[]>
132
+ {
133
+ return List.toArray(this);
134
+ }
135
+
136
+ public any(): SyncResult<boolean>
137
+ {
138
+ return List.any(this);
139
+ }
140
+
141
+ public equals(right: JavascriptIterable<string>, equalFunctions?: EqualFunctions): SyncResult<boolean>
142
+ {
143
+ return List.equals(this, right, equalFunctions);
144
+ }
145
+
146
+ public toString(toStringFunctions?: ToStringFunctions): string
147
+ {
148
+ return List.toString(this, toStringFunctions);
149
+ }
150
+
151
+ public concatenate(...toConcatenate: JavascriptIterable<string>[]): Iterable<string>
152
+ {
153
+ return List.concatenate(this, ...toConcatenate);
154
+ }
155
+
156
+ public map<TOutput>(mapping: (value: string) => TOutput | SyncResult<TOutput>): Iterable<TOutput>
157
+ {
158
+ return List.map(this, mapping);
159
+ }
160
+
161
+ public flatMap<TOutput>(mapping: (value: string) => JavascriptIterable<TOutput>): Iterable<TOutput>
162
+ {
163
+ return List.flatMap(this, mapping);
164
+ }
165
+
166
+ public where(condition: (value: string) => (boolean | SyncResult<boolean>)): Iterable<string>
167
+ {
168
+ return List.where(this, condition);
169
+ }
170
+
171
+ public instanceOf<TOutput extends string>(typeOrTypeCheck: Type<TOutput> | ((value: string) => value is TOutput)): Iterable<TOutput>
172
+ {
173
+ return List.instanceOf(this, typeOrTypeCheck);
174
+ }
175
+
176
+ public first(condition?: (value: string) => (boolean | SyncResult<boolean>)): SyncResult<string>
177
+ {
178
+ return List.first(this, condition);
179
+ }
180
+
181
+ public last(condition?: (value: string) => (boolean | SyncResult<boolean>)): SyncResult<string>
182
+ {
183
+ return List.last(this, condition);
184
+ }
185
+
186
+ public contains(value: string, equalFunctions?: EqualFunctions): SyncResult<boolean>
187
+ {
188
+ return List.contains(this, value, equalFunctions);
189
+ }
190
+
191
+ public [Symbol.iterator](): JavascriptIterator<string>
192
+ {
193
+ return List[Symbol.iterator](this);
194
+ }
195
+ }
@@ -0,0 +1,151 @@
1
+ import { CharacterList } from "./characterList";
2
+ import { CharacterReadStream } from "./characterReadStream";
3
+ import { CharacterWriteStream } from "./characterWriteStream";
4
+ import { EmptyError } from "./emptyError";
5
+ import { JavascriptIterable } from "./javascript";
6
+ import { PreCondition } from "./preCondition";
7
+ import { AsyncResult } from "./asyncResult";
8
+ import { join } from "./strings";
9
+ import { SyncResult } from "./syncResult";
10
+ import { isNumber, isString, isUndefinedOrNull } from "./types";
11
+
12
+ /**
13
+ * A {@link CharacterReadStream} and {@link CharacterWriteStream} implementation that is implemented using a
14
+ * {@link CharacterList}.
15
+ */
16
+ export class CharacterListStream implements CharacterReadStream, CharacterWriteStream
17
+ {
18
+ private readonly list: CharacterList;
19
+
20
+ private constructor()
21
+ {
22
+ this.list = CharacterList.create();
23
+ }
24
+
25
+ public static create(initialValues?: JavascriptIterable<string>): CharacterListStream
26
+ {
27
+ const result: CharacterListStream = new CharacterListStream();
28
+ if (initialValues)
29
+ {
30
+ result.writeCharacters(initialValues).await();
31
+ }
32
+ return result;
33
+ }
34
+
35
+ public writeCharacters(characters: JavascriptIterable<string>, startIndex?: number, length?: number): SyncResult<number>
36
+ {
37
+ PreCondition.assertNotUndefinedAndNotNull(characters, "characters");
38
+
39
+ const characterString: string = isString(characters) ? characters : join("", characters);
40
+ if (isUndefinedOrNull(startIndex))
41
+ {
42
+ startIndex = 0;
43
+ }
44
+ if (isUndefinedOrNull(length))
45
+ {
46
+ length = characterString.length - startIndex;
47
+ }
48
+
49
+ PreCondition.assertInsertIndex(startIndex, characterString.length, "startIndex");
50
+ PreCondition.assertBetween(0, length, characterString.length - startIndex, "length");
51
+
52
+ this.list.addAll(characterString.slice(startIndex, length + startIndex));
53
+
54
+ return SyncResult.value(length);
55
+ }
56
+
57
+
58
+ public writeString(text: string): AsyncResult<number>
59
+ {
60
+ this.list.addAll(text);
61
+
62
+ return SyncResult.value(text.length);
63
+ }
64
+
65
+ public writeLine(text?: string): AsyncResult<number>
66
+ {
67
+ return CharacterWriteStream.writeLine(this, text);
68
+ }
69
+
70
+ /**
71
+ * Get the number of characters that are available to be read.
72
+ */
73
+ public getAvailableCharacterCount(): number
74
+ {
75
+ return this.list.getCount().await();
76
+ }
77
+
78
+ public readCharacter(): AsyncResult<string>
79
+ {
80
+ return !this.list.any().await()
81
+ ? SyncResult.error(new EmptyError())
82
+ : SyncResult.value(this.list.removeFirst().await());
83
+ }
84
+
85
+ public readCharacters(count: number): SyncResult<string>;
86
+ public readCharacters(output: string[], startIndex?: number, count?: number): SyncResult<number>;
87
+ readCharacters(countOrOutput: number | string[], startIndex?: number, count?: number): SyncResult<number> | SyncResult<string>
88
+ {
89
+ let result: SyncResult<number> | SyncResult<string>;
90
+ if (isNumber(countOrOutput))
91
+ {
92
+ PreCondition.assertGreaterThanOrEqualTo(countOrOutput, 0, "count");
93
+
94
+ if (!this.list.any().await())
95
+ {
96
+ result = SyncResult.error<string>(new EmptyError());
97
+ }
98
+ else
99
+ {
100
+ const bytesReadCount: number = Math.min(countOrOutput, this.list.getCount().await());
101
+ let output: string = "";
102
+ for (let i = 0; i < bytesReadCount; i++)
103
+ {
104
+ output += this.list.removeFirst().await();
105
+ }
106
+ result = SyncResult.value(output);
107
+ }
108
+ }
109
+ else
110
+ {
111
+ PreCondition.assertNotUndefinedAndNotNull(countOrOutput, "output");
112
+
113
+ if (isUndefinedOrNull(startIndex))
114
+ {
115
+ startIndex = 0;
116
+ }
117
+ if (isUndefinedOrNull(count))
118
+ {
119
+ count = countOrOutput.length - startIndex;
120
+ }
121
+
122
+ PreCondition.assertInsertIndex(startIndex, countOrOutput.length, "startIndex");
123
+ PreCondition.assertBetween(0, count, countOrOutput.length - startIndex, "count");
124
+
125
+ if (!this.list.any().await())
126
+ {
127
+ result = SyncResult.error<number>(new EmptyError());
128
+ }
129
+ else
130
+ {
131
+ const bytesReadCount: number = Math.min(count, this.list.getCount().await());
132
+ for (let i = 0; i < bytesReadCount; i++)
133
+ {
134
+ countOrOutput[startIndex + i] = this.list.removeFirst().await();
135
+ }
136
+ result = SyncResult.value(bytesReadCount);
137
+ }
138
+ }
139
+ return result;
140
+ }
141
+
142
+ public readUntil(searchString: string): AsyncResult<string>
143
+ {
144
+ return CharacterReadStream.readUntil(this, searchString);
145
+ }
146
+
147
+ public readLine(): AsyncResult<string>
148
+ {
149
+ return CharacterReadStream.readLine(this);
150
+ }
151
+ }
@@ -0,0 +1,81 @@
1
+ import { PreCondition } from "./preCondition";
2
+ import { AsyncResult } from "./asyncResult";
3
+ import { SyncResult } from "./syncResult";
4
+
5
+ export abstract class CharacterReadStream
6
+ {
7
+ /**
8
+ * Read a single character from this stream.
9
+ */
10
+ public abstract readCharacter(): AsyncResult<string>;
11
+
12
+ public readCharacters(count: number): AsyncResult<string>
13
+ {
14
+ return CharacterReadStream.readCharacters(this, count);
15
+ }
16
+
17
+ public static readCharacters(readStream: CharacterReadStream, count: number): AsyncResult<string>
18
+ {
19
+ let characters: string = "";
20
+ function readUntilCount(countRemaining: number): AsyncResult<string>
21
+ {
22
+ return readStream.readCharacter()
23
+ .then((character: string) =>
24
+ {
25
+ characters += character;
26
+ return countRemaining === 0
27
+ ? SyncResult.value(characters)
28
+ : readUntilCount(countRemaining - 1);
29
+ });
30
+ }
31
+ return readUntilCount(count);
32
+ }
33
+
34
+ /**
35
+ * Read characters from this stream until the provided {@link searchString} is found or the end
36
+ * of the stream is reached. The {@link searchString} will be included in the returned string if
37
+ * it is found..
38
+ * @param searchString The string to search for.
39
+ */
40
+ public readUntil(searchString: string): AsyncResult<string>
41
+ {
42
+ return CharacterReadStream.readUntil(this, searchString);
43
+ }
44
+
45
+ public static readUntil(readStream: CharacterReadStream, searchString: string): AsyncResult<string>
46
+ {
47
+ PreCondition.assertNotUndefinedAndNotNull(readStream, "readStream");
48
+ PreCondition.assertNotEmpty(searchString, "searchString");
49
+
50
+ let characters: string = "";
51
+ function readUntilSearchString(): AsyncResult<string>
52
+ {
53
+ return readStream.readCharacter()
54
+ .then((character: string) =>
55
+ {
56
+ characters += character;
57
+ return characters.endsWith(searchString)
58
+ ? SyncResult.value(characters)
59
+ : readUntilSearchString();
60
+ });
61
+ }
62
+ return readUntilSearchString();
63
+ }
64
+
65
+ /**
66
+ * Read a sequence of characters from this stream until either a newline character ('\\n') or
67
+ * the end of the stream is reached. Terminating newline characters will be included in the
68
+ * returned string.
69
+ */
70
+ public readLine(): AsyncResult<string>
71
+ {
72
+ return CharacterReadStream.readLine(this);
73
+ }
74
+
75
+ public static readLine(readStream: CharacterReadStream): AsyncResult<string>
76
+ {
77
+ PreCondition.assertNotUndefinedAndNotNull(readStream, "readStream");
78
+
79
+ return readStream.readUntil("\n");
80
+ }
81
+ }
@@ -0,0 +1,128 @@
1
+ import { AsyncIterator } from "./asyncIterator";
2
+ import { PromiseAsyncResult } from "./promiseAsyncResult";
3
+ import { CharacterReadStream } from "./characterReadStream";
4
+ import { JavascriptAsyncIterator } from "./javascript";
5
+ import { NotFoundError } from "./notFoundError";
6
+ import { PreCondition } from "./preCondition";
7
+ import { Type } from "./types";
8
+
9
+ export class CharacterReadStreamAsyncIterator implements AsyncIterator<string>
10
+ {
11
+ private readonly readStream: CharacterReadStream;
12
+ private current: string;
13
+ private started: boolean;
14
+
15
+ private constructor(readStream: CharacterReadStream)
16
+ {
17
+ PreCondition.assertNotUndefinedAndNotNull(readStream, "readStream");
18
+
19
+ this.readStream = readStream;
20
+ this.current = "";
21
+ this.started = false;
22
+ }
23
+
24
+ public static create(readStream: CharacterReadStream): CharacterReadStreamAsyncIterator
25
+ {
26
+ return new CharacterReadStreamAsyncIterator(readStream);
27
+ }
28
+
29
+ public next(): PromiseAsyncResult<boolean>
30
+ {
31
+ return PromiseAsyncResult.create(async () =>
32
+ {
33
+ this.started = true;
34
+
35
+ this.current = await this.readStream.readCharacter()
36
+ .catch(NotFoundError, () => "");
37
+
38
+ return this.hasCurrent();
39
+ });
40
+ }
41
+
42
+ public hasStarted(): boolean
43
+ {
44
+ return this.started;
45
+ }
46
+
47
+ public hasCurrent(): boolean
48
+ {
49
+ return this.current !== "";
50
+ }
51
+
52
+ public getCurrent(): string
53
+ {
54
+ PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
55
+
56
+ return this.current;
57
+ }
58
+
59
+ public start(): PromiseAsyncResult<this>
60
+ {
61
+ return AsyncIterator.start<string,this>(this);
62
+ }
63
+
64
+ public takeCurrent(): PromiseAsyncResult<string>
65
+ {
66
+ return AsyncIterator.takeCurrent(this);
67
+ }
68
+
69
+ public any(): PromiseAsyncResult<boolean>
70
+ {
71
+ return AsyncIterator.any(this);
72
+ }
73
+
74
+ public getCount(): PromiseAsyncResult<number>
75
+ {
76
+ return AsyncIterator.getCount(this);
77
+ }
78
+
79
+ public toArray(): PromiseAsyncResult<string[]>
80
+ {
81
+ return AsyncIterator.toArray(this);
82
+ }
83
+
84
+ public where(condition: (value: string) => boolean | PromiseLike<boolean>): AsyncIterator<string>
85
+ {
86
+ return AsyncIterator.where(this, condition);
87
+ }
88
+
89
+ public map<TOutput>(mapping: (value: string) => (TOutput | PromiseLike<TOutput>)): AsyncIterator<TOutput>
90
+ {
91
+ return AsyncIterator.map(this, mapping);
92
+ }
93
+
94
+ public whereInstanceOf<U extends string>(typeCheck: (value: string) => value is U): AsyncIterator<U>
95
+ {
96
+ return AsyncIterator.whereInstanceOf(this, typeCheck);
97
+ }
98
+
99
+ public whereInstanceOfType<U extends string>(type: Type<U>): AsyncIterator<U>
100
+ {
101
+ return AsyncIterator.whereInstanceOfType(this, type);
102
+ }
103
+
104
+ public first(condition?: (value: string) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<string>
105
+ {
106
+ return AsyncIterator.first(this, condition);
107
+ }
108
+
109
+ public last(condition?: (value: string) => (boolean | PromiseLike<boolean>)): PromiseAsyncResult<string>
110
+ {
111
+ return AsyncIterator.last(this, condition);
112
+ }
113
+
114
+ public take(maximumToTake: number): AsyncIterator<string>
115
+ {
116
+ return AsyncIterator.take(this, maximumToTake);
117
+ }
118
+
119
+ public skip(maximumToSkip: number): AsyncIterator<string>
120
+ {
121
+ return AsyncIterator.skip(this, maximumToSkip);
122
+ }
123
+
124
+ public [Symbol.asyncIterator](): JavascriptAsyncIterator<string>
125
+ {
126
+ return AsyncIterator[Symbol.asyncIterator](this);
127
+ }
128
+ }
@@ -0,0 +1,45 @@
1
+ import { PromiseAsyncResult } from "./promiseAsyncResult";
2
+ import { PostCondition } from "./postCondition";
3
+ import { PreCondition } from "./preCondition";
4
+ import { AsyncResult } from "./asyncResult";
5
+
6
+ export abstract class CharacterWriteStream
7
+ {
8
+ /**
9
+ * Write the provided text to this {@link CharacterWriteStream}.
10
+ * @param text The text to write.
11
+ * @returns The number of characters that were written.
12
+ */
13
+ public abstract writeString(text: string): AsyncResult<number>
14
+
15
+ /**
16
+ * Write the provided text (if provided) and then write a newline character sequence to this
17
+ * {@link CharacterWriteStream}.
18
+ * @param text The optional text to write before the newline character sequence.
19
+ * @returns The number of characters that were written.
20
+ */
21
+ public writeLine(text?: string): AsyncResult<number>
22
+ {
23
+ return CharacterWriteStream.writeLine(this, text);
24
+ }
25
+
26
+ public static writeLine(writeStream: CharacterWriteStream, text?: string): AsyncResult<number>
27
+ {
28
+ PreCondition.assertNotUndefinedAndNotNull(writeStream, "writeStream");
29
+
30
+ return PromiseAsyncResult.create(async () =>
31
+ {
32
+ let result: number = 0;
33
+
34
+ if (text)
35
+ {
36
+ result += await writeStream.writeString(text);
37
+ }
38
+ result += await writeStream.writeString("\n");
39
+
40
+ PostCondition.assertGreaterThan(result, 0, "result");
41
+
42
+ return result;
43
+ });
44
+ }
45
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * An individual parameter from a {@link CommandLineParameters} object.
3
+ */
4
+ export class CommandLineParameter<T>
5
+ {
6
+ /**
7
+ * The full name of this {@link CommandLineParameter}.
8
+ */
9
+ private readonly name: string;
10
+ /**
11
+ * The description for this {@link CommandLineParameter}.
12
+ */
13
+ private readonly description: string;
14
+ /**
15
+ * The function that can be invoked to get this {@link CommandLineParameter}'s value.
16
+ */
17
+ private readonly valueGetter: () => T;
18
+
19
+ private constructor(name: string, description: string, valueGetter: () => T)
20
+ {
21
+ this.name = name;
22
+ this.description = description;
23
+ this.valueGetter = valueGetter;
24
+ }
25
+
26
+ /**
27
+ * Create a new {@link CommandLineParameter}.
28
+ * @param name The full name of the {@link CommandLineParameter}.
29
+ * @param description The description for the {@link CommandLineParameter}.
30
+ * @param valueGetter The function that will be used to get the returned
31
+ * {@link CommandLineParameter}'s value.
32
+ */
33
+ public static create<T>(name: string, description: string, valueGetter: () => T)
34
+ {
35
+ return new CommandLineParameter<T>(name, description, valueGetter);
36
+ }
37
+
38
+ /**
39
+ * Get the value for this {@link CommandLineParameter}.
40
+ */
41
+ public getValue(): T
42
+ {
43
+ return this.valueGetter();
44
+ }
45
+ }
@@ -0,0 +1,21 @@
1
+ import { JavascriptIterable } from "./javascript";
2
+
3
+ /**
4
+ * A class that can be used to define and interact with an application's command line interface.
5
+ */
6
+ export class CommandLineParameters
7
+ {
8
+ private readonly args: JavascriptIterable<string>;
9
+
10
+ private constructor(argv: JavascriptIterable<string>)
11
+ {
12
+ this.args = argv
13
+ }
14
+
15
+ public static create(args: JavascriptIterable<string>): CommandLineParameters
16
+ {
17
+ return new CommandLineParameters(args)
18
+ }
19
+
20
+
21
+ }