@fgv/ts-extras 2.1.1-alpha.1 → 2.1.1-alpha.3

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.
@@ -1,15 +1,8 @@
1
1
  import { Converter } from '@fgv/ts-utils';
2
2
  import { Converters as Converters_2 } from '@fgv/ts-utils';
3
+ import { Hash as Hash_2 } from '@fgv/ts-utils';
3
4
  import { Result } from '@fgv/ts-utils';
4
5
 
5
- /**
6
- * Computes an md5 hash from an array of strings. Not secure and not intended to be secure.
7
- * @param parts - The strings to be hashed
8
- * @returns An md5 hash of the parts
9
- * @public
10
- */
11
- declare function computeHash(parts: string[]): string;
12
-
13
6
  declare namespace Converters {
14
7
  export {
15
8
  extendedArrayOf,
@@ -199,8 +192,7 @@ declare interface Formattable {
199
192
 
200
193
  declare namespace Hash {
201
194
  export {
202
- computeHash,
203
- Normalizer
195
+ Md5Normalizer
204
196
  }
205
197
  }
206
198
  export { Hash }
@@ -226,46 +218,13 @@ declare interface Formattable {
226
218
  }
227
219
 
228
220
  /**
229
- * Computes a normalized hash for an arbitrary javascript value.
221
+ * A {@link Hash.HashingNormalizer | hashing normalizer} which computes object
222
+ * hash using the MD5 algorithm.
230
223
  * @public
231
224
  */
232
- declare class Normalizer {
233
- /**
234
- * Computes a normalized md5 hash from an arbitrary supplied object. Not secure and not
235
- * intended to be secure. Also not fast and not intended to be fast.
236
- *
237
- * Normalization just sorts Maps, Sets and object keys by hash so that differences in order
238
- * do not affect the hash.
239
- *
240
- * @param from - The arbitrary `unknown` to be hashed.
241
- * @returns A normalized md5 hash for the supplied value.
242
- */
243
- computeHash(from: unknown): Result<string>;
244
- /**
245
- * Compares two property names from some object being normalized.
246
- * @param k1 - First key to be compared.
247
- * @param k2 - Second key to be compared.
248
- * @returns `1` if `k1` is greater, `-1` if `k2` is greater and
249
- * `0` if they are equal.
250
- * @internal
251
- */
252
- protected _compareKeys(k1: unknown, k2: unknown): number;
253
- /**
254
- * Normalizes an array of object property entries (e.g. as returned by `Object.entries()`).
255
- * @remarks
256
- * Converts property names (entry key) to string and then sorts as string.
257
- * @param entries - The entries to be normalized.
258
- * @returns A normalized sorted array of entries.
259
- * @internal
260
- */
261
- protected _normalizeEntries(entries: Iterable<[unknown, unknown]>): [unknown, unknown][];
262
- /**
263
- * Constructs a normalized string representation of some literal value.
264
- * @param from - The literal value to be normalized.
265
- * @returns A normalized string representation of the literal.
266
- * @internal
267
- */
268
- protected _normalizeLiteral(from: string | number | bigint | boolean | symbol | undefined | Date | RegExp | null): Result<string>;
225
+ declare class Md5Normalizer extends Hash_2.HashingNormalizer {
226
+ constructor();
227
+ static md5Hash(parts: string[]): string;
269
228
  }
270
229
 
271
230
  /**
@@ -1,2 +1,2 @@
1
- export * from './hash';
1
+ export * from './md5Normalizer';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/hash/index.ts"],"names":[],"mappings":"AAsBA,cAAc,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/hash/index.ts"],"names":[],"mappings":"AAsBA,cAAc,iBAAiB,CAAC"}
@@ -35,5 +35,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
35
35
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- __exportStar(require("./hash"), exports);
38
+ __exportStar(require("./md5Normalizer"), exports);
39
39
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/hash/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;AAEH,yCAAuB","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nexport * from './hash';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/hash/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;AAEH,kDAAgC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nexport * from './md5Normalizer';\n"]}
@@ -0,0 +1,11 @@
1
+ import { Hash } from '@fgv/ts-utils';
2
+ /**
3
+ * A {@link Hash.HashingNormalizer | hashing normalizer} which computes object
4
+ * hash using the MD5 algorithm.
5
+ * @public
6
+ */
7
+ export declare class Md5Normalizer extends Hash.HashingNormalizer {
8
+ constructor();
9
+ static md5Hash(parts: string[]): string;
10
+ }
11
+ //# sourceMappingURL=md5Normalizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"md5Normalizer.d.ts","sourceRoot":"","sources":["../../../src/packlets/hash/md5Normalizer.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAErC;;;;GAIG;AACH,qBAAa,aAAc,SAAQ,IAAI,CAAC,iBAAiB;;WAKzC,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM;CAG/C"}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2023 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ var desc = Object.getOwnPropertyDescriptor(m, k);
26
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
27
+ desc = { enumerable: true, get: function() { return m[k]; } };
28
+ }
29
+ Object.defineProperty(o, k2, desc);
30
+ }) : (function(o, m, k, k2) {
31
+ if (k2 === undefined) k2 = k;
32
+ o[k2] = m[k];
33
+ }));
34
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
35
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
36
+ }) : function(o, v) {
37
+ o["default"] = v;
38
+ });
39
+ var __importStar = (this && this.__importStar) || function (mod) {
40
+ if (mod && mod.__esModule) return mod;
41
+ var result = {};
42
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
43
+ __setModuleDefault(result, mod);
44
+ return result;
45
+ };
46
+ Object.defineProperty(exports, "__esModule", { value: true });
47
+ exports.Md5Normalizer = void 0;
48
+ const ts_utils_1 = require("@fgv/ts-utils");
49
+ const crypto = __importStar(require("crypto"));
50
+ /**
51
+ * A {@link Hash.HashingNormalizer | hashing normalizer} which computes object
52
+ * hash using the MD5 algorithm.
53
+ * @public
54
+ */
55
+ class Md5Normalizer extends ts_utils_1.Hash.HashingNormalizer {
56
+ constructor() {
57
+ super(Md5Normalizer.md5Hash);
58
+ }
59
+ static md5Hash(parts) {
60
+ return crypto.createHash('md5').update(parts.join('|'), 'utf8').digest('hex');
61
+ }
62
+ }
63
+ exports.Md5Normalizer = Md5Normalizer;
64
+ //# sourceMappingURL=md5Normalizer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"md5Normalizer.js","sourceRoot":"","sources":["../../../src/packlets/hash/md5Normalizer.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,4CAAqC;AACrC,+CAAiC;AACjC;;;;GAIG;AACH,MAAa,aAAc,SAAQ,eAAI,CAAC,iBAAiB;IACvD;QACE,KAAK,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAC/B,CAAC;IAEM,MAAM,CAAC,OAAO,CAAC,KAAe;QACnC,OAAO,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAChF,CAAC;CACF;AARD,sCAQC","sourcesContent":["/*\n * Copyright (c) 2023 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Hash } from '@fgv/ts-utils';\nimport * as crypto from 'crypto';\n/**\n * A {@link Hash.HashingNormalizer | hashing normalizer} which computes object\n * hash using the MD5 algorithm.\n * @public\n */\nexport class Md5Normalizer extends Hash.HashingNormalizer {\n public constructor() {\n super(Md5Normalizer.md5Hash);\n }\n\n public static md5Hash(parts: string[]): string {\n return crypto.createHash('md5').update(parts.join('|'), 'utf8').digest('hex');\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-extras",
3
- "version": "2.1.1-alpha.1",
3
+ "version": "2.1.1-alpha.3",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-extras.d.ts",
@@ -46,8 +46,8 @@
46
46
  "@types/heft-jest": "1.0.6",
47
47
  "@rushstack/heft-jest-plugin": "~0.11.0",
48
48
  "eslint-plugin-tsdoc": "~0.2.17",
49
- "@fgv/ts-utils-jest": "2.1.1-alpha.1",
50
- "@fgv/ts-utils": "2.1.1-alpha.1"
49
+ "@fgv/ts-utils-jest": "2.1.1-alpha.3",
50
+ "@fgv/ts-utils": "2.1.1-alpha.3"
51
51
  },
52
52
  "dependencies": {
53
53
  "luxon": "^3.4.4",
@@ -55,7 +55,7 @@
55
55
  "papaparse": "^5.4.1"
56
56
  },
57
57
  "peerDependencies": {
58
- "@fgv/ts-utils": "2.1.1-alpha.1"
58
+ "@fgv/ts-utils": "2.1.1-alpha.3"
59
59
  },
60
60
  "scripts": {
61
61
  "build": "heft test --clean",
@@ -1,51 +0,0 @@
1
- import { Result } from '@fgv/ts-utils';
2
- /**
3
- * Computes an md5 hash from an array of strings. Not secure and not intended to be secure.
4
- * @param parts - The strings to be hashed
5
- * @returns An md5 hash of the parts
6
- * @public
7
- */
8
- export declare function computeHash(parts: string[]): string;
9
- /**
10
- * Computes a normalized hash for an arbitrary javascript value.
11
- * @public
12
- */
13
- export declare class Normalizer {
14
- /**
15
- * Computes a normalized md5 hash from an arbitrary supplied object. Not secure and not
16
- * intended to be secure. Also not fast and not intended to be fast.
17
- *
18
- * Normalization just sorts Maps, Sets and object keys by hash so that differences in order
19
- * do not affect the hash.
20
- *
21
- * @param from - The arbitrary `unknown` to be hashed.
22
- * @returns A normalized md5 hash for the supplied value.
23
- */
24
- computeHash(from: unknown): Result<string>;
25
- /**
26
- * Compares two property names from some object being normalized.
27
- * @param k1 - First key to be compared.
28
- * @param k2 - Second key to be compared.
29
- * @returns `1` if `k1` is greater, `-1` if `k2` is greater and
30
- * `0` if they are equal.
31
- * @internal
32
- */
33
- protected _compareKeys(k1: unknown, k2: unknown): number;
34
- /**
35
- * Normalizes an array of object property entries (e.g. as returned by `Object.entries()`).
36
- * @remarks
37
- * Converts property names (entry key) to string and then sorts as string.
38
- * @param entries - The entries to be normalized.
39
- * @returns A normalized sorted array of entries.
40
- * @internal
41
- */
42
- protected _normalizeEntries(entries: Iterable<[unknown, unknown]>): [unknown, unknown][];
43
- /**
44
- * Constructs a normalized string representation of some literal value.
45
- * @param from - The literal value to be normalized.
46
- * @returns A normalized string representation of the literal.
47
- * @internal
48
- */
49
- protected _normalizeLiteral(from: string | number | bigint | boolean | symbol | undefined | Date | RegExp | null): Result<string>;
50
- }
51
- //# sourceMappingURL=hash.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../../src/packlets/hash/hash.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,MAAM,EAA4C,MAAM,eAAe,CAAC;AAEjF;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAEnD;AAED;;;GAGG;AACH,qBAAa,UAAU;IACrB;;;;;;;;;OASG;IACI,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC;IA4BjD;;;;;;;OAOG;IACH,SAAS,CAAC,YAAY,CAAC,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,GAAG,MAAM;IAaxD;;;;;;;OAOG;IACH,SAAS,CAAC,iBAAiB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE;IAIxF;;;;;OAKG;IACH,SAAS,CAAC,iBAAiB,CACzB,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,GACnF,MAAM,CAAC,MAAM,CAAC;CAuBlB"}
@@ -1,166 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2021 Erik Fortune
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining a copy
6
- * of this software and associated documentation files (the "Software"), to deal
7
- * in the Software without restriction, including without limitation the rights
8
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- * copies of the Software, and to permit persons to whom the Software is
10
- * furnished to do so, subject to the following conditions:
11
- *
12
- * The above copyright notice and this permission notice shall be included in all
13
- * copies or substantial portions of the Software.
14
- *
15
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- * SOFTWARE.
22
- */
23
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
24
- if (k2 === undefined) k2 = k;
25
- var desc = Object.getOwnPropertyDescriptor(m, k);
26
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
27
- desc = { enumerable: true, get: function() { return m[k]; } };
28
- }
29
- Object.defineProperty(o, k2, desc);
30
- }) : (function(o, m, k, k2) {
31
- if (k2 === undefined) k2 = k;
32
- o[k2] = m[k];
33
- }));
34
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
35
- Object.defineProperty(o, "default", { enumerable: true, value: v });
36
- }) : function(o, v) {
37
- o["default"] = v;
38
- });
39
- var __importStar = (this && this.__importStar) || function (mod) {
40
- if (mod && mod.__esModule) return mod;
41
- var result = {};
42
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
43
- __setModuleDefault(result, mod);
44
- return result;
45
- };
46
- Object.defineProperty(exports, "__esModule", { value: true });
47
- exports.Normalizer = exports.computeHash = void 0;
48
- const crypto = __importStar(require("crypto"));
49
- const ts_utils_1 = require("@fgv/ts-utils");
50
- /**
51
- * Computes an md5 hash from an array of strings. Not secure and not intended to be secure.
52
- * @param parts - The strings to be hashed
53
- * @returns An md5 hash of the parts
54
- * @public
55
- */
56
- function computeHash(parts) {
57
- return crypto.createHash('md5').update(parts.join('|'), 'utf8').digest('hex');
58
- }
59
- exports.computeHash = computeHash;
60
- /**
61
- * Computes a normalized hash for an arbitrary javascript value.
62
- * @public
63
- */
64
- class Normalizer {
65
- /**
66
- * Computes a normalized md5 hash from an arbitrary supplied object. Not secure and not
67
- * intended to be secure. Also not fast and not intended to be fast.
68
- *
69
- * Normalization just sorts Maps, Sets and object keys by hash so that differences in order
70
- * do not affect the hash.
71
- *
72
- * @param from - The arbitrary `unknown` to be hashed.
73
- * @returns A normalized md5 hash for the supplied value.
74
- */
75
- computeHash(from) {
76
- switch (typeof from) {
77
- case 'string':
78
- case 'bigint':
79
- case 'boolean':
80
- case 'number':
81
- case 'symbol':
82
- case 'undefined':
83
- return this._normalizeLiteral(from).onSuccess((v) => {
84
- return (0, ts_utils_1.captureResult)(() => computeHash([v]));
85
- });
86
- case 'object':
87
- if (from === null || from instanceof Date || from instanceof RegExp) {
88
- return this._normalizeLiteral(from).onSuccess((v) => {
89
- return (0, ts_utils_1.captureResult)(() => computeHash([v]));
90
- });
91
- }
92
- else if (Array.isArray(from)) {
93
- return (0, ts_utils_1.mapResults)(from.map((e) => this.computeHash(e))).onSuccess((a) => {
94
- return (0, ts_utils_1.captureResult)(() => computeHash(a));
95
- });
96
- }
97
- else if (from instanceof Map || from instanceof Set) {
98
- return this.computeHash(this._normalizeEntries(from.entries()));
99
- }
100
- return this.computeHash(this._normalizeEntries(Object.entries(from)));
101
- }
102
- return (0, ts_utils_1.fail)(`computeHash: Unexpected type - cannot hash '${typeof from}'`);
103
- }
104
- /**
105
- * Compares two property names from some object being normalized.
106
- * @param k1 - First key to be compared.
107
- * @param k2 - Second key to be compared.
108
- * @returns `1` if `k1` is greater, `-1` if `k2` is greater and
109
- * `0` if they are equal.
110
- * @internal
111
- */
112
- _compareKeys(k1, k2) {
113
- const cs1 = String(k1);
114
- const cs2 = String(k2);
115
- if (cs1 > cs2) {
116
- return 1;
117
- }
118
- if (cs2 > cs1) {
119
- return -1;
120
- }
121
- /* c8 ignore next 2 */
122
- return 0;
123
- }
124
- /**
125
- * Normalizes an array of object property entries (e.g. as returned by `Object.entries()`).
126
- * @remarks
127
- * Converts property names (entry key) to string and then sorts as string.
128
- * @param entries - The entries to be normalized.
129
- * @returns A normalized sorted array of entries.
130
- * @internal
131
- */
132
- _normalizeEntries(entries) {
133
- return Array.from(entries).sort((e1, e2) => this._compareKeys(e1[0], e2[0]));
134
- }
135
- /**
136
- * Constructs a normalized string representation of some literal value.
137
- * @param from - The literal value to be normalized.
138
- * @returns A normalized string representation of the literal.
139
- * @internal
140
- */
141
- _normalizeLiteral(from) {
142
- switch (typeof from) {
143
- case 'string':
144
- return (0, ts_utils_1.succeed)(from);
145
- case 'bigint':
146
- case 'boolean':
147
- case 'number':
148
- case 'symbol':
149
- case 'undefined':
150
- return (0, ts_utils_1.succeed)(`${typeof from}:[[[${String(from)}]]]`);
151
- }
152
- if (from === null) {
153
- return (0, ts_utils_1.succeed)('object:[[[null]]');
154
- }
155
- if (from instanceof Date) {
156
- return (0, ts_utils_1.succeed)(`Date:[[[${String(from.valueOf())}]]]`);
157
- }
158
- if (from instanceof RegExp) {
159
- return (0, ts_utils_1.succeed)(`RegExp:[[[${from.toString()}]]]`);
160
- }
161
- /* c8 ignore next 2 */
162
- return (0, ts_utils_1.fail)(`cannot normalize ${JSON.stringify(from)}`);
163
- }
164
- }
165
- exports.Normalizer = Normalizer;
166
- //# sourceMappingURL=hash.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hash.js","sourceRoot":"","sources":["../../../src/packlets/hash/hash.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,+CAAiC;AACjC,4CAAiF;AAEjF;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAe;IACzC,OAAO,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChF,CAAC;AAFD,kCAEC;AAED;;;GAGG;AACH,MAAa,UAAU;IACrB;;;;;;;;;OASG;IACI,WAAW,CAAC,IAAa;QAC9B,QAAQ,OAAO,IAAI,EAAE,CAAC;YACpB,KAAK,QAAQ,CAAC;YACd,KAAK,QAAQ,CAAC;YACd,KAAK,SAAS,CAAC;YACf,KAAK,QAAQ,CAAC;YACd,KAAK,QAAQ,CAAC;YACd,KA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I,EAAE,CAAC;YACzB,OAAO,IAAA,kBAAO,EAAC,WAAW,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;QACzD,CAAC;QACD,IAAI,IAAI,YAAY,MAAM,EAAE,CAAC;YAC3B,OAAO,IAAA,kBAAO,EAAC,aAAa,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QACpD,CAAC;QACD,sBAAsB;QACtB,OAAO,IAAA,eAAI,EAAC,oBAAoB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1D,CAAC;CACF;AAvGD,gCAuGC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport * as crypto from 'crypto';\nimport { Result, captureResult, fail, mapResults, succeed } from '@fgv/ts-utils';\n\n/**\n * Computes an md5 hash from an array of strings. Not secure and not intended to be secure.\n * @param parts - The strings to be hashed\n * @returns An md5 hash of the parts\n * @public\n */\nexport function computeHash(parts: string[]): string {\n return crypto.createHash('md5').update(parts.join('|'), 'utf8').digest('hex');\n}\n\n/**\n * Computes a normalized hash for an arbitrary javascript value.\n * @public\n */\nexport class Normalizer {\n /**\n * Computes a normalized md5 hash from an arbitrary supplied object. Not secure and not\n * intended to be secure. Also not fast and not intended to be fast.\n *\n * Normalization just sorts Maps, Sets and object keys by hash so that differences in order\n * do not affect the hash.\n *\n * @param from - The arbitrary `unknown` to be hashed.\n * @returns A normalized md5 hash for the supplied value.\n */\n public computeHash(from: unknown): Result<string> {\n switch (typeof from) {\n case 'string':\n case 'bigint':\n case 'boolean':\n case 'number':\n case 'symbol':\n case 'undefined':\n return this._normalizeLiteral(from).onSuccess((v) => {\n return captureResult(() => computeHash([v]));\n });\n case 'object':\n if (from === null || from instanceof Date || from instanceof RegExp) {\n return this._normalizeLiteral(from).onSuccess((v) => {\n return captureResult(() => computeHash([v]));\n });\n } else if (Array.isArray(from)) {\n return mapResults(from.map((e) => this.computeHash(e))).onSuccess((a) => {\n return captureResult(() => computeHash(a));\n });\n } else if (from instanceof Map || from instanceof Set) {\n return this.computeHash(this._normalizeEntries(from.entries()));\n }\n return this.computeHash(this._normalizeEntries(Object.entries(from)));\n }\n return fail(`computeHash: Unexpected type - cannot hash '${typeof from}'`);\n }\n\n /**\n * Compares two property names from some object being normalized.\n * @param k1 - First key to be compared.\n * @param k2 - Second key to be compared.\n * @returns `1` if `k1` is greater, `-1` if `k2` is greater and\n * `0` if they are equal.\n * @internal\n */\n protected _compareKeys(k1: unknown, k2: unknown): number {\n const cs1 = String(k1);\n const cs2 = String(k2);\n if (cs1 > cs2) {\n return 1;\n }\n if (cs2 > cs1) {\n return -1;\n }\n /* c8 ignore next 2 */\n return 0;\n }\n\n /**\n * Normalizes an array of object property entries (e.g. as returned by `Object.entries()`).\n * @remarks\n * Converts property names (entry key) to string and then sorts as string.\n * @param entries - The entries to be normalized.\n * @returns A normalized sorted array of entries.\n * @internal\n */\n protected _normalizeEntries(entries: Iterable<[unknown, unknown]>): [unknown, unknown][] {\n return Array.from(entries).sort((e1, e2) => this._compareKeys(e1[0], e2[0]));\n }\n\n /**\n * Constructs a normalized string representation of some literal value.\n * @param from - The literal value to be normalized.\n * @returns A normalized string representation of the literal.\n * @internal\n */\n protected _normalizeLiteral(\n from: string | number | bigint | boolean | symbol | undefined | Date | RegExp | null\n ): Result<string> {\n switch (typeof from) {\n case 'string':\n return succeed(from);\n case 'bigint':\n case 'boolean':\n case 'number':\n case 'symbol':\n case 'undefined':\n return succeed(`${typeof from}:[[[${String(from)}]]]`);\n }\n if (from === null) {\n return succeed('object:[[[null]]');\n }\n if (from instanceof Date) {\n return succeed(`Date:[[[${String(from.valueOf())}]]]`);\n }\n if (from instanceof RegExp) {\n return succeed(`RegExp:[[[${from.toString()}]]]`);\n }\n /* c8 ignore next 2 */\n return fail(`cannot normalize ${JSON.stringify(from)}`);\n }\n}\n"]}