@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.
Files changed (187) hide show
  1. package/CHANGELOG.json +0 -21
  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/base/index.ts → lib/packlets/logging/index.js} +19 -10
  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/package.json +6 -6
  19. package/.vscode/launch.json +0 -16
  20. package/.vscode/settings.json +0 -36
  21. package/dist/ts-utils.d.ts +0 -4832
  22. package/dist/tsdoc-metadata.json +0 -11
  23. package/lib/packlets/base/logger.d.ts +0 -52
  24. package/lib/packlets/base/logger.js +0 -128
  25. package/lib/test/helpers/jest/helpers/fsHelpers.d.ts +0 -52
  26. package/lib/test/helpers/jest/helpers/fsHelpers.js +0 -180
  27. package/lib/test/helpers/jest/helpers/index.d.ts +0 -2
  28. package/lib/test/helpers/jest/helpers/index.js +0 -18
  29. package/lib/test/helpers/jest/index.d.ts +0 -3
  30. package/lib/test/helpers/jest/index.js +0 -27
  31. package/lib/test/helpers/jest/matchers/index.d.ts +0 -16
  32. package/lib/test/helpers/jest/matchers/index.js +0 -19
  33. package/lib/test/helpers/jest/matchers/toFail/index.d.ts +0 -16
  34. package/lib/test/helpers/jest/matchers/toFail/index.js +0 -25
  35. package/lib/test/helpers/jest/matchers/toFail/predicate.d.ts +0 -4
  36. package/lib/test/helpers/jest/matchers/toFail/predicate.js +0 -9
  37. package/lib/test/helpers/jest/matchers/toFailTest/index.d.ts +0 -16
  38. package/lib/test/helpers/jest/matchers/toFailTest/index.js +0 -30
  39. package/lib/test/helpers/jest/matchers/toFailTest/predicate.d.ts +0 -3
  40. package/lib/test/helpers/jest/matchers/toFailTest/predicate.js +0 -12
  41. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.d.ts +0 -17
  42. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +0 -26
  43. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.d.ts +0 -4
  44. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +0 -15
  45. package/lib/test/helpers/jest/matchers/toFailTestWith/index.d.ts +0 -18
  46. package/lib/test/helpers/jest/matchers/toFailTestWith/index.js +0 -31
  47. package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.d.ts +0 -4
  48. package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.js +0 -30
  49. package/lib/test/helpers/jest/matchers/toFailWith/index.d.ts +0 -18
  50. package/lib/test/helpers/jest/matchers/toFailWith/index.js +0 -29
  51. package/lib/test/helpers/jest/matchers/toFailWith/predicate.d.ts +0 -4
  52. package/lib/test/helpers/jest/matchers/toFailWith/predicate.js +0 -20
  53. package/lib/test/helpers/jest/matchers/toFailWithDetail/index.d.ts +0 -19
  54. package/lib/test/helpers/jest/matchers/toFailWithDetail/index.js +0 -29
  55. package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.d.ts +0 -4
  56. package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.js +0 -26
  57. package/lib/test/helpers/jest/matchers/toSucceed/index.d.ts +0 -16
  58. package/lib/test/helpers/jest/matchers/toSucceed/index.js +0 -25
  59. package/lib/test/helpers/jest/matchers/toSucceed/predicate.d.ts +0 -4
  60. package/lib/test/helpers/jest/matchers/toSucceed/predicate.js +0 -9
  61. package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts +0 -17
  62. package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +0 -26
  63. package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.d.ts +0 -17
  64. package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +0 -26
  65. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.d.ts +0 -19
  66. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +0 -55
  67. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.d.ts +0 -4
  68. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +0 -21
  69. package/lib/test/helpers/jest/matchers/toSucceedWith/index.d.ts +0 -18
  70. package/lib/test/helpers/jest/matchers/toSucceedWith/index.js +0 -29
  71. package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.d.ts +0 -4
  72. package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.js +0 -16
  73. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.d.ts +0 -18
  74. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.js +0 -29
  75. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.d.ts +0 -4
  76. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +0 -21
  77. package/lib/test/helpers/jest/resolvers/cli.d.ts +0 -5
  78. package/lib/test/helpers/jest/resolvers/cli.js +0 -48
  79. package/lib/test/helpers/jest/resolvers/ide.d.ts +0 -5
  80. package/lib/test/helpers/jest/resolvers/ide.js +0 -48
  81. package/lib/test/helpers/jest/ts-utils.d.ts +0 -2
  82. package/lib/test/helpers/jest/ts-utils.js +0 -18
  83. package/lib/test/helpers/jest/types/index.d.ts +0 -79
  84. package/lib/test/helpers/jest/types/index.js +0 -4
  85. package/lib/test/helpers/jest/utils/matcherHelpers.d.ts +0 -6
  86. package/lib/test/helpers/jest/utils/matcherHelpers.js +0 -53
  87. package/lib/test/helpers/jest/utils/snapshotResolver.d.ts +0 -4
  88. package/lib/test/helpers/jest/utils/snapshotResolver.js +0 -19
  89. package/lib/test/unit/collections/collectible.test.d.ts +0 -2
  90. package/lib/test/unit/collections/collector.test.d.ts +0 -2
  91. package/lib/test/unit/collections/collectors.test.d.ts +0 -2
  92. package/lib/test/unit/collections/convertingCollector.test.d.ts +0 -2
  93. package/lib/test/unit/collections/helpers.d.ts +0 -44
  94. package/lib/test/unit/collections/helpers.js +0 -115
  95. package/lib/test/unit/collections/resultMap.test.d.ts +0 -2
  96. package/lib/test/unit/collections/validatingCollector.test.d.ts +0 -2
  97. package/lib/test/unit/collections/validatingConvertingCollector.test.d.ts +0 -2
  98. package/lib/test/unit/collections/validatingResultMap.test.d.ts +0 -2
  99. package/lib/test/unit/converter.test.d.ts +0 -2
  100. package/lib/test/unit/converters.test.d.ts +0 -2
  101. package/lib/test/unit/defaultingConverter.test.d.ts +0 -2
  102. package/lib/test/unit/file-tree/fileItem.test.d.ts +0 -2
  103. package/lib/test/unit/file-tree/fileTree.test.d.ts +0 -2
  104. package/lib/test/unit/file-tree/fsTree.test.d.ts +0 -2
  105. package/lib/test/unit/file-tree/inMemoryTree.test.d.ts +0 -2
  106. package/lib/test/unit/file-tree/sample/rootFile.json +0 -1
  107. package/lib/test/unit/file-tree/sample/some/other/path/file2.json +0 -1
  108. package/lib/test/unit/file-tree/sample/some/path/below/file4.json +0 -1
  109. package/lib/test/unit/file-tree/sample/some/path/file1.json +0 -1
  110. package/lib/test/unit/file-tree/sample/some/path/file3.json +0 -1
  111. package/lib/test/unit/hash.test.d.ts +0 -2
  112. package/lib/test/unit/logger.test.d.ts +0 -2
  113. package/lib/test/unit/mapResults.test.d.ts +0 -3
  114. package/lib/test/unit/messageAggregator.test.d.ts +0 -3
  115. package/lib/test/unit/normalize.test.d.ts +0 -2
  116. package/lib/test/unit/result.test.d.ts +0 -3
  117. package/lib/test/unit/utils.test.d.ts +0 -3
  118. package/lib/test/unit/validation/array.test.d.ts +0 -2
  119. package/lib/test/unit/validation/boolean.test.d.ts +0 -2
  120. package/lib/test/unit/validation/genericValidator.test.d.ts +0 -2
  121. package/lib/test/unit/validation/number.test.d.ts +0 -2
  122. package/lib/test/unit/validation/object.test.d.ts +0 -2
  123. package/lib/test/unit/validation/oneOf.test.d.ts +0 -2
  124. package/lib/test/unit/validation/recordOf.test.d.ts +0 -2
  125. package/lib/test/unit/validation/recursiveValidation.test.d.ts +0 -2
  126. package/lib/test/unit/validation/string.test.d.ts +0 -2
  127. package/lib/test/unit/validation/traits.test.d.ts +0 -2
  128. package/lib/test/unit/validation/typeGuard.test.d.ts +0 -2
  129. package/lib/test/unit/validation/validators.test.d.ts +0 -2
  130. package/src/index.ts +0 -63
  131. package/src/packlets/base/brand.ts +0 -28
  132. package/src/packlets/base/logger.ts +0 -156
  133. package/src/packlets/base/mapResults.ts +0 -302
  134. package/src/packlets/base/messageAggregator.ts +0 -120
  135. package/src/packlets/base/normalize.ts +0 -144
  136. package/src/packlets/base/result.ts +0 -961
  137. package/src/packlets/base/utils.ts +0 -236
  138. package/src/packlets/collections/collectible.ts +0 -238
  139. package/src/packlets/collections/collector.ts +0 -273
  140. package/src/packlets/collections/collectorValidator.ts +0 -178
  141. package/src/packlets/collections/common.ts +0 -27
  142. package/src/packlets/collections/convertingCollector.ts +0 -223
  143. package/src/packlets/collections/convertingCollectorValidator.ts +0 -164
  144. package/src/packlets/collections/index.ts +0 -39
  145. package/src/packlets/collections/keyValueConverters.ts +0 -142
  146. package/src/packlets/collections/readonlyResultMap.ts +0 -99
  147. package/src/packlets/collections/resultMap.ts +0 -322
  148. package/src/packlets/collections/resultMapValidator.ts +0 -188
  149. package/src/packlets/collections/utils.ts +0 -33
  150. package/src/packlets/collections/validatingCollector.ts +0 -123
  151. package/src/packlets/collections/validatingConvertingCollector.ts +0 -113
  152. package/src/packlets/collections/validatingResultMap.ts +0 -94
  153. package/src/packlets/conversion/baseConverter.ts +0 -330
  154. package/src/packlets/conversion/converter.ts +0 -260
  155. package/src/packlets/conversion/converters.ts +0 -1039
  156. package/src/packlets/conversion/defaultingConverter.ts +0 -188
  157. package/src/packlets/conversion/index.ts +0 -30
  158. package/src/packlets/conversion/objectConverter.ts +0 -259
  159. package/src/packlets/conversion/stringConverter.ts +0 -165
  160. package/src/packlets/file-tree/directoryItem.ts +0 -84
  161. package/src/packlets/file-tree/fileItem.ts +0 -119
  162. package/src/packlets/file-tree/fileTree.ts +0 -152
  163. package/src/packlets/file-tree/fileTreeAccessors.ts +0 -176
  164. package/src/packlets/file-tree/fsTree.ts +0 -118
  165. package/src/packlets/file-tree/in-memory/inMemoryTree.ts +0 -171
  166. package/src/packlets/file-tree/in-memory/index.ts +0 -23
  167. package/src/packlets/file-tree/in-memory/treeBuilder.ts +0 -203
  168. package/src/packlets/file-tree/index.ts +0 -31
  169. package/src/packlets/hash/crcNormalizer.ts +0 -81
  170. package/src/packlets/hash/hashingNormalizer.ts +0 -102
  171. package/src/packlets/hash/index.ts +0 -24
  172. package/src/packlets/validation/array.ts +0 -84
  173. package/src/packlets/validation/boolean.ts +0 -63
  174. package/src/packlets/validation/classes.ts +0 -38
  175. package/src/packlets/validation/common.ts +0 -28
  176. package/src/packlets/validation/field.ts +0 -99
  177. package/src/packlets/validation/genericValidator.ts +0 -204
  178. package/src/packlets/validation/index.ts +0 -31
  179. package/src/packlets/validation/number.ts +0 -66
  180. package/src/packlets/validation/object.ts +0 -208
  181. package/src/packlets/validation/oneOf.ts +0 -78
  182. package/src/packlets/validation/string.ts +0 -66
  183. package/src/packlets/validation/traits.ts +0 -113
  184. package/src/packlets/validation/typeGuard.ts +0 -83
  185. package/src/packlets/validation/validator.ts +0 -157
  186. package/src/packlets/validation/validatorBase.ts +0 -66
  187. package/src/packlets/validation/validators.ts +0 -254
@@ -1,171 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 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 { captureResult, fail, Result, succeed } from '../../base';
24
- import { DirectoryItem } from '../directoryItem';
25
- import { FileItem } from '../fileItem';
26
- import { FileTreeItem, IFileTreeAccessors } from '../fileTreeAccessors';
27
- import { InMemoryDirectory, InMemoryFile, TreeBuilder } from './treeBuilder';
28
-
29
- /**
30
- * Represents a single file in an in-memory {@link FileTree.FileTree | file tree}.
31
- * @public
32
- */
33
- export interface IInMemoryFile {
34
- /**
35
- * The absolute path of the file in the tree.
36
- */
37
- readonly path: string;
38
-
39
- /**
40
- * The contents of the file
41
- */
42
- readonly contents: unknown;
43
- }
44
-
45
- /**
46
- * Implementation of {@link FileTree.IFileTreeAccessors | IFileTreeAccessors} that uses an in-memory
47
- * tree to access files and directories.
48
- * @public
49
- */
50
- export class InMemoryTreeAccessors implements IFileTreeAccessors {
51
- private readonly _tree: TreeBuilder;
52
-
53
- /**
54
- * Protected constructor for derived classes.
55
- * @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
56
- * @param prefix - Optional prefix for the tree.
57
- * @public
58
- */
59
- protected constructor(files: IInMemoryFile[], prefix?: string) {
60
- this._tree = TreeBuilder.create(prefix).orThrow();
61
- for (const file of files) {
62
- this._tree.addFile(file.path, file.contents).orThrow();
63
- }
64
- }
65
-
66
- /**
67
- * Creates a new {@link FileTree.InMemoryTreeAccessors | InMemoryTreeAccessors} instance with the supplied
68
- * in-memory files.
69
- * @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
70
- * @param prefix - Optional prefix for the tree.
71
- */
72
- public static create(files: IInMemoryFile[], prefix?: string): Result<InMemoryTreeAccessors> {
73
- return captureResult(() => new InMemoryTreeAccessors(files, prefix));
74
- }
75
-
76
- /**
77
- * {@inheritDoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
78
- */
79
- public resolveAbsolutePath(...paths: string[]): string {
80
- const parts = paths[0].startsWith('/') ? paths : [this._tree.prefix, ...paths];
81
- const joined = parts.flatMap((p) => p.split('/').filter((s) => s.length > 0)).join('/');
82
- return `/${joined}`;
83
- }
84
-
85
- /**
86
- * {@inheritdoc FileTree.IFileTreeAccessors.getExtension}
87
- */
88
- public getExtension(path: string): string {
89
- const parts = path.split('.');
90
- if (parts.length === 1) {
91
- return '';
92
- }
93
- return `.${parts.pop()}`;
94
- }
95
-
96
- /**
97
- * {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}
98
- */
99
- public getBaseName(path: string, suffix?: string): string {
100
- /* c8 ignore next 1 - ?? is defense in depth should never happen */
101
- const base = path.split('/').pop() ?? '';
102
- if (suffix && base.endsWith(suffix)) {
103
- return base.slice(0, base.length - suffix.length);
104
- }
105
- return base;
106
- }
107
-
108
- /**
109
- * {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}
110
- */
111
- public joinPaths(...paths: string[]): string {
112
- return paths.join('/');
113
- }
114
-
115
- /**
116
- * {@inheritDoc FileTree.IFileTreeAccessors.getItem}
117
- */
118
- public getItem(itemPath: string): Result<FileTreeItem> {
119
- const existing = this._tree.byAbsolutePath.get(itemPath);
120
- if (existing) {
121
- if (existing instanceof InMemoryFile) {
122
- return FileItem.create(existing.absolutePath, this);
123
- } else if (existing instanceof InMemoryDirectory) {
124
- return DirectoryItem.create(existing.absolutePath, this);
125
- }
126
- }
127
- return fail(`${itemPath}: not found`);
128
- }
129
-
130
- /**
131
- * {@inheritDoc FileTree.IFileTreeAccessors.getFileContents}
132
- */
133
- public getFileContents(path: string): Result<string> {
134
- const item = this._tree.byAbsolutePath.get(path);
135
- if (item === undefined) {
136
- return fail(`${path}: not found`);
137
- }
138
- if (!(item instanceof InMemoryFile)) {
139
- return fail(`${path}: not a file`);
140
- }
141
- // if the body is a string we don't want to add quotes
142
- if (typeof item.contents === 'string') {
143
- return succeed(item.contents);
144
- }
145
- return captureResult(() => JSON.stringify(item.contents));
146
- }
147
-
148
- /**
149
- * {@inheritDoc FileTree.IFileTreeAccessors.getChildren}
150
- */
151
- public getChildren(path: string): Result<ReadonlyArray<FileTreeItem>> {
152
- const item = this._tree.byAbsolutePath.get(path);
153
- if (item === undefined) {
154
- return fail(`${path}: not found`);
155
- }
156
- if (!(item instanceof InMemoryDirectory)) {
157
- return fail(`${path}: not a directory`);
158
- }
159
- return captureResult(() => {
160
- const children: FileTreeItem[] = [];
161
- for (const child of item.children.values()) {
162
- if (child instanceof InMemoryFile) {
163
- children.push(FileItem.create(child.absolutePath, this).orThrow());
164
- } else if (child instanceof InMemoryDirectory) {
165
- children.push(DirectoryItem.create(child.absolutePath, this).orThrow());
166
- }
167
- }
168
- return children;
169
- });
170
- }
171
- }
@@ -1,23 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 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
- export * from './inMemoryTree';
@@ -1,203 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 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 { Result, captureResult, fail, succeed } from '../../base';
24
-
25
- /**
26
- * Represents a file in an in-memory file tree.
27
- * @public
28
- */
29
- export class InMemoryFile {
30
- /**
31
- * The absolute path of the file.
32
- */
33
- public readonly absolutePath: string;
34
- /**
35
- * The contents of the file.
36
- */
37
- public readonly contents: unknown;
38
-
39
- /**
40
- * Creates a new {@link InMemoryFile | InMemoryFile} instance.
41
- * @param absolutePath - The absolute path of the file.
42
- * @param contents - The contents of the file.
43
- */
44
- public constructor(absolutePath: string, contents: unknown) {
45
- this.absolutePath = absolutePath;
46
- this.contents = contents;
47
- }
48
- }
49
-
50
- /**
51
- * Represents a directory in an in-memory file tree.
52
- * @public
53
- */
54
- export class InMemoryDirectory {
55
- /**
56
- * The absolute path of the directory.
57
- */
58
- public readonly absolutePath: string;
59
- protected _children: Map<string, InMemoryDirectory | InMemoryFile>;
60
-
61
- /**
62
- * The children of the directory.
63
- */
64
- public get children(): ReadonlyMap<string, InMemoryDirectory | InMemoryFile> {
65
- return this._children;
66
- }
67
-
68
- /**
69
- * Creates an empty new {@link InMemoryDirectory | InMemoryDirectory} instance.
70
- * @param absolutePath - The absolute path of the directory.
71
- */
72
- public constructor(absolutePath: string) {
73
- this.absolutePath = absolutePath;
74
- this._children = new Map<string, InMemoryDirectory | InMemoryFile>();
75
- }
76
-
77
- /**
78
- * Gets or adds a child directory with the specified name.
79
- * @param name - The name of the child directory.
80
- * @returns {@link Success | Success} with the child directory if successful, or
81
- * {@link Failure | Failure} with an error message otherwise.
82
- */
83
- public getOrAddDirectory(name: string): Result<InMemoryDirectory> {
84
- const existing = this._children.get(name);
85
- if (existing) {
86
- if (existing instanceof InMemoryDirectory) {
87
- return succeed(existing);
88
- }
89
- return fail(`${name}: not a directory`);
90
- }
91
- const child = new InMemoryDirectory(this.getChildPath(name));
92
- this._children.set(name, child);
93
- return succeed(child);
94
- }
95
-
96
- /**
97
- * Adds a file to the directory.
98
- * @param name - The name of the file.
99
- * @param contents - The contents of the file.
100
- * @returns {@link Success | Success} with the new file if successful, or
101
- * {@link Failure | Failure} with an error message otherwise.
102
- */
103
- public addFile(name: string, contents: unknown): Result<InMemoryFile> {
104
- if (this._children.has(name)) {
105
- return fail(`${name}: already exists`);
106
- }
107
- const child = new InMemoryFile(this.getChildPath(name), contents);
108
- this._children.set(name, child);
109
- return succeed(child);
110
- }
111
-
112
- /**
113
- * Gets the absolute path for a child of this directory with the supplied
114
- * name.
115
- * @param name - The name of the child.
116
- * @returns {@link Success | Success} with the absolute path if successful, or
117
- * {@link Failure | Failure} with an error message otherwise.
118
- */
119
- public getChildPath(name: string): string {
120
- if (this.absolutePath === '/') {
121
- return `/${name}`;
122
- }
123
- return [this.absolutePath, name].join('/');
124
- }
125
- }
126
-
127
- /**
128
- * Helper class to build an in-memory file tree.
129
- * @public
130
- */
131
- export class TreeBuilder {
132
- /**
133
- * The prefix for all paths in the tree.
134
- */
135
- public readonly prefix: string;
136
-
137
- /**
138
- * The root directory of the tree.
139
- */
140
- public readonly root: InMemoryDirectory;
141
-
142
- /**
143
- * A map of all directories and files in the tree by absolute path.
144
- */
145
- public readonly byAbsolutePath: Map<string, InMemoryDirectory | InMemoryFile>;
146
-
147
- /**
148
- * Protected constructor for derived classes.
149
- * @param prefix - The prefix for all paths in the tree.
150
- * @public
151
- */
152
- protected constructor(prefix?: string) {
153
- this.prefix = prefix ?? '/';
154
- if (!this.prefix.startsWith('/')) {
155
- throw new Error(`${prefix}: not an absolute path`);
156
- }
157
- this.root = new InMemoryDirectory(this.prefix);
158
- this.byAbsolutePath = new Map<string, InMemoryDirectory | InMemoryFile>();
159
- this.byAbsolutePath.set(this.prefix, this.root);
160
- }
161
-
162
- /**
163
- * Creates a new {@link TreeBuilder | TreeBuilder} instance.
164
- * @param prefix - The prefix for all paths in the tree.
165
- * @returns {@link Success | Success} with the new {@link TreeBuilder | TreeBuilder} instance if successful,
166
- * or {@link Failure | Failure} with an error message otherwise.
167
- * @public
168
- */
169
- public static create(prefix?: string): Result<TreeBuilder> {
170
- return captureResult(() => new TreeBuilder(prefix));
171
- }
172
-
173
- /**
174
- * Adds a file to the tree.
175
- * @param absolutePath - The absolute path of the file.
176
- * @param contents - The contents of the file.
177
- * @returns {@link Success | Success} with the new file if successful, or
178
- * {@link Failure | Failure} with an error message otherwise.
179
- * @public
180
- */
181
- public addFile(absolutePath: string, contents: unknown): Result<InMemoryFile> {
182
- const parts = absolutePath.split('/').filter((p) => p.length > 0);
183
- if (parts.length === 0) {
184
- return fail(`${absolutePath}: invalid file path`);
185
- }
186
- let dir = this.root;
187
- while (parts.length > 1) {
188
- const part = parts.shift()!;
189
- const result = dir.getOrAddDirectory(part);
190
- if (result.isFailure()) {
191
- return fail(result.message);
192
- }
193
- dir = result.value;
194
- if (!this.byAbsolutePath.has(dir.absolutePath)) {
195
- this.byAbsolutePath.set(dir.absolutePath, dir);
196
- }
197
- }
198
- return dir.addFile(parts[0], contents).onSuccess((file) => {
199
- this.byAbsolutePath.set(file.absolutePath, file);
200
- return succeed(file);
201
- });
202
- }
203
- }
@@ -1,31 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 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 { IInMemoryFile, InMemoryTreeAccessors } from './in-memory';
24
-
25
- export * from './directoryItem';
26
- export * from './fileItem';
27
- export * from './fileTreeAccessors';
28
- export * from './fsTree';
29
- export * from './fileTree';
30
-
31
- export { IInMemoryFile, InMemoryTreeAccessors };
@@ -1,81 +0,0 @@
1
- /*
2
- * Copyright (c) 2023 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 { HashingNormalizer } from './hashingNormalizer';
24
-
25
- const textEncoder: TextEncoder = new TextEncoder();
26
-
27
- const POLYNOMIAL: number = 0xedb88320;
28
-
29
- const crc32Table: number[] = [];
30
-
31
- function crc32(bytes: Uint8Array, crc: number = 0xffffffff): number {
32
- if (crc32Table.length === 0) {
33
- buildCrc32Table();
34
- }
35
- for (let i = 0; i < bytes.length; ++i) {
36
- // eslint-disable-next-line no-bitwise
37
- crc = crc32Table[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
38
- }
39
- return (crc ^ -1) >>> 0;
40
- }
41
-
42
- const toUInt32 = (n: number): number => {
43
- if (n >= 0) {
44
- return n;
45
- }
46
- return 0xffffffff - n * -1 + 1;
47
- };
48
-
49
- function buildCrc32Table(): void {
50
- if (crc32Table.length === 0) {
51
- for (let n = 0; n < 256; n++) {
52
- let r = n;
53
- for (let i = 0; i < 8; i++) {
54
- // eslint-disable-next-line no-bitwise
55
- if (r & 1) {
56
- r = (r >>> 1) ^ POLYNOMIAL;
57
- } else {
58
- r = r >>> 1;
59
- }
60
- }
61
- crc32Table.push(toUInt32(r));
62
- }
63
- }
64
- }
65
-
66
- /**
67
- * A {@link Hash.HashingNormalizer | hashing normalizer} which computes object
68
- * hash using the CRC32 algorithm.
69
- * @public
70
- */
71
- export class Crc32Normalizer extends HashingNormalizer {
72
- public constructor() {
73
- super(Crc32Normalizer.crc32Hash);
74
- }
75
-
76
- public static crc32Hash(parts: string[]): string {
77
- return crc32(textEncoder.encode(parts.join('|')))
78
- .toString(16)
79
- .padStart(8, '0');
80
- }
81
- }
@@ -1,102 +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 { Normalizer, Result, captureResult, fail, mapResults, succeed } from '../base';
24
-
25
- /**
26
- * Function to compute a hash from a pre-normalized array of strings.
27
- * @public
28
- */
29
- export type HashFunction = (parts: string[]) => string;
30
-
31
- /**
32
- * Normalizes an arbitrary JSON object
33
- * @public
34
- */
35
- export class HashingNormalizer extends Normalizer {
36
- private _hash: HashFunction;
37
-
38
- public constructor(hash: HashFunction) {
39
- super();
40
- this._hash = hash;
41
- }
42
-
43
- public computeHash(from: unknown): Result<string> {
44
- switch (typeof from) {
45
- case 'string':
46
- case 'bigint':
47
- case 'boolean':
48
- case 'number':
49
- case 'symbol':
50
- case 'undefined':
51
- return this._normalizeLiteralToString(from).onSuccess((v) => {
52
- return captureResult(() => this._hash([v]));
53
- });
54
- case 'object':
55
- if (from === null || from instanceof Date || from instanceof RegExp) {
56
- return this._normalizeLiteralToString(from).onSuccess((v) => {
57
- return captureResult(() => this._hash([v]));
58
- });
59
- } else if (Array.isArray(from)) {
60
- return mapResults(from.map((e) => this.computeHash(e))).onSuccess((a) => {
61
- return captureResult(() => this._hash(a));
62
- });
63
- } else if (from instanceof Map || from instanceof Set) {
64
- return this.computeHash(this.normalizeEntries(from.entries()));
65
- }
66
- return this.computeHash(this.normalizeEntries(Object.entries(from)));
67
- }
68
- return fail(`computeHash: Unexpected type - cannot hash '${typeof from}'`);
69
- }
70
-
71
- /**
72
- * Constructs a normalized string representation of some literal value.
73
- * @param from - The literal value to be normalized.
74
- * @returns A normalized string representation of the literal.
75
- * @internal
76
- */
77
- protected _normalizeLiteralToString(
78
- from: string | number | bigint | boolean | symbol | undefined | Date | RegExp | null
79
- ): Result<string> {
80
- switch (typeof from) {
81
- case 'string':
82
- return succeed(from);
83
- case 'bigint':
84
- case 'boolean':
85
- case 'number':
86
- case 'symbol':
87
- case 'undefined':
88
- return succeed(`${typeof from}:[[[${String(from)}]]]`);
89
- }
90
- if (from === null) {
91
- return succeed('object:[[[null]]');
92
- }
93
- if (from instanceof Date) {
94
- return succeed(`Date:[[[${String(from.valueOf())}]]]`);
95
- }
96
- if (from instanceof RegExp) {
97
- return succeed(`RegExp:[[[${from.toString()}]]]`);
98
- }
99
- /* c8 ignore next 2 */
100
- return fail(`cannot normalize ${JSON.stringify(from)}`);
101
- }
102
- }
@@ -1,24 +0,0 @@
1
- /*
2
- * Copyright (c) 2020 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
- export * from './crcNormalizer';
24
- export * from './hashingNormalizer';