@fgv/ts-utils 5.0.0-21 → 5.0.0-23

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 (80) hide show
  1. package/dist/ts-utils.d.ts +307 -33
  2. package/lib/index.d.ts +2 -1
  3. package/lib/index.js +3 -1
  4. package/lib/packlets/base/index.d.ts +0 -2
  5. package/lib/packlets/base/index.js +1 -25
  6. package/lib/packlets/base/result.d.ts +49 -0
  7. package/lib/packlets/base/result.js +36 -0
  8. package/lib/packlets/collections/collector.d.ts +9 -0
  9. package/lib/packlets/collections/collector.js +6 -0
  10. package/lib/packlets/collections/validatingCollector.d.ts +5 -1
  11. package/lib/packlets/hash/hashingNormalizer.js +4 -3
  12. package/lib/packlets/logging/index.d.ts +3 -0
  13. package/{src/packlets/hash/index.ts → lib/packlets/logging/index.js} +19 -3
  14. package/lib/packlets/logging/logReporter.d.ts +69 -0
  15. package/lib/packlets/logging/logReporter.js +72 -0
  16. package/lib/packlets/logging/logger.d.ts +183 -0
  17. package/lib/packlets/logging/logger.js +237 -0
  18. package/lib/test/unit/consoleLogger.test.d.ts +2 -0
  19. package/lib/test/unit/hashingNormalizerCollisions.test.d.ts +2 -0
  20. package/package.json +1 -1
  21. package/lib/packlets/base/logger.d.ts +0 -52
  22. package/lib/packlets/base/logger.js +0 -128
  23. package/src/index.ts +0 -63
  24. package/src/packlets/base/brand.ts +0 -28
  25. package/src/packlets/base/index.ts +0 -31
  26. package/src/packlets/base/logger.ts +0 -156
  27. package/src/packlets/base/mapResults.ts +0 -302
  28. package/src/packlets/base/messageAggregator.ts +0 -120
  29. package/src/packlets/base/normalize.ts +0 -144
  30. package/src/packlets/base/result.ts +0 -961
  31. package/src/packlets/base/utils.ts +0 -236
  32. package/src/packlets/collections/collectible.ts +0 -238
  33. package/src/packlets/collections/collector.ts +0 -273
  34. package/src/packlets/collections/collectorValidator.ts +0 -178
  35. package/src/packlets/collections/common.ts +0 -27
  36. package/src/packlets/collections/convertingCollector.ts +0 -223
  37. package/src/packlets/collections/convertingCollectorValidator.ts +0 -164
  38. package/src/packlets/collections/index.ts +0 -39
  39. package/src/packlets/collections/keyValueConverters.ts +0 -142
  40. package/src/packlets/collections/readonlyResultMap.ts +0 -99
  41. package/src/packlets/collections/resultMap.ts +0 -322
  42. package/src/packlets/collections/resultMapValidator.ts +0 -188
  43. package/src/packlets/collections/utils.ts +0 -33
  44. package/src/packlets/collections/validatingCollector.ts +0 -123
  45. package/src/packlets/collections/validatingConvertingCollector.ts +0 -113
  46. package/src/packlets/collections/validatingResultMap.ts +0 -94
  47. package/src/packlets/conversion/baseConverter.ts +0 -330
  48. package/src/packlets/conversion/converter.ts +0 -260
  49. package/src/packlets/conversion/converters.ts +0 -1039
  50. package/src/packlets/conversion/defaultingConverter.ts +0 -188
  51. package/src/packlets/conversion/index.ts +0 -30
  52. package/src/packlets/conversion/objectConverter.ts +0 -259
  53. package/src/packlets/conversion/stringConverter.ts +0 -165
  54. package/src/packlets/file-tree/directoryItem.ts +0 -84
  55. package/src/packlets/file-tree/fileItem.ts +0 -119
  56. package/src/packlets/file-tree/fileTree.ts +0 -152
  57. package/src/packlets/file-tree/fileTreeAccessors.ts +0 -176
  58. package/src/packlets/file-tree/fsTree.ts +0 -118
  59. package/src/packlets/file-tree/in-memory/inMemoryTree.ts +0 -171
  60. package/src/packlets/file-tree/in-memory/index.ts +0 -23
  61. package/src/packlets/file-tree/in-memory/treeBuilder.ts +0 -203
  62. package/src/packlets/file-tree/index.ts +0 -31
  63. package/src/packlets/hash/crcNormalizer.ts +0 -81
  64. package/src/packlets/hash/hashingNormalizer.ts +0 -102
  65. package/src/packlets/validation/array.ts +0 -84
  66. package/src/packlets/validation/boolean.ts +0 -63
  67. package/src/packlets/validation/classes.ts +0 -38
  68. package/src/packlets/validation/common.ts +0 -28
  69. package/src/packlets/validation/field.ts +0 -99
  70. package/src/packlets/validation/genericValidator.ts +0 -204
  71. package/src/packlets/validation/index.ts +0 -31
  72. package/src/packlets/validation/number.ts +0 -66
  73. package/src/packlets/validation/object.ts +0 -208
  74. package/src/packlets/validation/oneOf.ts +0 -78
  75. package/src/packlets/validation/string.ts +0 -66
  76. package/src/packlets/validation/traits.ts +0 -113
  77. package/src/packlets/validation/typeGuard.ts +0 -83
  78. package/src/packlets/validation/validator.ts +0 -157
  79. package/src/packlets/validation/validatorBase.ts +0 -66
  80. package/src/packlets/validation/validators.ts +0 -254
@@ -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
- }