@fgv/ts-utils 5.0.0-22 → 5.0.0-24
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/CHANGELOG.json +0 -21
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -1
- package/lib/packlets/base/index.d.ts +0 -2
- package/lib/packlets/base/index.js +1 -25
- package/lib/packlets/base/result.d.ts +49 -0
- package/lib/packlets/base/result.js +36 -0
- package/lib/packlets/collections/collector.d.ts +9 -0
- package/lib/packlets/collections/collector.js +6 -0
- package/lib/packlets/collections/validatingCollector.d.ts +5 -1
- package/lib/packlets/hash/hashingNormalizer.js +4 -3
- package/lib/packlets/logging/index.d.ts +3 -0
- package/{src/packlets/base/index.ts → lib/packlets/logging/index.js} +19 -10
- package/lib/packlets/logging/logReporter.d.ts +69 -0
- package/lib/packlets/logging/logReporter.js +72 -0
- package/lib/packlets/logging/logger.d.ts +183 -0
- package/lib/packlets/logging/logger.js +237 -0
- package/package.json +6 -6
- package/.vscode/launch.json +0 -16
- package/.vscode/settings.json +0 -36
- package/dist/ts-utils.d.ts +0 -4832
- package/dist/tsdoc-metadata.json +0 -11
- package/lib/packlets/base/logger.d.ts +0 -52
- package/lib/packlets/base/logger.js +0 -128
- package/lib/test/helpers/jest/helpers/fsHelpers.d.ts +0 -52
- package/lib/test/helpers/jest/helpers/fsHelpers.js +0 -180
- package/lib/test/helpers/jest/helpers/index.d.ts +0 -2
- package/lib/test/helpers/jest/helpers/index.js +0 -18
- package/lib/test/helpers/jest/index.d.ts +0 -3
- package/lib/test/helpers/jest/index.js +0 -27
- package/lib/test/helpers/jest/matchers/index.d.ts +0 -16
- package/lib/test/helpers/jest/matchers/index.js +0 -19
- package/lib/test/helpers/jest/matchers/toFail/index.d.ts +0 -16
- package/lib/test/helpers/jest/matchers/toFail/index.js +0 -25
- package/lib/test/helpers/jest/matchers/toFail/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toFail/predicate.js +0 -9
- package/lib/test/helpers/jest/matchers/toFailTest/index.d.ts +0 -16
- package/lib/test/helpers/jest/matchers/toFailTest/index.js +0 -30
- package/lib/test/helpers/jest/matchers/toFailTest/predicate.d.ts +0 -3
- package/lib/test/helpers/jest/matchers/toFailTest/predicate.js +0 -12
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.d.ts +0 -17
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +0 -26
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +0 -15
- package/lib/test/helpers/jest/matchers/toFailTestWith/index.d.ts +0 -18
- package/lib/test/helpers/jest/matchers/toFailTestWith/index.js +0 -31
- package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.js +0 -30
- package/lib/test/helpers/jest/matchers/toFailWith/index.d.ts +0 -18
- package/lib/test/helpers/jest/matchers/toFailWith/index.js +0 -29
- package/lib/test/helpers/jest/matchers/toFailWith/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toFailWith/predicate.js +0 -20
- package/lib/test/helpers/jest/matchers/toFailWithDetail/index.d.ts +0 -19
- package/lib/test/helpers/jest/matchers/toFailWithDetail/index.js +0 -29
- package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.js +0 -26
- package/lib/test/helpers/jest/matchers/toSucceed/index.d.ts +0 -16
- package/lib/test/helpers/jest/matchers/toSucceed/index.js +0 -25
- package/lib/test/helpers/jest/matchers/toSucceed/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toSucceed/predicate.js +0 -9
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts +0 -17
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +0 -26
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.d.ts +0 -17
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +0 -26
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.d.ts +0 -19
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +0 -55
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +0 -21
- package/lib/test/helpers/jest/matchers/toSucceedWith/index.d.ts +0 -18
- package/lib/test/helpers/jest/matchers/toSucceedWith/index.js +0 -29
- package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.js +0 -16
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.d.ts +0 -18
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.js +0 -29
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.d.ts +0 -4
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +0 -21
- package/lib/test/helpers/jest/resolvers/cli.d.ts +0 -5
- package/lib/test/helpers/jest/resolvers/cli.js +0 -48
- package/lib/test/helpers/jest/resolvers/ide.d.ts +0 -5
- package/lib/test/helpers/jest/resolvers/ide.js +0 -48
- package/lib/test/helpers/jest/ts-utils.d.ts +0 -2
- package/lib/test/helpers/jest/ts-utils.js +0 -18
- package/lib/test/helpers/jest/types/index.d.ts +0 -79
- package/lib/test/helpers/jest/types/index.js +0 -4
- package/lib/test/helpers/jest/utils/matcherHelpers.d.ts +0 -6
- package/lib/test/helpers/jest/utils/matcherHelpers.js +0 -53
- package/lib/test/helpers/jest/utils/snapshotResolver.d.ts +0 -4
- package/lib/test/helpers/jest/utils/snapshotResolver.js +0 -19
- package/lib/test/unit/collections/collectible.test.d.ts +0 -2
- package/lib/test/unit/collections/collector.test.d.ts +0 -2
- package/lib/test/unit/collections/collectors.test.d.ts +0 -2
- package/lib/test/unit/collections/convertingCollector.test.d.ts +0 -2
- package/lib/test/unit/collections/helpers.d.ts +0 -44
- package/lib/test/unit/collections/helpers.js +0 -115
- package/lib/test/unit/collections/resultMap.test.d.ts +0 -2
- package/lib/test/unit/collections/validatingCollector.test.d.ts +0 -2
- package/lib/test/unit/collections/validatingConvertingCollector.test.d.ts +0 -2
- package/lib/test/unit/collections/validatingResultMap.test.d.ts +0 -2
- package/lib/test/unit/converter.test.d.ts +0 -2
- package/lib/test/unit/converters.test.d.ts +0 -2
- package/lib/test/unit/defaultingConverter.test.d.ts +0 -2
- package/lib/test/unit/file-tree/fileItem.test.d.ts +0 -2
- package/lib/test/unit/file-tree/fileTree.test.d.ts +0 -2
- package/lib/test/unit/file-tree/fsTree.test.d.ts +0 -2
- package/lib/test/unit/file-tree/inMemoryTree.test.d.ts +0 -2
- package/lib/test/unit/file-tree/sample/rootFile.json +0 -1
- package/lib/test/unit/file-tree/sample/some/other/path/file2.json +0 -1
- package/lib/test/unit/file-tree/sample/some/path/below/file4.json +0 -1
- package/lib/test/unit/file-tree/sample/some/path/file1.json +0 -1
- package/lib/test/unit/file-tree/sample/some/path/file3.json +0 -1
- package/lib/test/unit/hash.test.d.ts +0 -2
- package/lib/test/unit/logger.test.d.ts +0 -2
- package/lib/test/unit/mapResults.test.d.ts +0 -3
- package/lib/test/unit/messageAggregator.test.d.ts +0 -3
- package/lib/test/unit/normalize.test.d.ts +0 -2
- package/lib/test/unit/result.test.d.ts +0 -3
- package/lib/test/unit/utils.test.d.ts +0 -3
- package/lib/test/unit/validation/array.test.d.ts +0 -2
- package/lib/test/unit/validation/boolean.test.d.ts +0 -2
- package/lib/test/unit/validation/genericValidator.test.d.ts +0 -2
- package/lib/test/unit/validation/number.test.d.ts +0 -2
- package/lib/test/unit/validation/object.test.d.ts +0 -2
- package/lib/test/unit/validation/oneOf.test.d.ts +0 -2
- package/lib/test/unit/validation/recordOf.test.d.ts +0 -2
- package/lib/test/unit/validation/recursiveValidation.test.d.ts +0 -2
- package/lib/test/unit/validation/string.test.d.ts +0 -2
- package/lib/test/unit/validation/traits.test.d.ts +0 -2
- package/lib/test/unit/validation/typeGuard.test.d.ts +0 -2
- package/lib/test/unit/validation/validators.test.d.ts +0 -2
- package/src/index.ts +0 -63
- package/src/packlets/base/brand.ts +0 -28
- package/src/packlets/base/logger.ts +0 -156
- package/src/packlets/base/mapResults.ts +0 -302
- package/src/packlets/base/messageAggregator.ts +0 -120
- package/src/packlets/base/normalize.ts +0 -144
- package/src/packlets/base/result.ts +0 -961
- package/src/packlets/base/utils.ts +0 -236
- package/src/packlets/collections/collectible.ts +0 -238
- package/src/packlets/collections/collector.ts +0 -273
- package/src/packlets/collections/collectorValidator.ts +0 -178
- package/src/packlets/collections/common.ts +0 -27
- package/src/packlets/collections/convertingCollector.ts +0 -223
- package/src/packlets/collections/convertingCollectorValidator.ts +0 -164
- package/src/packlets/collections/index.ts +0 -39
- package/src/packlets/collections/keyValueConverters.ts +0 -142
- package/src/packlets/collections/readonlyResultMap.ts +0 -99
- package/src/packlets/collections/resultMap.ts +0 -322
- package/src/packlets/collections/resultMapValidator.ts +0 -188
- package/src/packlets/collections/utils.ts +0 -33
- package/src/packlets/collections/validatingCollector.ts +0 -123
- package/src/packlets/collections/validatingConvertingCollector.ts +0 -113
- package/src/packlets/collections/validatingResultMap.ts +0 -94
- package/src/packlets/conversion/baseConverter.ts +0 -330
- package/src/packlets/conversion/converter.ts +0 -260
- package/src/packlets/conversion/converters.ts +0 -1039
- package/src/packlets/conversion/defaultingConverter.ts +0 -188
- package/src/packlets/conversion/index.ts +0 -30
- package/src/packlets/conversion/objectConverter.ts +0 -259
- package/src/packlets/conversion/stringConverter.ts +0 -165
- package/src/packlets/file-tree/directoryItem.ts +0 -84
- package/src/packlets/file-tree/fileItem.ts +0 -119
- package/src/packlets/file-tree/fileTree.ts +0 -152
- package/src/packlets/file-tree/fileTreeAccessors.ts +0 -176
- package/src/packlets/file-tree/fsTree.ts +0 -118
- package/src/packlets/file-tree/in-memory/inMemoryTree.ts +0 -171
- package/src/packlets/file-tree/in-memory/index.ts +0 -23
- package/src/packlets/file-tree/in-memory/treeBuilder.ts +0 -203
- package/src/packlets/file-tree/index.ts +0 -31
- package/src/packlets/hash/crcNormalizer.ts +0 -81
- package/src/packlets/hash/hashingNormalizer.ts +0 -102
- package/src/packlets/hash/index.ts +0 -24
- package/src/packlets/validation/array.ts +0 -84
- package/src/packlets/validation/boolean.ts +0 -63
- package/src/packlets/validation/classes.ts +0 -38
- package/src/packlets/validation/common.ts +0 -28
- package/src/packlets/validation/field.ts +0 -99
- package/src/packlets/validation/genericValidator.ts +0 -204
- package/src/packlets/validation/index.ts +0 -31
- package/src/packlets/validation/number.ts +0 -66
- package/src/packlets/validation/object.ts +0 -208
- package/src/packlets/validation/oneOf.ts +0 -78
- package/src/packlets/validation/string.ts +0 -66
- package/src/packlets/validation/traits.ts +0 -113
- package/src/packlets/validation/typeGuard.ts +0 -83
- package/src/packlets/validation/validator.ts +0 -157
- package/src/packlets/validation/validatorBase.ts +0 -66
- package/src/packlets/validation/validators.ts +0 -254
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2024 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { IMessageAggregator, Result, fail } from './result';
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* A simple error aggregator to simplify collecting and reporting all errors in
|
|
27
|
-
* a flow.
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
export class MessageAggregator implements IMessageAggregator {
|
|
31
|
-
private readonly _messages: string[];
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Constructs a new {@link MessageAggregator | ErrorAggregator} with an
|
|
35
|
-
* optionally specified initial set of error messages.
|
|
36
|
-
* @param errors - optional array of errors to be included
|
|
37
|
-
* in the aggregation.
|
|
38
|
-
*/
|
|
39
|
-
public constructor(errors?: string[]) {
|
|
40
|
-
this._messages = errors ? Array.from(errors) : [];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* {@inheritdoc IMessageAggregator.hasMessages}
|
|
45
|
-
*/
|
|
46
|
-
public get hasMessages(): boolean {
|
|
47
|
-
return this._messages.length > 0;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* {@inheritdoc IMessageAggregator.numMessages}
|
|
52
|
-
*/
|
|
53
|
-
public get numMessages(): number {
|
|
54
|
-
return this._messages.length;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* {@inheritdoc IMessageAggregator.messages}
|
|
59
|
-
*/
|
|
60
|
-
public get messages(): string[] {
|
|
61
|
-
return this._messages;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* {@inheritdoc IMessageAggregator.addMessage}
|
|
66
|
-
*/
|
|
67
|
-
public addMessage(message: string | undefined): this {
|
|
68
|
-
if (message) {
|
|
69
|
-
this._messages.push(message);
|
|
70
|
-
}
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* {@inheritdoc IMessageAggregator.addMessages}
|
|
76
|
-
*/
|
|
77
|
-
public addMessages(messages: string[] | undefined): this {
|
|
78
|
-
if (messages && messages.length > 0) {
|
|
79
|
-
this._messages.push(...messages);
|
|
80
|
-
}
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* {@inheritdoc IMessageAggregator.toString}
|
|
86
|
-
*/
|
|
87
|
-
public toString(separator?: string): string {
|
|
88
|
-
return this._messages.join(separator ?? '\n');
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* If any error messages have been aggregated, returns
|
|
93
|
-
* {@link Failure | Failure<T>} with the aggregated
|
|
94
|
-
* messages concatenated using the optionally-supplied
|
|
95
|
-
* separator, or newline. If the supplied {@link Result | Result<T>}
|
|
96
|
-
* contains an error message that has not already been aggregated,
|
|
97
|
-
* it will be included in the aggregated messages.
|
|
98
|
-
*
|
|
99
|
-
* If no error messages have been aggregated, returns
|
|
100
|
-
* the supplied {@link Result | Result<T>}.
|
|
101
|
-
* @param result - The {@link Result | Result<T>} to be returned
|
|
102
|
-
* if no messages have been aggregated.
|
|
103
|
-
* @param separator - Optional string separator used to construct
|
|
104
|
-
* the error message.
|
|
105
|
-
* @returns {@link Failure | Failure<T>} with an aggregated message
|
|
106
|
-
* if any error messages were collected, the supplied
|
|
107
|
-
* {@link Result | Result<T>} otherwise.
|
|
108
|
-
*/
|
|
109
|
-
public returnOrReport<T>(result: Result<T>, separator?: string): Result<T> {
|
|
110
|
-
if (!this.hasMessages) {
|
|
111
|
-
return result;
|
|
112
|
-
}
|
|
113
|
-
if (!result.success) {
|
|
114
|
-
if (!this._messages.find((s) => s === result.message)) {
|
|
115
|
-
return fail([...this._messages, result.message].join(separator ?? '\n'));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return fail(this.toString(separator));
|
|
119
|
-
}
|
|
120
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2021 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { mapResults } from './mapResults';
|
|
24
|
-
import { Result, fail, succeed } from './result';
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @internal
|
|
28
|
-
*/
|
|
29
|
-
export type Entry<T> = [string | number | symbol, T];
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @internal
|
|
33
|
-
*/
|
|
34
|
-
export type ResultEntry<T> = [string | number | symbol, Result<T>];
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Normalizes an arbitrary JSON object
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
export class Normalizer {
|
|
41
|
-
/**
|
|
42
|
-
* Normalizes the supplied value
|
|
43
|
-
*
|
|
44
|
-
* @param from - The value to be normalized
|
|
45
|
-
* @returns A normalized version of the value
|
|
46
|
-
*/
|
|
47
|
-
public normalize<T>(from: T): Result<T> {
|
|
48
|
-
switch (typeof from) {
|
|
49
|
-
case 'string':
|
|
50
|
-
case 'bigint':
|
|
51
|
-
case 'boolean':
|
|
52
|
-
case 'number':
|
|
53
|
-
case 'symbol':
|
|
54
|
-
case 'undefined':
|
|
55
|
-
return this.normalizeLiteral(from);
|
|
56
|
-
case 'object':
|
|
57
|
-
if (from === null || from instanceof Date || from instanceof RegExp) {
|
|
58
|
-
return this.normalizeLiteral(from);
|
|
59
|
-
} else if (Array.isArray(from)) {
|
|
60
|
-
return this._normalizeArray(from) as unknown as Result<T>;
|
|
61
|
-
} else if (from instanceof Map) {
|
|
62
|
-
return succeed(new Map(this.normalizeEntries(from.entries())) as unknown as T);
|
|
63
|
-
} else if (from instanceof Set) {
|
|
64
|
-
return succeed(new Set(this.normalizeEntries(from.entries())) as unknown as T);
|
|
65
|
-
}
|
|
66
|
-
const obj: { [key in number | string | symbol]: unknown } = {};
|
|
67
|
-
for (const e of this.normalizeEntries(Object.entries(from as unknown as object))) {
|
|
68
|
-
obj[e[0]] = e[1];
|
|
69
|
-
}
|
|
70
|
-
return succeed(obj as T);
|
|
71
|
-
}
|
|
72
|
-
return fail(`normalize: Unexpected type - cannot normalize '${typeof from}'`);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Compares two property names from some object being normalized.
|
|
77
|
-
* @param k1 - First key to be compared.
|
|
78
|
-
* @param k2 - Second key to be compared.
|
|
79
|
-
* @returns `1` if `k1` is greater, `-1` if `k2` is greater and
|
|
80
|
-
* `0` if they are equal.
|
|
81
|
-
* @internal
|
|
82
|
-
*/
|
|
83
|
-
protected _compareKeys(k1: unknown, k2: unknown): number {
|
|
84
|
-
const cs1 = String(k1);
|
|
85
|
-
const cs2 = String(k2);
|
|
86
|
-
if (cs1 > cs2) {
|
|
87
|
-
return 1;
|
|
88
|
-
}
|
|
89
|
-
if (cs2 > cs1) {
|
|
90
|
-
return -1;
|
|
91
|
-
}
|
|
92
|
-
/* c8 ignore next 2 */
|
|
93
|
-
return 0;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Normalizes an array of object property entries (e.g. as returned by `Object.entries()`).
|
|
98
|
-
* @remarks
|
|
99
|
-
* Converts property names (entry key) to string and then sorts as string.
|
|
100
|
-
* @param entries - The entries to be normalized.
|
|
101
|
-
* @returns A normalized sorted array of entries.
|
|
102
|
-
*/
|
|
103
|
-
public normalizeEntries<T = unknown>(entries: Iterable<Entry<T>>): Entry<T>[] {
|
|
104
|
-
return Array.from(entries)
|
|
105
|
-
.sort((e1, e2) => this._compareKeys(e1[0], e2[0]))
|
|
106
|
-
.map((e) => [e[0], this.normalize(e[1])] as ResultEntry<T>)
|
|
107
|
-
.filter((e) => e[1].isSuccess())
|
|
108
|
-
.map((e) => [e[0], e[1].orThrow()]);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
protected _normalizeArray(from: unknown[]): Result<unknown[]> {
|
|
112
|
-
return mapResults(from.map((v) => this.normalize(v)));
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Normalizes the supplied literal value
|
|
117
|
-
* @param from - The literal value to be normalized.
|
|
118
|
-
* @returns A normalized value for the literal.
|
|
119
|
-
*/
|
|
120
|
-
public normalizeLiteral<T>(from: T): Result<T> {
|
|
121
|
-
// TODO: Apply configurable normalization rules
|
|
122
|
-
switch (typeof from) {
|
|
123
|
-
case 'string':
|
|
124
|
-
return succeed(from);
|
|
125
|
-
case 'bigint':
|
|
126
|
-
case 'boolean':
|
|
127
|
-
case 'number':
|
|
128
|
-
case 'symbol':
|
|
129
|
-
case 'undefined':
|
|
130
|
-
return succeed(from);
|
|
131
|
-
}
|
|
132
|
-
if (from === null) {
|
|
133
|
-
return succeed(from);
|
|
134
|
-
}
|
|
135
|
-
if (from instanceof Date) {
|
|
136
|
-
return succeed(from);
|
|
137
|
-
}
|
|
138
|
-
if (from instanceof RegExp) {
|
|
139
|
-
return succeed(from);
|
|
140
|
-
}
|
|
141
|
-
/* c8 ignore next 2 */
|
|
142
|
-
return fail(`cannot normalize ${JSON.stringify(from)}`);
|
|
143
|
-
}
|
|
144
|
-
}
|