@fgv/ts-extras 5.0.1-9 → 5.0.1

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 (31) hide show
  1. package/dist/index.browser.js +35 -0
  2. package/dist/index.js +29 -0
  3. package/dist/packlets/conversion/converters.js +107 -0
  4. package/dist/packlets/conversion/index.js +24 -0
  5. package/dist/packlets/csv/csvFileHelpers.js +55 -0
  6. package/dist/packlets/csv/csvHelpers.js +37 -0
  7. package/dist/packlets/csv/index.browser.js +29 -0
  8. package/dist/packlets/csv/index.js +26 -0
  9. package/dist/packlets/experimental/extendedArray.js +102 -0
  10. package/dist/packlets/experimental/formatter.js +67 -0
  11. package/dist/packlets/experimental/index.js +25 -0
  12. package/dist/packlets/experimental/rangeOf.js +178 -0
  13. package/dist/packlets/hash/index.browser.js +24 -0
  14. package/dist/packlets/hash/index.js +23 -0
  15. package/dist/packlets/hash/index.node.js +26 -0
  16. package/dist/packlets/hash/md5Normalizer.browser.js +34 -0
  17. package/dist/packlets/hash/md5Normalizer.js +37 -0
  18. package/dist/packlets/record-jar/index.browser.js +29 -0
  19. package/dist/packlets/record-jar/index.js +26 -0
  20. package/dist/packlets/record-jar/recordJarFileHelpers.js +60 -0
  21. package/dist/packlets/record-jar/recordJarHelpers.js +216 -0
  22. package/dist/packlets/zip-file-tree/index.js +32 -0
  23. package/dist/packlets/zip-file-tree/zipFileTreeAccessors.js +344 -0
  24. package/dist/tsdoc-metadata.json +1 -1
  25. package/lib/index.browser.d.ts +3 -1
  26. package/lib/index.browser.js +7 -1
  27. package/lib/packlets/csv/index.browser.d.ts +3 -0
  28. package/lib/packlets/csv/index.browser.js +47 -0
  29. package/lib/packlets/record-jar/index.browser.d.ts +3 -0
  30. package/lib/packlets/record-jar/index.browser.js +47 -0
  31. package/package.json +6 -5
@@ -0,0 +1,35 @@
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
+ /* c8 ignore start - Browser-specific export used conditionally in package.json */
23
+ // eslint-disable-next-line @rushstack/packlets/mechanics
24
+ import * as Csv from './packlets/csv/index.browser';
25
+ import * as Experimental from './packlets/experimental';
26
+ // eslint-disable-next-line @rushstack/packlets/mechanics
27
+ import * as Hash from './packlets/hash/index.browser';
28
+ // eslint-disable-next-line @rushstack/packlets/mechanics
29
+ import * as RecordJar from './packlets/record-jar/index.browser';
30
+ import * as ZipFileTree from './packlets/zip-file-tree';
31
+ import { Converters } from './packlets/conversion';
32
+ // Browser-safe exports - Node.js crypto-based hash excluded (using CRC32 instead)
33
+ export { Converters, Csv, Experimental, Hash, RecordJar, ZipFileTree };
34
+ /* c8 ignore stop */
35
+ //# sourceMappingURL=index.browser.js.map
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
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
+ import * as Csv from './packlets/csv';
23
+ import * as Experimental from './packlets/experimental';
24
+ import * as Hash from './packlets/hash';
25
+ import * as RecordJar from './packlets/record-jar';
26
+ import * as ZipFileTree from './packlets/zip-file-tree';
27
+ import { Converters } from './packlets/conversion';
28
+ export { Converters, Csv, Experimental, Hash, RecordJar, ZipFileTree };
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,107 @@
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
+ import { Conversion, Converters, captureResult, fail, succeed } from '@fgv/ts-utils';
23
+ import { DateTime } from 'luxon';
24
+ import Mustache from 'mustache';
25
+ import { ExtendedArray, RangeOf } from '../experimental';
26
+ /**
27
+ * Helper function to create a `StringConverter` which converts
28
+ * `unknown` to `string`, applying template conversions supplied at construction time or at
29
+ * runtime as context.
30
+ * @remarks
31
+ * Template conversions are applied using `mustache` syntax.
32
+ * @param defaultContext - Optional default context to use for template values.
33
+ * @returns A new `Converter` returning `string`.
34
+ * @public
35
+ */
36
+ export function templateString(defaultContext) {
37
+ return new Conversion.StringConverter(defaultContext, undefined, (from, __self, context) => {
38
+ if (typeof from !== 'string') {
39
+ return fail(`Not a string: ${JSON.stringify(from)}`);
40
+ }
41
+ return captureResult(() => Mustache.render(from, context));
42
+ });
43
+ }
44
+ /**
45
+ * A `Converter` which converts an iso formatted string, a number or a `Date` object to
46
+ * a `Date` object.
47
+ * @public
48
+ */
49
+ export const isoDate = new Conversion.BaseConverter((from) => {
50
+ if (typeof from === 'string') {
51
+ const dt = DateTime.fromISO(from);
52
+ if (dt.isValid) {
53
+ return succeed(dt.toJSDate());
54
+ }
55
+ return fail(`Invalid date: ${dt.invalidExplanation}`);
56
+ }
57
+ else if (typeof from === 'number') {
58
+ return succeed(new Date(from));
59
+ }
60
+ else if (from instanceof Date) {
61
+ return succeed(from);
62
+ }
63
+ return fail(`Cannot convert ${JSON.stringify(from)} to Date`);
64
+ });
65
+ /**
66
+ * A helper function to create a `Converter` which converts `unknown` to {@link Experimental.ExtendedArray | ExtendedArray<T>}.
67
+ * @remarks
68
+ * If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
69
+ * be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
70
+ * @param converter - `Converter` used to convert each item in the array
71
+ * @param ignoreErrors - Specifies treatment of unconvertible elements
72
+ * @beta
73
+ */
74
+ export function extendedArrayOf(label, converter, onError = 'failOnError') {
75
+ return Converters.arrayOf(converter, onError).map((items) => {
76
+ return captureResult(() => new ExtendedArray(label, ...items));
77
+ });
78
+ }
79
+ /**
80
+ * A helper wrapper to construct a `Converter` which converts to an arbitrary strongly-typed
81
+ * range of some comparable type.
82
+ * @param converter - `Converter` used to convert `min` and `max` extent of the range.
83
+ * @param constructor - Static constructor to instantiate the object.
84
+ * @public
85
+ */
86
+ export function rangeTypeOf(converter, constructor) {
87
+ return new Conversion.BaseConverter((from, __self, context) => {
88
+ const result = Converters.object({
89
+ min: converter,
90
+ max: converter
91
+ }, { optionalFields: ['min', 'max'] }).convert(from, context);
92
+ if (result.isSuccess()) {
93
+ return constructor({ min: result.value.min, max: result.value.max });
94
+ }
95
+ return fail(result.message);
96
+ });
97
+ }
98
+ /**
99
+ * A helper wrapper to construct a `Converter` which converts to {@link Experimental.RangeOf | RangeOf<T>}
100
+ * where `<T>` is some comparable type.
101
+ * @param converter - `Converter` used to convert `min` and `max` extent of the range.
102
+ * @public
103
+ */
104
+ export function rangeOf(converter) {
105
+ return rangeTypeOf(converter, RangeOf.createRange);
106
+ }
107
+ //# sourceMappingURL=converters.js.map
@@ -0,0 +1,24 @@
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
+ import * as Converters from './converters';
23
+ export { Converters };
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,55 @@
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
+ import { captureResult } from '@fgv/ts-utils';
23
+ import * as fs from 'fs';
24
+ import * as path from 'path';
25
+ import { parseCsvString } from './csvHelpers';
26
+ /**
27
+ * Reads a CSV file from a supplied path.
28
+ * @param srcPath - Source path from which the file is read.
29
+ * @param options - optional parameters to control the processing
30
+ * @returns The contents of the file.
31
+ * @beta
32
+ */
33
+ export function readCsvFileSync(srcPath, options) {
34
+ return captureResult(() => {
35
+ const fullPath = path.resolve(srcPath);
36
+ return fs.readFileSync(fullPath, 'utf8').toString();
37
+ }).onSuccess((body) => {
38
+ return parseCsvString(body, options);
39
+ });
40
+ }
41
+ /**
42
+ * Reads a CSV file from a FileTree.
43
+ * @param fileTree - The FileTree to read from.
44
+ * @param filePath - Path of the file within the tree.
45
+ * @param options - optional parameters to control the processing
46
+ * @returns The parsed CSV data.
47
+ * @beta
48
+ */
49
+ export function readCsvFromTree(fileTree, filePath, options) {
50
+ return fileTree
51
+ .getFile(filePath)
52
+ .onSuccess((file) => file.getRawContents())
53
+ .onSuccess((contents) => parseCsvString(contents, options));
54
+ }
55
+ //# sourceMappingURL=csvFileHelpers.js.map
@@ -0,0 +1,37 @@
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
+ import { captureResult } from '@fgv/ts-utils';
23
+ import { parse } from 'papaparse';
24
+ /**
25
+ * Parses CSV data from a string.
26
+ * @param body - The CSV string to parse.
27
+ * @param options - optional parameters to control the processing
28
+ * @returns The parsed CSV data.
29
+ * @beta
30
+ */
31
+ export function parseCsvString(body, options) {
32
+ return captureResult(() => {
33
+ options = options !== null && options !== void 0 ? options : {};
34
+ return parse(body, Object.assign({ transform: (s) => s.trim(), header: false, dynamicTyping: false, skipEmptyLines: 'greedy' }, options)).data.slice(1);
35
+ });
36
+ }
37
+ //# sourceMappingURL=csvHelpers.js.map
@@ -0,0 +1,29 @@
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
+ // Browser-safe CSV exports - excludes Node.js filesystem dependencies
23
+ // Export all browser-safe parsing functionality
24
+ export * from './csvHelpers';
25
+ // Export FileTree-based reading (web-compatible)
26
+ export { readCsvFromTree } from './csvFileHelpers';
27
+ // Exclude:
28
+ // - readCsvFileSync (requires Node.js fs/path)
29
+ //# sourceMappingURL=index.browser.js.map
@@ -0,0 +1,26 @@
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
+ // Export tree-shakeable functions
23
+ export * from './csvHelpers';
24
+ // Filesystem helpers in separate module for tree-shaking
25
+ export * from './csvFileHelpers';
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,102 @@
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
+ import { fail, succeed } from '@fgv/ts-utils';
23
+ /**
24
+ * An experimental array template which extend built-in `Array` to include a handful
25
+ * of predicates which return `Result<T>`.
26
+ * @beta
27
+ */
28
+ export class ExtendedArray extends Array {
29
+ /**
30
+ * Constructs an {@link Experimental.ExtendedArray | ExtendedArray}.
31
+ * @param itemDescription - Brief description of the type of each item in this array.
32
+ * @param items - The initial contents of the array.
33
+ */
34
+ constructor(itemDescription, ...items) {
35
+ super(...items);
36
+ this.itemDescription = itemDescription;
37
+ }
38
+ /**
39
+ * Type guard to determine if some arbitrary array is an
40
+ * {@link Experimental.ExtendedArray}
41
+ * @param a - The `Array` to be tested.
42
+ * @returns Returns `true` if `a` is an {@link Experimental.ExtendedArray | ExtendedArray},
43
+ * `false` otherwise.
44
+ */
45
+ static isExtendedArray(a) {
46
+ return a instanceof ExtendedArray;
47
+ }
48
+ /**
49
+ * Determines if this array contains exactly one element which matches
50
+ * a supplied predicate.
51
+ * @param predicate - The predicate function to be applied.
52
+ * @returns Returns `Success<T>` with the single matching
53
+ * result if exactly one item matches `predicate`. Returns `Failure<T>`
54
+ * with an error message if there are no matches or more than one match.
55
+ */
56
+ single(predicate) {
57
+ const match = predicate ? this.filter(predicate) : this;
58
+ if (match.length === 1) {
59
+ return succeed(match[0]);
60
+ }
61
+ if (match.length === 0) {
62
+ return fail(`${this.itemDescription} not found`);
63
+ }
64
+ return fail(`${this.itemDescription} matches ${match.length} items`);
65
+ }
66
+ /**
67
+ * Returns the first element of an {@link Experimental.ExtendedArray | ExtendedArray}. Fails with an
68
+ * error message if the array is empty.
69
+ * @param failMessage - Optional message to be displayed in the event of failure.
70
+ * @returns Returns `Success<T>` with the value of the first element
71
+ * in the array, or `Failure<T>` with an error message if the array is empty.
72
+ */
73
+ first(failMessage) {
74
+ if (this.length > 0) {
75
+ return succeed(this[0]);
76
+ }
77
+ return fail(failMessage !== null && failMessage !== void 0 ? failMessage : `${this.itemDescription} not found`);
78
+ }
79
+ /**
80
+ * Returns an array containing all elements of an {@link Experimental.ExtendedArray | ExtendedArray}.
81
+ * Fails with an error message if the array is empty.
82
+ * @param failMessage - Optional message to be displayed in the event of failure.
83
+ * @returns Returns `Success<T>` with a new (non-extended) `Array`
84
+ * containing the elements of this array, or `Failure<T>` with an error message
85
+ * if the array is empty.
86
+ */
87
+ atLeastOne(failMessage) {
88
+ if (this.length > 0) {
89
+ return succeed(Array.from(this));
90
+ }
91
+ return fail(failMessage !== null && failMessage !== void 0 ? failMessage : `${this.itemDescription} not found`);
92
+ }
93
+ /**
94
+ * Gets a new (non-extended) `Array` containing all of the elements from this
95
+ * {@link Experimental.ExtendedArray | ExtendedArray}.
96
+ * @returns A new (non-extended) `Array<T>`.
97
+ */
98
+ all() {
99
+ return Array.from(this);
100
+ }
101
+ }
102
+ //# sourceMappingURL=extendedArray.js.map
@@ -0,0 +1,67 @@
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
+ import { captureResult, mapResults, succeed } from '@fgv/ts-utils';
23
+ import Mustache from 'mustache';
24
+ Mustache.escape = (s) => s;
25
+ /**
26
+ * Base class which adds common formatting.
27
+ * @beta
28
+ */
29
+ export class FormattableBase {
30
+ /**
31
+ * Helper enables derived classes to add named details to a formatted presentation.
32
+ * @param details - An array of detail description strings.
33
+ * @param label - Label to use for the new detail.
34
+ * @param value - Value to use for the new detail.
35
+ * @internal
36
+ */
37
+ static _tryAddDetail(details, label, value) {
38
+ if (value !== undefined) {
39
+ const padded = ` ${label}:`.padEnd(20, ' ');
40
+ details.push(`${padded} ${value}`);
41
+ }
42
+ }
43
+ /**
44
+ * {@inheritdoc Experimental.Formattable.format}
45
+ */
46
+ format(template) {
47
+ return captureResult(() => Mustache.render(template, this));
48
+ }
49
+ }
50
+ /**
51
+ * Formats a list of items using the supplied template and formatter, one result
52
+ * per output line.
53
+ * @param format - A mustache template used to format each item.
54
+ * @param items - The items to be formatted.
55
+ * @param itemFormatter - The {@link Experimental.Formatter | Formatter<T>} used to format each item.
56
+ * @returns The resulting string.
57
+ * @beta
58
+ */
59
+ export function formatList(format, items, itemFormatter) {
60
+ return mapResults(items.map((item) => {
61
+ return itemFormatter(format, item);
62
+ })).onSuccess((results) => {
63
+ const filtered = results.filter((s) => s !== '');
64
+ return succeed(filtered.join('\n'));
65
+ });
66
+ }
67
+ //# sourceMappingURL=formatter.js.map
@@ -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
+ export * from './extendedArray';
23
+ export * from './formatter';
24
+ export * from './rangeOf';
25
+ //# sourceMappingURL=index.js.map