@fgv/ts-extras 5.0.0-11 → 5.0.0-14

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 (59) hide show
  1. package/.vscode/settings.json +27 -0
  2. package/config/api-extractor.json +343 -0
  3. package/config/rig.json +16 -0
  4. package/dist/tsdoc-metadata.json +1 -1
  5. package/lib/test/unit/converters.test.d.ts +2 -0
  6. package/lib/test/unit/csvHelpers.test.d.ts +2 -0
  7. package/lib/test/unit/extendedArray.test.d.ts +2 -0
  8. package/lib/test/unit/formatter.test.d.ts +2 -0
  9. package/lib/test/unit/md5Normalizer.test.d.ts +2 -0
  10. package/lib/test/unit/rangeOf.test.d.ts +2 -0
  11. package/lib/test/unit/recordJarHelpers.test.d.ts +2 -0
  12. package/lib/test/unit/zipFileTreeAccessors.test.d.ts +2 -0
  13. package/package.json +13 -13
  14. package/src/index.ts +31 -0
  15. package/src/packlets/conversion/converters.ts +124 -0
  16. package/src/packlets/conversion/index.ts +25 -0
  17. package/src/packlets/csv/csvHelpers.ts +58 -0
  18. package/src/packlets/csv/index.ts +23 -0
  19. package/src/packlets/experimental/extendedArray.ts +110 -0
  20. package/src/packlets/experimental/formatter.ts +114 -0
  21. package/src/packlets/experimental/index.ts +25 -0
  22. package/src/packlets/experimental/rangeOf.ts +222 -0
  23. package/src/packlets/hash/index.ts +23 -0
  24. package/src/packlets/hash/md5Normalizer.ts +38 -0
  25. package/src/packlets/record-jar/index.ts +23 -0
  26. package/src/packlets/record-jar/recordJarHelpers.ts +278 -0
  27. package/src/packlets/zip-file-tree/index.ts +33 -0
  28. package/src/packlets/zip-file-tree/zipFileTreeAccessors.ts +375 -0
  29. package/CHANGELOG.md +0 -98
  30. package/lib/index.d.ts.map +0 -1
  31. package/lib/index.js.map +0 -1
  32. package/lib/packlets/conversion/converters.d.ts.map +0 -1
  33. package/lib/packlets/conversion/converters.js.map +0 -1
  34. package/lib/packlets/conversion/index.d.ts.map +0 -1
  35. package/lib/packlets/conversion/index.js.map +0 -1
  36. package/lib/packlets/csv/csvHelpers.d.ts.map +0 -1
  37. package/lib/packlets/csv/csvHelpers.js.map +0 -1
  38. package/lib/packlets/csv/index.d.ts.map +0 -1
  39. package/lib/packlets/csv/index.js.map +0 -1
  40. package/lib/packlets/experimental/extendedArray.d.ts.map +0 -1
  41. package/lib/packlets/experimental/extendedArray.js.map +0 -1
  42. package/lib/packlets/experimental/formatter.d.ts.map +0 -1
  43. package/lib/packlets/experimental/formatter.js.map +0 -1
  44. package/lib/packlets/experimental/index.d.ts.map +0 -1
  45. package/lib/packlets/experimental/index.js.map +0 -1
  46. package/lib/packlets/experimental/rangeOf.d.ts.map +0 -1
  47. package/lib/packlets/experimental/rangeOf.js.map +0 -1
  48. package/lib/packlets/hash/index.d.ts.map +0 -1
  49. package/lib/packlets/hash/index.js.map +0 -1
  50. package/lib/packlets/hash/md5Normalizer.d.ts.map +0 -1
  51. package/lib/packlets/hash/md5Normalizer.js.map +0 -1
  52. package/lib/packlets/record-jar/index.d.ts.map +0 -1
  53. package/lib/packlets/record-jar/index.js.map +0 -1
  54. package/lib/packlets/record-jar/recordJarHelpers.d.ts.map +0 -1
  55. package/lib/packlets/record-jar/recordJarHelpers.js.map +0 -1
  56. package/lib/packlets/zip-file-tree/index.d.ts.map +0 -1
  57. package/lib/packlets/zip-file-tree/index.js.map +0 -1
  58. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts.map +0 -1
  59. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js.map +0 -1
@@ -0,0 +1,25 @@
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 * as Converters from './converters';
24
+
25
+ export { Converters };
@@ -0,0 +1,58 @@
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
+ import { Result, captureResult } from '@fgv/ts-utils';
24
+ import * as fs from 'fs';
25
+ import { parse } from 'papaparse';
26
+ import * as path from 'path';
27
+
28
+ /**
29
+ * Options for {@link Csv.readCsvFileSync | readCsvFileSync}
30
+ * @beta
31
+ */
32
+ // eslint-disable-next-line @typescript-eslint/naming-convention
33
+ export interface CsvOptions {
34
+ delimiter?: string;
35
+ }
36
+
37
+ /**
38
+ * Reads a CSV file from a supplied path.
39
+ * @param srcPath - Source path from which the file is read.
40
+ * @param options - optional parameters to control the processing
41
+ * @returns The contents of the file.
42
+ * @beta
43
+ */
44
+ export function readCsvFileSync(srcPath: string, options?: CsvOptions): Result<unknown> {
45
+ return captureResult(() => {
46
+ const fullPath = path.resolve(srcPath);
47
+ const body = fs.readFileSync(fullPath, 'utf8').toString();
48
+ options = options ?? {};
49
+ // eslint-disable-next-line
50
+ return parse(body, {
51
+ transform: (s: string) => s.trim(),
52
+ header: false,
53
+ dynamicTyping: false,
54
+ skipEmptyLines: 'greedy',
55
+ ...options
56
+ }).data.slice(1);
57
+ });
58
+ }
@@ -0,0 +1,23 @@
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 './csvHelpers';
@@ -0,0 +1,110 @@
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
+ import { Result, fail, succeed } from '@fgv/ts-utils';
24
+
25
+ /**
26
+ * An experimental array template which extend built-in `Array` to include a handful
27
+ * of predicates which return `Result<T>`.
28
+ * @beta
29
+ */
30
+ export class ExtendedArray<T> extends Array<T> {
31
+ public readonly itemDescription: string;
32
+
33
+ /**
34
+ * Constructs an {@link Experimental.ExtendedArray | ExtendedArray}.
35
+ * @param itemDescription - Brief description of the type of each item in this array.
36
+ * @param items - The initial contents of the array.
37
+ */
38
+ public constructor(itemDescription: string, ...items: T[]) {
39
+ super(...items);
40
+ this.itemDescription = itemDescription;
41
+ }
42
+
43
+ /**
44
+ * Type guard to determine if some arbitrary array is an
45
+ * {@link Experimental.ExtendedArray}
46
+ * @param a - The `Array` to be tested.
47
+ * @returns Returns `true` if `a` is an {@link Experimental.ExtendedArray | ExtendedArray},
48
+ * `false` otherwise.
49
+ */
50
+ public static isExtendedArray<T>(a?: T[]): a is ExtendedArray<T> {
51
+ return a instanceof ExtendedArray;
52
+ }
53
+
54
+ /**
55
+ * Determines if this array contains exactly one element which matches
56
+ * a supplied predicate.
57
+ * @param predicate - The predicate function to be applied.
58
+ * @returns Returns `Success<T>` with the single matching
59
+ * result if exactly one item matches `predicate`. Returns `Failure<T>`
60
+ * with an error message if there are no matches or more than one match.
61
+ */
62
+ public single(predicate?: (item: T) => boolean): Result<T> {
63
+ const match = predicate ? this.filter(predicate) : this;
64
+ if (match.length === 1) {
65
+ return succeed(match[0]);
66
+ }
67
+ if (match.length === 0) {
68
+ return fail(`${this.itemDescription} not found`);
69
+ }
70
+ return fail(`${this.itemDescription} matches ${match.length} items`);
71
+ }
72
+
73
+ /**
74
+ * Returns the first element of an {@link Experimental.ExtendedArray | ExtendedArray}. Fails with an
75
+ * error message if the array is empty.
76
+ * @param failMessage - Optional message to be displayed in the event of failure.
77
+ * @returns Returns `Success<T>` with the value of the first element
78
+ * in the array, or `Failure<T>` with an error message if the array is empty.
79
+ */
80
+ public first(failMessage?: string): Result<T> {
81
+ if (this.length > 0) {
82
+ return succeed(this[0]);
83
+ }
84
+ return fail(failMessage ?? `${this.itemDescription} not found`);
85
+ }
86
+
87
+ /**
88
+ * Returns an array containing all elements of an {@link Experimental.ExtendedArray | ExtendedArray}.
89
+ * Fails with an error message if the array is empty.
90
+ * @param failMessage - Optional message to be displayed in the event of failure.
91
+ * @returns Returns `Success<T>` with a new (non-extended) `Array`
92
+ * containing the elements of this array, or `Failure<T>` with an error message
93
+ * if the array is empty.
94
+ */
95
+ public atLeastOne(failMessage?: string): Result<T[]> {
96
+ if (this.length > 0) {
97
+ return succeed(Array.from(this));
98
+ }
99
+ return fail(failMessage ?? `${this.itemDescription} not found`);
100
+ }
101
+
102
+ /**
103
+ * Gets a new (non-extended) `Array` containing all of the elements from this
104
+ * {@link Experimental.ExtendedArray | ExtendedArray}.
105
+ * @returns A new (non-extended) `Array<T>`.
106
+ */
107
+ public all(): T[] {
108
+ return Array.from(this);
109
+ }
110
+ }
@@ -0,0 +1,114 @@
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
+ import { Result, captureResult, mapResults, succeed } from '@fgv/ts-utils';
24
+ import Mustache from 'mustache';
25
+
26
+ Mustache.escape = (s: string) => s;
27
+
28
+ /**
29
+ * Destination format for some formatted string.
30
+ * @beta
31
+ */
32
+ export type FormatTargets = 'text' | 'markdown' | 'embed';
33
+
34
+ /**
35
+ * Interface for an object that can be formatted.
36
+ * @beta
37
+ */
38
+ // eslint-disable-next-line @typescript-eslint/naming-convention
39
+ export interface Formattable {
40
+ /**
41
+ * Formats an object using the supplied mustache template.
42
+ * @param format - A mustache template used to format the object.
43
+ * @returns `Success<string>` with the resulting string, or `Failure<string>`
44
+ * with an error message if an error occurs.
45
+ */
46
+ format(format: string): Result<string>;
47
+ }
48
+
49
+ /**
50
+ * Base class which adds common formatting.
51
+ * @beta
52
+ */
53
+ export class FormattableBase {
54
+ /**
55
+ * Helper enables derived classes to add named details to a formatted presentation.
56
+ * @param details - An array of detail description strings.
57
+ * @param label - Label to use for the new detail.
58
+ * @param value - Value to use for the new detail.
59
+ * @internal
60
+ */
61
+ protected static _tryAddDetail(details: string[], label: string, value: string | undefined): void {
62
+ if (value !== undefined) {
63
+ const padded = ` ${label}:`.padEnd(20, ' ');
64
+ details.push(`${padded} ${value}`);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * {@inheritdoc Experimental.Formattable.format}
70
+ */
71
+ public format(template: string): Result<string> {
72
+ return captureResult(() => Mustache.render(template, this));
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Type definition for a formatting function, which takes a `string` and an
78
+ * item and returns `Result<string>`.
79
+ * @beta
80
+ */
81
+ export type Formatter<T> = (format: string, item: T) => Result<string>;
82
+
83
+ /**
84
+ * A collection of {@link Experimental.Formatter | formatters} indexed by target name, to enable
85
+ * different format methods per output target.
86
+ * @beta
87
+ */
88
+ export type FormattersByExtendedTarget<TFT extends FormatTargets, T> = Record<TFT, Formatter<T>>;
89
+ /**
90
+ * A collection of {@link Experimental.Formatter | formatters} indexed by the
91
+ * {@link Experimental.FormatTargets | default supported target formats}.
92
+ * @beta
93
+ */
94
+ export type FormattersByTarget<T> = FormattersByExtendedTarget<FormatTargets, T>;
95
+
96
+ /**
97
+ * Formats a list of items using the supplied template and formatter, one result
98
+ * per output line.
99
+ * @param format - A mustache template used to format each item.
100
+ * @param items - The items to be formatted.
101
+ * @param itemFormatter - The {@link Experimental.Formatter | Formatter<T>} used to format each item.
102
+ * @returns The resulting string.
103
+ * @beta
104
+ */
105
+ export function formatList<T>(format: string, items: T[], itemFormatter: Formatter<T>): Result<string> {
106
+ return mapResults(
107
+ items.map((item) => {
108
+ return itemFormatter(format, item);
109
+ })
110
+ ).onSuccess((results: string[]) => {
111
+ const filtered = results.filter((s) => s !== '');
112
+ return succeed(filtered.join('\n'));
113
+ });
114
+ }
@@ -0,0 +1,25 @@
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
+ export * from './extendedArray';
24
+ export * from './formatter';
25
+ export * from './rangeOf';
@@ -0,0 +1,222 @@
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
+ import { Result, captureResult } from '@fgv/ts-utils';
24
+ import Mustache from 'mustache';
25
+
26
+ /**
27
+ * Represents a generic range of some comparable type `<T>`.
28
+ * @public
29
+ */
30
+ // eslint-disable-next-line @typescript-eslint/naming-convention
31
+ export interface RangeOfProperties<T> {
32
+ readonly min?: T;
33
+ readonly max?: T;
34
+ }
35
+
36
+ /**
37
+ * Format strings (in mustache format) to
38
+ * use for both open-ended and complete
39
+ * {@link Experimental.RangeOf | RangeOf<T>}.
40
+ * @public
41
+ */
42
+ // eslint-disable-next-line @typescript-eslint/naming-convention
43
+ export interface RangeOfFormats {
44
+ minOnly: string;
45
+ maxOnly: string;
46
+ minMax: string;
47
+ }
48
+
49
+ /**
50
+ * Default {@link Experimental.RangeOfFormats | formats} to use for both
51
+ * open-ended and complete {@link Experimental.RangeOf | RangeOf<T>}.
52
+ * @public
53
+ */
54
+ export const DEFAULT_RANGEOF_FORMATS: RangeOfFormats = {
55
+ minOnly: '{{min}}-',
56
+ maxOnly: '-{{max}}',
57
+ minMax: '{{min}}-{{max}}'
58
+ };
59
+
60
+ /**
61
+ * Simple implementation of a possibly open-ended range of some comparable
62
+ * type `<T>` with test and formatting.
63
+ * @public
64
+ */
65
+ export class RangeOf<T> implements RangeOfProperties<T> {
66
+ /**
67
+ * Minimum extent of the range.
68
+ */
69
+ public readonly min?: T;
70
+ /**
71
+ * Maximum extent of the range.
72
+ */
73
+ public readonly max?: T;
74
+
75
+ /**
76
+ * Creates a new {@link Experimental.RangeOf | RangeOf<T>}.
77
+ * @param min - Optional minimum extent of the range.
78
+ * @param max - Optional maximum extent of the range.
79
+ */
80
+ public constructor(min?: T, max?: T) {
81
+ if (min !== undefined && max !== undefined && this._compare(min, max) === 'greater') {
82
+ throw new Error(`Inverted range - ${JSON.stringify(min)} must be <= ${JSON.stringify(max)}.`);
83
+ }
84
+ this.min = min;
85
+ this.max = max;
86
+ }
87
+
88
+ /**
89
+ * Static constructor for a {@link Experimental.RangeOf | RangeOf<T>}.
90
+ * @param init - {@link Experimental.RangeOfProperties | Range initializer}.
91
+ * @returns A new {@link Experimental.RangeOf | RangeOf<T>}.
92
+ */
93
+ public static createRange<T>(init?: RangeOfProperties<T>): Result<RangeOf<T>> {
94
+ return captureResult(() => new RangeOf<T>(init?.min, init?.max));
95
+ }
96
+
97
+ /**
98
+ * Gets a formatted description of a {@link Experimental.RangeOfProperties | RangeOfProperties<T>} given an
99
+ * optional set of formats and 'empty' value to use.
100
+ * @param range - The {@link Experimental.RangeOfProperties | RangeOfProperties<T>} to be formatted.
101
+ * @param formats - Optional {@link Experimental.RangeOfFormats | formats} to use. Default is
102
+ * {@link Experimental.DEFAULT_RANGEOF_FORMATS | DEFAULT_RANGEOF_FORMATS}.
103
+ * @param emptyValue - Value which represents unbounded minimum or maximum for this range. Default is `undefined`.
104
+ * @returns A string representation of the range.
105
+ */
106
+ public static propertiesToString<T>(
107
+ range: RangeOfProperties<T>,
108
+ formats?: RangeOfFormats,
109
+ emptyValue?: T
110
+ ): string | undefined {
111
+ formats = formats ?? DEFAULT_RANGEOF_FORMATS;
112
+ if (range.min !== undefined && range.min !== emptyValue) {
113
+ if (range.max !== undefined && range.max !== emptyValue) {
114
+ return Mustache.render(formats.minMax, range);
115
+ } else {
116
+ return Mustache.render(formats.minOnly, range);
117
+ }
118
+ } else if (range.max !== undefined && range.max !== emptyValue) {
119
+ return Mustache.render(formats.maxOnly, range);
120
+ }
121
+ return undefined;
122
+ }
123
+
124
+ /**
125
+ * Default comparison uses javascript built-in comparison.
126
+ * @param t1 - First value to be compared.
127
+ * @param t2 - Second value to be compared.
128
+ * @returns `'less'` if `t1` is less than `t2`, `'greater'` if `t1` is larger
129
+ * and `'equal'` if `t1` and `t2` are equal.
130
+ * @internal
131
+ */
132
+ protected static _defaultCompare<T>(t1: T, t2: T): 'less' | 'equal' | 'greater' {
133
+ if (t1 < t2) {
134
+ return 'less';
135
+ } else if (t1 > t2) {
136
+ return 'greater';
137
+ }
138
+ return 'equal';
139
+ }
140
+
141
+ /**
142
+ * Checks if a supplied value is within this range.
143
+ * @param t - The value to be tested.
144
+ * @returns `'included'` if `t` falls within the range, `'less'` if `t` falls
145
+ * below the minimum extent of the range and `'greater'` if `t` is above the
146
+ * maximum extent.
147
+ */
148
+ public check(t: T): 'less' | 'included' | 'greater' {
149
+ if (this.min !== undefined && this._compare(t, this.min) === 'less') {
150
+ return 'less';
151
+ }
152
+ if (this.max !== undefined && this._compare(t, this.max) !== 'less') {
153
+ return 'greater';
154
+ }
155
+ return 'included';
156
+ }
157
+
158
+ /**
159
+ * Determines if a supplied value is within this range.
160
+ * @param t - The value to be tested.
161
+ * @returns Returns `true` if `t` falls within the range, `false` otherwise.
162
+ */
163
+ public includes(t: T): boolean {
164
+ return this.check(t) === 'included';
165
+ }
166
+
167
+ /**
168
+ * Finds the transition value that would bring a supplied value `t` into
169
+ * range.
170
+ * @param t - The value to be tested.
171
+ * @returns The minimum extent of the range if `t` is below the range or
172
+ * the maximum extent of the range if `t` is above the range. Returns
173
+ * `undefined` if `t` already falls within the range.
174
+ */
175
+ public findTransition(t: T): T | undefined {
176
+ switch (this.check(t)) {
177
+ case 'less':
178
+ return this.min;
179
+ case 'included':
180
+ return this.max;
181
+ }
182
+ return undefined;
183
+ }
184
+
185
+ /**
186
+ * Formats the minimum and maximum values of this range.
187
+ * @param format - A format function used to format the values.
188
+ * @returns A {@link Experimental.RangeOfProperties | RangeOfProperties<string>} containing the
189
+ * formatted representation of the {@link Experimental.RangeOf.min | minimum} and
190
+ * {@link Experimental.RangeOf.max | maximum}
191
+ * extent of the range, or `undefined` for an extent that is not present.
192
+ */
193
+ public toFormattedProperties(format: (value: T) => string | undefined): RangeOfProperties<string> {
194
+ return {
195
+ min: this.min !== undefined ? format(this.min) : undefined,
196
+ max: this.max !== undefined ? format(this.max) : undefined
197
+ };
198
+ }
199
+
200
+ /**
201
+ * Formats this range using the supplied format function.
202
+ * @param format - Format function used to format minimum and maximum extent values.
203
+ * @param formats - The {@link Experimental.RangeOfFormats | format strings} used to format the range
204
+ * (default {@link Experimental.DEFAULT_RANGEOF_FORMATS}).
205
+ * @returns Returns a formatted representation of this range.
206
+ */
207
+ public format(format: (value: T) => string | undefined, formats?: RangeOfFormats): string | undefined {
208
+ return RangeOf.propertiesToString(this.toFormattedProperties(format), formats);
209
+ }
210
+
211
+ /**
212
+ * Inner compare method can be overridden by a derived class.
213
+ * @param t1 - First value to compare.
214
+ * @param t2 - Second value to compare.
215
+ * @returns `'less'` if `t1` is less than `t2`, `'greater'` if `t1` is larger
216
+ * and `'equal'` if `t1` and `t2` are equal.
217
+ * @internal
218
+ */
219
+ protected _compare(t1: T, t2: T): 'less' | 'equal' | 'greater' {
220
+ return RangeOf._defaultCompare(t1, t2);
221
+ }
222
+ }
@@ -0,0 +1,23 @@
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 './md5Normalizer';
@@ -0,0 +1,38 @@
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 { Hash } from '@fgv/ts-utils';
24
+ import * as crypto from 'crypto';
25
+ /**
26
+ * A hashing normalizer which computes object
27
+ * hash using the MD5 algorithm.
28
+ * @public
29
+ */
30
+ export class Md5Normalizer extends Hash.HashingNormalizer {
31
+ public constructor() {
32
+ super(Md5Normalizer.md5Hash);
33
+ }
34
+
35
+ public static md5Hash(parts: string[]): string {
36
+ return crypto.createHash('md5').update(parts.join('|'), 'utf8').digest('hex');
37
+ }
38
+ }
@@ -0,0 +1,23 @@
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
+ export * from './recordJarHelpers';