@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,451 @@
1
+ import { AssertMessageParameters } from "./assertMessageParameters";
2
+ import { Bytes } from "./bytes";
3
+ import { Comparer } from "./comparer";
4
+ import { Comparison } from "./comparison";
5
+ import { Condition } from "./condition";
6
+ import { EqualFunctions } from "./equalFunctions";
7
+ import { Iterable } from "./iterable";
8
+ import { JavascriptIterable } from "./javascript";
9
+ import { ToStringFunctions } from "./toStringFunctions";
10
+ import {
11
+ hasFunction, hasProperty, instanceOf, isJavascriptIterable, isString, isUndefinedOrNull, Type
12
+ } from "./types";
13
+
14
+ /**
15
+ * A collection of condition methods that can be used to assert the state of an application.
16
+ */
17
+ export class MutableCondition implements Condition
18
+ {
19
+ private toStringFunctions: ToStringFunctions;
20
+ private equalFunctions: EqualFunctions;
21
+ private createErrorFunction: (message: string) => Error;
22
+
23
+ protected constructor()
24
+ {
25
+ this.toStringFunctions = ToStringFunctions.create();
26
+ this.equalFunctions = EqualFunctions.create();
27
+ this.createErrorFunction = MutableCondition.defaultCreateErrorFunction;
28
+ }
29
+
30
+ private static defaultCreateErrorFunction(message: string): Error
31
+ {
32
+ return new Error(message);
33
+ }
34
+
35
+ /**
36
+ * Create a new {@link MutableCondition} object.
37
+ */
38
+ public static create(): MutableCondition
39
+ {
40
+ return new MutableCondition();
41
+ }
42
+
43
+ /**
44
+ * Set the {@link ToStringFunctions} that will be used to convert values to their {@link String}
45
+ * representations.
46
+ * @param toStringFunctions The {@link ToStringFunctions} that will be used to convert values to
47
+ * their {@link String} representations.
48
+ * @returns This object for method chaining.
49
+ */
50
+ public setToStringFunctions(toStringFunctions: ToStringFunctions): this
51
+ {
52
+ this.assertNotUndefinedAndNotNull(toStringFunctions, "toStringFunctions");
53
+
54
+ this.toStringFunctions = toStringFunctions;
55
+
56
+ return this;
57
+ }
58
+
59
+ /**
60
+ * Set the {@link EqualFunctions} that will be used to determine if values are equal.
61
+ * @param equalFunctions The {@link EqualFunctions} that will be used to determine if values are
62
+ * equal.
63
+ * @returns This object for method chaining.
64
+ */
65
+ public setEqualFunctions(equalFunctions: EqualFunctions): this
66
+ {
67
+ this.assertNotUndefinedAndNotNull(equalFunctions, "equalFunctions");
68
+
69
+ this.equalFunctions = equalFunctions;
70
+
71
+ return this;
72
+ }
73
+
74
+ /**
75
+ * Set the {@link Function} that will be used to create {@link Error}s.
76
+ * @param createErrorFunction The {@link Function} to use to create {@link Error}.
77
+ * @returns This object for method chaining.
78
+ */
79
+ public setCreateErrorFunction(createErrorFunction: (message: string) => Error): this
80
+ {
81
+ this.assertNotUndefinedAndNotNull(createErrorFunction, "createErrorFunction");
82
+
83
+ this.createErrorFunction = createErrorFunction;
84
+ return this;
85
+ }
86
+
87
+ public assertUndefined(value: unknown, expression?: string, message?: string): asserts value is undefined
88
+ {
89
+ Condition.assertUndefined(this, value, expression, message);
90
+ }
91
+
92
+ public assertNotUndefined<T>(value: T, expression?: string, message?: string): asserts value is NonNullable<T>
93
+ {
94
+ Condition.assertNotUndefined(this, value, expression, message);
95
+ }
96
+
97
+ /**
98
+ * Get whether the provided values are equal.
99
+ * @param left The left part of the comparison.
100
+ * @param right The right part of the comparison.
101
+ */
102
+ public areEqual(left: unknown, right: unknown): boolean
103
+ {
104
+ return this.equalFunctions.areEqual(left, right).await();
105
+ }
106
+
107
+ /**
108
+ * Get the {@link String} representation of the provided value.
109
+ * @param value The value to get the {@link String} representation of.
110
+ */
111
+ public toValueString(value: unknown): string
112
+ {
113
+ return this.toStringFunctions.toString(value);
114
+ }
115
+
116
+ /**
117
+ * Create an {@link Error} based on the provided {@link AssertMessageParameters}.
118
+ * @param parameters The {@link AssertMessageParameters} that define how the should be made.
119
+ */
120
+ public createError(parameters: AssertMessageParameters): Error
121
+ {
122
+ const message: string = MutableCondition.createErrorMessage(parameters);
123
+ return this.createErrorFunction(message);
124
+ }
125
+
126
+ /**
127
+ * Create an error message based on the provided parameters.
128
+ * @param parameters The parameters to use to create the error message.
129
+ */
130
+ public static createErrorMessage(parameters: AssertMessageParameters): string
131
+ {
132
+ let result: string = "";
133
+
134
+ if (parameters.message)
135
+ {
136
+ result += `Message: ${parameters.message}`;
137
+ }
138
+
139
+ if (parameters.expression)
140
+ {
141
+ if (result)
142
+ {
143
+ result += "\n";
144
+ }
145
+ result += `Expression: ${parameters.expression}`;
146
+ }
147
+
148
+ if (result)
149
+ {
150
+ result += "\n";
151
+ }
152
+ result += `Expected: ${parameters.expected}`;
153
+
154
+ if (result)
155
+ {
156
+ result += "\n";
157
+ }
158
+ result += `Actual: ${parameters.actual}`;
159
+
160
+ return result;
161
+ }
162
+
163
+ public assertNotUndefinedAndNotNull<T>(value: T, expression?: string, message?: string): asserts value is NonNullable<T>
164
+ {
165
+ if (value === undefined || value === null)
166
+ {
167
+ throw this.createError({
168
+ expected: `not ${this.toValueString(undefined)} and not ${this.toValueString(null)}`,
169
+ actual: this.toValueString(value),
170
+ expression: expression,
171
+ message: message,
172
+ });
173
+ }
174
+ }
175
+
176
+ public assertTrue(value: boolean, expression?: string, message?: string): asserts value is true
177
+ {
178
+ Condition.assertTrue(this, value, expression, message);
179
+ }
180
+
181
+ public assertFalse(value: boolean, expression?: string, message?: string): asserts value is false
182
+ {
183
+ if (value !== false)
184
+ {
185
+ throw this.createError({
186
+ expected: this.toValueString(false),
187
+ actual: this.toValueString(value),
188
+ expression: expression,
189
+ message: message,
190
+ });
191
+ }
192
+ }
193
+
194
+ public assertSame<T>(expected: T, actual: T, expression?: string, message?: string): void
195
+ {
196
+ if (expected !== actual)
197
+ {
198
+ throw this.createError({
199
+ expected: this.toValueString(expected),
200
+ actual: this.toValueString(actual),
201
+ expression: expression,
202
+ message: message,
203
+ });
204
+ }
205
+ }
206
+
207
+ public assertNotSame<T>(expected: T, actual: T, expression?: string, message?: string): void
208
+ {
209
+ if (expected === actual)
210
+ {
211
+ throw this.createError({
212
+ expected: `not ${this.toValueString(expected)}`,
213
+ actual: this.toValueString(actual),
214
+ expression: expression,
215
+ message: message,
216
+ });
217
+ }
218
+ }
219
+
220
+ public assertEqual<T>(expected: T, actual: T, expression?: string, message?: string): void
221
+ {
222
+ let comparison: Comparison | undefined = Comparer.compareSameUndefinedNull(expected, actual);
223
+ if (comparison === undefined && this.areEqual(expected, actual))
224
+ {
225
+ comparison = Comparison.Equal;
226
+ }
227
+
228
+ if (comparison !== Comparison.Equal)
229
+ {
230
+ throw this.createError({
231
+ expected: this.toValueString(expected),
232
+ actual: this.toValueString(actual),
233
+ expression: expression,
234
+ message: message,
235
+ });
236
+ }
237
+ }
238
+
239
+ public assertNotEqual<T>(notExpected: T, actual: T, expression?: string, message?: string): void
240
+ {
241
+ let comparison: Comparison | undefined = Comparer.compareSameUndefinedNull(notExpected, actual);
242
+ if (comparison === undefined && this.areEqual(notExpected, actual))
243
+ {
244
+ comparison = Comparison.Equal;
245
+ }
246
+
247
+ if (comparison === Comparison.Equal)
248
+ {
249
+ throw this.createError({
250
+ expected: `not ${this.toValueString(notExpected)}`,
251
+ actual: this.toValueString(actual),
252
+ expression: expression,
253
+ message: message,
254
+ });
255
+ }
256
+ }
257
+
258
+ public assertNotEmpty<T extends JavascriptIterable<unknown> | string | null | undefined>(value: T, expression?: string, message?: string): asserts value is NonNullable<T>
259
+ {
260
+ this.assertNotUndefinedAndNotNull(value, expression, message);
261
+ if ((isString(value) && value.length === 0) ||
262
+ (isJavascriptIterable(value) && !Iterable.create(value).any()))
263
+ {
264
+ throw this.createError({
265
+ expected: "not empty",
266
+ actual: this.toValueString(value),
267
+ expression: expression,
268
+ message: message,
269
+ });
270
+ }
271
+ }
272
+
273
+ public assertLessThan(value: number, upperBound: number, expression?: string, message?: string): void
274
+ {
275
+ if (!(value < upperBound))
276
+ {
277
+ throw this.createError({
278
+ expected: `less than ${this.toValueString(upperBound)}`,
279
+ actual: this.toValueString(value),
280
+ expression: expression,
281
+ message: message,
282
+ });
283
+ }
284
+ }
285
+
286
+ public assertLessThanOrEqualTo(value: number, upperBound: number, expression?: string, message?: string): void
287
+ {
288
+ if (!(value <= upperBound))
289
+ {
290
+ throw this.createError({
291
+ expected: `less than or equal to ${this.toValueString(upperBound)}`,
292
+ actual: this.toValueString(value),
293
+ expression: expression,
294
+ message: message,
295
+ });
296
+ }
297
+ }
298
+
299
+ public assertGreaterThanOrEqualTo(value: number, lowerBound: number, expression?: string, message?: string): void
300
+ {
301
+ if (!(lowerBound <= value))
302
+ {
303
+ throw this.createError({
304
+ expected: `greater than or equal to ${this.toValueString(lowerBound)}`,
305
+ actual: this.toValueString(value),
306
+ expression: expression,
307
+ message: message,
308
+ });
309
+ }
310
+ }
311
+
312
+ public assertGreaterThan(value: number, lowerBound: number, expression?: string, message?: string): void
313
+ {
314
+ if (!(lowerBound < value))
315
+ {
316
+ throw this.createError({
317
+ expected: `greater than ${this.toValueString(lowerBound)}`,
318
+ actual: this.toValueString(value),
319
+ expression: expression,
320
+ message: message,
321
+ });
322
+ }
323
+ }
324
+
325
+ public assertBetween(lowerBound: number, value: number, upperBound: number, expression?: string, message?: string): void
326
+ {
327
+ this.assertLessThanOrEqualTo(lowerBound, upperBound, "lowerBound");
328
+ if (isUndefinedOrNull(value) || !(lowerBound <= value && value <= upperBound))
329
+ {
330
+ throw this.createError({
331
+ expected: (lowerBound === upperBound
332
+ ? this.toValueString(lowerBound)
333
+ : `between ${this.toValueString(lowerBound)} and ${this.toValueString(upperBound)}`),
334
+ actual: this.toValueString(value),
335
+ expression: expression,
336
+ message: message,
337
+ });
338
+ }
339
+ }
340
+
341
+ public assertAccessIndex(index: number, count: number, expression?: string, message?: string): void
342
+ {
343
+ this.assertGreaterThanOrEqualTo(count, 1, "count");
344
+ this.assertInteger(index, expression);
345
+ this.assertBetween(0, index, count - 1, expression, message);
346
+ }
347
+
348
+ public assertInsertIndex(index: number, count: number, expression?: string, message?: string): void
349
+ {
350
+ this.assertGreaterThanOrEqualTo(count, 0, "count");
351
+ this.assertInteger(index, expression);
352
+ this.assertBetween(0, index, count, expression, message);
353
+ }
354
+
355
+ public assertOneOf<T>(possibilities: JavascriptIterable<T>, value: T, expression?: string, message?: string): void
356
+ {
357
+ this.assertNotUndefinedAndNotNull(possibilities, "possibilities");
358
+
359
+ let found: boolean = false;
360
+ for (const possibility of possibilities)
361
+ {
362
+ if (this.areEqual(possibility, value))
363
+ {
364
+ found = true;
365
+ break;
366
+ }
367
+ }
368
+
369
+ if (!found)
370
+ {
371
+ throw this.createError({
372
+ expected: `one of ${this.toValueString(possibilities)}`,
373
+ actual: this.toValueString(value),
374
+ expression: expression,
375
+ message: message,
376
+ });
377
+ }
378
+ }
379
+
380
+ public assertByte(value: number, expression?: string, message?: string): void
381
+ {
382
+ this.assertBetween(Bytes.minimumValue, value, Bytes.maximumValue, expression, message);
383
+ this.assertInteger(value, "value");
384
+ }
385
+
386
+ public assertInteger(value: number, expression?: string, message?: string): void
387
+ {
388
+ if (value % 1 !== 0)
389
+ {
390
+ throw this.createError({
391
+ expected: `integer`,
392
+ actual: this.toValueString(value),
393
+ expression: expression,
394
+ message: message,
395
+ });
396
+ }
397
+ }
398
+
399
+ public assertCharacter(value: string, expression?: string, message?: string): void
400
+ {
401
+ if (!value || value.length !== 1)
402
+ {
403
+ throw this.createError({
404
+ expected: `character`,
405
+ actual: this.toValueString(value),
406
+ expression: expression,
407
+ message: message,
408
+ });
409
+ }
410
+ }
411
+
412
+ public assertInstanceOf<T>(value: unknown, type: Type<T>, typeCheck?: (value: unknown) => value is T, expression?: string, message?: string): asserts value is T;
413
+ public assertInstanceOf<T>(parameters: { value: unknown, type: Type<T>, typeCheck?: (value: unknown) => value is T, expression?: string, message?: string }): void;
414
+ public assertInstanceOf<T>(parametersOrValue: unknown | { value: unknown, type: Type<T>, typeCheck?: (value: unknown) => value is T, expression?: string, message?: string }, type?: Type<T>, typeCheck?: (value: unknown) => value is T, expression?: string, message?: string): void;
415
+ public assertInstanceOf<T>(parametersOrValue: unknown | { value: unknown, type: Type<T>, typeCheck?: (value: unknown) => value is T, expression?: string, message?: string }, type?: Type<T>, typeCheck?: (value: unknown) => value is T, expression?: string, message?: string): void
416
+ {
417
+ let value: unknown;
418
+ if (hasProperty(parametersOrValue, "value") &&
419
+ hasProperty(parametersOrValue, "type"))
420
+ {
421
+ value = parametersOrValue.value;
422
+ type = parametersOrValue.type as Type<T>;
423
+ if (hasFunction(parametersOrValue, "typeCheck"))
424
+ {
425
+ typeCheck = parametersOrValue.typeCheck as (value: unknown) => value is T;
426
+ }
427
+ if (hasProperty(parametersOrValue, "expression"))
428
+ {
429
+ expression = parametersOrValue.expression as string;
430
+ }
431
+ if (hasProperty(parametersOrValue, "message"))
432
+ {
433
+ message = parametersOrValue.message as string;
434
+ }
435
+ }
436
+ else
437
+ {
438
+ value = parametersOrValue;
439
+ }
440
+
441
+ if (!instanceOf(value, type!, typeCheck))
442
+ {
443
+ throw this.createError({
444
+ expected: `instance of ${type!.name}`,
445
+ actual: this.toValueString(value),
446
+ expression: expression,
447
+ message: message,
448
+ });
449
+ }
450
+ }
451
+ }
@@ -0,0 +1,204 @@
1
+ import { EqualFunctions } from "./equalFunctions";
2
+ import { HttpHeader } from "./httpHeader";
3
+ import { HttpHeaders } from "./httpHeaders";
4
+ import { Iterable } from "./iterable";
5
+ import { Iterator } from "./iterator";
6
+ import { JavascriptIterable, JavascriptIterator } from "./javascript";
7
+ import { List } from "./list";
8
+ import { NotFoundError } from "./notFoundError";
9
+ import { PreCondition } from "./preCondition";
10
+ import { escapeAndQuote } from "./strings";
11
+ import { SyncResult } from "./syncResult";
12
+ import { ToStringFunctions } from "./toStringFunctions";
13
+ import { isString, Type } from "./types";
14
+
15
+ export class MutableHttpHeaders implements HttpHeaders
16
+ {
17
+ private readonly headers: List<HttpHeader>;
18
+
19
+ private constructor(headers?: JavascriptIterable<HttpHeader>)
20
+ {
21
+ this.headers = List.create();
22
+ if (headers)
23
+ {
24
+ this.setAll(headers);
25
+ }
26
+ }
27
+
28
+ public static create(headers?: JavascriptIterable<HttpHeader>): MutableHttpHeaders
29
+ {
30
+ return new MutableHttpHeaders(headers);
31
+ }
32
+
33
+ public iterate(): Iterator<HttpHeader>
34
+ {
35
+ return this.headers.iterate();
36
+ }
37
+
38
+ public get(headerName: string): SyncResult<HttpHeader>
39
+ {
40
+ PreCondition.assertNotEmpty(headerName, "headerName");
41
+
42
+ return SyncResult.create(() =>
43
+ {
44
+ let result: HttpHeader | undefined;
45
+
46
+ const lowerHeaderName: string = headerName.toLowerCase();
47
+ for (const header of this.headers)
48
+ {
49
+ if (header.getName().toLowerCase() === lowerHeaderName)
50
+ {
51
+ result = header
52
+ break;
53
+ }
54
+ }
55
+ if (result === undefined)
56
+ {
57
+ throw new NotFoundError(`No HttpHeader found with the name ${escapeAndQuote(headerName)}.`);
58
+ }
59
+
60
+ return result;
61
+ });
62
+ }
63
+
64
+ public getValue(headerName: string): SyncResult<string>
65
+ {
66
+ return this.get(headerName)
67
+ .then((header: HttpHeader) => header.getValue());
68
+ }
69
+
70
+ /**
71
+ * Set a {@link HttpHeader} value in this collection. This will overwrite any existing
72
+ * {@link HttpHeader} with the same name.
73
+ * @param headerName The name of the header to set.
74
+ * @param headerValue The value of the header to set.
75
+ */
76
+ public set(headerName: string, headerValue: string): this;
77
+ public set(header: HttpHeader): this;
78
+ set(headerOrHeaderName: HttpHeader | string, headerValue?: string): this
79
+ {
80
+ let headerName: string;
81
+ if (isString(headerOrHeaderName))
82
+ {
83
+ headerName = headerOrHeaderName;
84
+ }
85
+ else
86
+ {
87
+ headerName = headerOrHeaderName.getName();
88
+ headerValue = headerOrHeaderName.getValue();
89
+ }
90
+ PreCondition.assertNotEmpty(headerName, "headerName");
91
+ PreCondition.assertNotUndefinedAndNotNull(headerValue, "headerValue");
92
+
93
+ let insertIndex: number = 0;
94
+ for (let insertIndex = 0; insertIndex < this.headers.getCount().await(); insertIndex++)
95
+ {
96
+ const header: HttpHeader = this.headers.get(insertIndex).await();
97
+ if (header.getName() === headerName)
98
+ {
99
+ this.headers.removeAt(insertIndex);
100
+ break;
101
+ }
102
+ }
103
+ this.headers.insert(insertIndex, HttpHeader.create(headerName, headerValue));
104
+
105
+ return this;
106
+ }
107
+
108
+ public setAll(headers: JavascriptIterable<HttpHeader>): this
109
+ {
110
+ PreCondition.assertNotUndefinedAndNotNull(headers, "headers");
111
+
112
+ for (const header of headers)
113
+ {
114
+ this.set(header);
115
+ }
116
+
117
+ return this;
118
+ }
119
+
120
+ public setContentType(contentType: string): this
121
+ {
122
+ return this.set(HttpHeaders.contentTypeHeaderName, contentType);
123
+ }
124
+
125
+ public getContentType(): SyncResult<HttpHeader>
126
+ {
127
+ return this.get(HttpHeaders.contentTypeHeaderName);
128
+ }
129
+
130
+ public getContentTypeValue(): SyncResult<string>
131
+ {
132
+ return this.getValue(HttpHeaders.contentTypeHeaderName);
133
+ }
134
+
135
+ public getCount(): SyncResult<number>
136
+ {
137
+ return this.headers.getCount();
138
+ }
139
+
140
+ public toArray(): SyncResult<HttpHeader[]>
141
+ {
142
+ return HttpHeaders.toArray(this);
143
+ }
144
+
145
+ public any(): SyncResult<boolean>
146
+ {
147
+ return HttpHeaders.any(this);
148
+ }
149
+
150
+ public equals(right: JavascriptIterable<HttpHeader>, equalFunctions?: EqualFunctions): SyncResult<boolean>
151
+ {
152
+ return HttpHeaders.equals(this, right, equalFunctions);
153
+ }
154
+
155
+ public toString(toStringFunctions?: ToStringFunctions): string
156
+ {
157
+ return HttpHeaders.toString(this, toStringFunctions);
158
+ }
159
+
160
+ public concatenate(...toConcatenate: JavascriptIterable<HttpHeader>[]): Iterable<HttpHeader>
161
+ {
162
+ return HttpHeaders.concatenate(this, ...toConcatenate);
163
+ }
164
+
165
+ public map<TOutput>(mapping: (value: HttpHeader) => (TOutput | SyncResult<TOutput>)): Iterable<TOutput>
166
+ {
167
+ return HttpHeaders.map(this, mapping);
168
+ }
169
+
170
+ public flatMap<TOutput>(mapping: (value: HttpHeader) => JavascriptIterable<TOutput>): Iterable<TOutput>
171
+ {
172
+ return HttpHeaders.flatMap(this, mapping);
173
+ }
174
+
175
+ public where(condition: (value: HttpHeader) => (boolean | SyncResult<boolean>)): Iterable<HttpHeader>
176
+ {
177
+ return HttpHeaders.where(this, condition);
178
+ }
179
+
180
+ public instanceOf<TOutput extends HttpHeader>(typeOrTypeCheck: Type<TOutput> | ((value: HttpHeader) => value is TOutput)): Iterable<TOutput>
181
+ {
182
+ return HttpHeaders.instanceOf(this, typeOrTypeCheck);
183
+ }
184
+
185
+ public first(condition?: (value: HttpHeader) => (boolean | SyncResult<boolean>)): SyncResult<HttpHeader>
186
+ {
187
+ return HttpHeaders.first(this, condition);
188
+ }
189
+
190
+ public last(condition?: (value: HttpHeader) => (boolean | SyncResult<boolean>)): SyncResult<HttpHeader>
191
+ {
192
+ return HttpHeaders.last(this, condition);
193
+ }
194
+
195
+ public [Symbol.iterator](): JavascriptIterator<HttpHeader>
196
+ {
197
+ return HttpHeaders[Symbol.iterator](this);
198
+ }
199
+
200
+ public contains(value: HttpHeader, equalFunctions?: EqualFunctions): SyncResult<boolean>
201
+ {
202
+ return HttpHeaders.contains(this, value, equalFunctions);
203
+ }
204
+ }