@fgv/ts-utils 4.2.1 → 4.3.0
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.
- package/CHANGELOG.json +24 -0
- package/CHANGELOG.md +15 -1
- package/dist/ts-utils.d.ts +3080 -2627
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -2
- package/lib/index.js.map +1 -1
- package/lib/packlets/base/messageAggregator.d.ts +4 -0
- package/lib/packlets/base/messageAggregator.d.ts.map +1 -1
- package/lib/packlets/base/messageAggregator.js +6 -0
- package/lib/packlets/base/messageAggregator.js.map +1 -1
- package/lib/packlets/base/result.d.ts +55 -0
- package/lib/packlets/base/result.d.ts.map +1 -1
- package/lib/packlets/base/result.js +32 -0
- package/lib/packlets/base/result.js.map +1 -1
- package/lib/packlets/collections/collectible.d.ts +48 -0
- package/lib/packlets/collections/collectible.d.ts.map +1 -0
- package/lib/packlets/collections/collectible.js +59 -0
- package/lib/packlets/collections/collectible.js.map +1 -0
- package/lib/packlets/collections/collector.d.ts +130 -0
- package/lib/packlets/collections/collector.d.ts.map +1 -0
- package/lib/packlets/collections/collector.js +147 -0
- package/lib/packlets/collections/collector.js.map +1 -0
- package/lib/packlets/collections/collectorConverter.d.ts +59 -0
- package/lib/packlets/collections/collectorConverter.d.ts.map +1 -0
- package/lib/packlets/collections/collectorConverter.js +86 -0
- package/lib/packlets/collections/collectorConverter.js.map +1 -0
- package/lib/packlets/collections/collectors.d.ts +32 -0
- package/lib/packlets/collections/collectors.d.ts.map +1 -0
- package/lib/packlets/collections/collectors.js +57 -0
- package/lib/packlets/collections/collectors.js.map +1 -0
- package/lib/packlets/collections/convertingCollector.d.ts +67 -0
- package/lib/packlets/collections/convertingCollector.d.ts.map +1 -0
- package/lib/packlets/collections/convertingCollector.js +77 -0
- package/lib/packlets/collections/convertingCollector.js.map +1 -0
- package/lib/packlets/collections/{validatingResultMap.d.ts → convertingResultMap.d.ts} +18 -18
- package/lib/packlets/collections/convertingResultMap.d.ts.map +1 -0
- package/lib/packlets/collections/{validatingResultMap.js → convertingResultMap.js} +18 -18
- package/lib/packlets/collections/convertingResultMap.js.map +1 -0
- package/lib/packlets/collections/index.d.ts +8 -2
- package/lib/packlets/collections/index.d.ts.map +1 -1
- package/lib/packlets/collections/index.js +8 -2
- package/lib/packlets/collections/index.js.map +1 -1
- package/lib/packlets/collections/keyValueConverters.d.ts +90 -0
- package/lib/packlets/collections/keyValueConverters.d.ts.map +1 -0
- package/lib/packlets/collections/keyValueConverters.js +111 -0
- package/lib/packlets/collections/keyValueConverters.js.map +1 -0
- package/lib/packlets/collections/readonlyResultMap.d.ts +1 -1
- package/lib/packlets/collections/readonlyResultMap.d.ts.map +1 -1
- package/lib/packlets/collections/readonlyResultMap.js.map +1 -1
- package/lib/packlets/collections/{resultMapValidator.d.ts → resultMapConverter.d.ts} +14 -18
- package/lib/packlets/collections/resultMapConverter.d.ts.map +1 -0
- package/lib/packlets/collections/{resultMapValidator.js → resultMapConverter.js} +15 -15
- package/lib/packlets/collections/resultMapConverter.js.map +1 -0
- package/lib/packlets/collections/utils.d.ts +0 -61
- package/lib/packlets/collections/utils.d.ts.map +1 -1
- package/lib/packlets/collections/utils.js +0 -82
- package/lib/packlets/collections/utils.js.map +1 -1
- package/lib/packlets/conversion/baseConverter.d.ts +12 -7
- package/lib/packlets/conversion/baseConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/baseConverter.js +12 -8
- package/lib/packlets/conversion/baseConverter.js.map +1 -1
- package/lib/packlets/conversion/converter.d.ts +6 -6
- package/lib/packlets/conversion/converter.d.ts.map +1 -1
- package/lib/packlets/conversion/converter.js.map +1 -1
- package/lib/packlets/conversion/converters.d.ts +8 -0
- package/lib/packlets/conversion/converters.d.ts.map +1 -1
- package/lib/packlets/conversion/converters.js +11 -0
- package/lib/packlets/conversion/converters.js.map +1 -1
- package/lib/packlets/conversion/objectConverter.d.ts +1 -1
- package/lib/packlets/conversion/objectConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/objectConverter.js.map +1 -1
- package/lib/packlets/validation/common.d.ts +8 -0
- package/lib/packlets/validation/common.d.ts.map +1 -1
- package/lib/packlets/validation/common.js.map +1 -1
- package/lib/packlets/validation/genericValidator.d.ts +4 -6
- package/lib/packlets/validation/genericValidator.d.ts.map +1 -1
- package/lib/packlets/validation/genericValidator.js.map +1 -1
- package/lib/packlets/validation/number.d.ts.map +1 -1
- package/lib/packlets/validation/number.js +3 -1
- package/lib/packlets/validation/number.js.map +1 -1
- package/lib/packlets/validation/string.d.ts.map +1 -1
- package/lib/packlets/validation/string.js +3 -1
- package/lib/packlets/validation/string.js.map +1 -1
- package/lib/packlets/validation/validators.d.ts +12 -2
- package/lib/packlets/validation/validators.d.ts.map +1 -1
- package/lib/packlets/validation/validators.js +13 -0
- package/lib/packlets/validation/validators.js.map +1 -1
- package/package.json +1 -1
- package/lib/packlets/collections/resultMapValidator.d.ts.map +0 -1
- package/lib/packlets/collections/resultMapValidator.js.map +0 -1
- package/lib/packlets/collections/validatingResultMap.d.ts.map +0 -1
- package/lib/packlets/collections/validatingResultMap.js.map +0 -1
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { DetailedResult, Result } from '../base';
|
|
2
|
+
import { CollectibleFactory, CollectibleFactoryCallback, ICollectible } from './collectible';
|
|
3
|
+
import { Collector } from './collector';
|
|
4
|
+
import { CollectorConverter } from './collectorConverter';
|
|
5
|
+
import { KeyValueEntry } from './common';
|
|
6
|
+
import { IReadOnlyConvertingResultMap } from './convertingResultMap';
|
|
7
|
+
import { KeyValueConverters } from './keyValueConverters';
|
|
8
|
+
import { ResultMapResultDetail } from './readonlyResultMap';
|
|
9
|
+
/**
|
|
10
|
+
* Parameters for constructing a {@link Collections.ConvertingCollector | ConvertingCollector}.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export interface IConvertingCollectorConstructorParams<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>, TSRC = TITEM> {
|
|
14
|
+
/**
|
|
15
|
+
* {@inheritdoc Collections.ICollectorConstructorParams.factory}
|
|
16
|
+
*/
|
|
17
|
+
factory: CollectibleFactory<TKEY, TINDEX, TITEM, TSRC>;
|
|
18
|
+
/**
|
|
19
|
+
* {@inheritdoc Collections.ICollectorConverterCreateParams.converters}
|
|
20
|
+
*/
|
|
21
|
+
converters: KeyValueConverters<TKEY, TSRC>;
|
|
22
|
+
/**
|
|
23
|
+
* {@inheritdoc Collections.ICollectorConstructorParams.entries}
|
|
24
|
+
*/
|
|
25
|
+
entries?: KeyValueEntry<TKEY, TSRC>[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A {@link Collections.Collector | Collector} with a {@link Collections.CollectorConverter | CollectorConverter}
|
|
29
|
+
* property that enables validated use of the underlying map with weakly-typed keys and values.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export declare class ConvertingCollector<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>, TSRC = TITEM> extends Collector<TKEY, TINDEX, TITEM, TSRC> {
|
|
33
|
+
/**
|
|
34
|
+
* A {@link Collections.CollectorConverter | CollectorConverter} which validates keys and values
|
|
35
|
+
* before inserting them into this collector.
|
|
36
|
+
*/
|
|
37
|
+
readonly converting: CollectorConverter<TKEY, TINDEX, TITEM, TSRC>;
|
|
38
|
+
protected readonly _converters: KeyValueConverters<TKEY, TSRC>;
|
|
39
|
+
/**
|
|
40
|
+
* Constructs a new {@link Collections.ConvertingCollector | ConvertingCollector}
|
|
41
|
+
* from the supplied {@link Collections.IConvertingCollectorConstructorParams | parameters}.
|
|
42
|
+
* @param params - Required parameters for constructing the collector.
|
|
43
|
+
*/
|
|
44
|
+
constructor(params: IConvertingCollectorConstructorParams<TKEY, TINDEX, TITEM, TSRC>);
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new {@link Collections.ConvertingCollector | ConvertingCollector} instance from
|
|
47
|
+
* the supplied {@link Collections.IConvertingCollectorConstructorParams | parameters}.
|
|
48
|
+
* @param params - Required parameters for constructing the collector.
|
|
49
|
+
* @returns {@link Success} with the new collector if successful, {@link Failure} otherwise.
|
|
50
|
+
*/
|
|
51
|
+
static createConvertingCollector<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>, TSRC = TITEM>(params: IConvertingCollectorConstructorParams<TKEY, TINDEX, TITEM, TSRC>): Result<ConvertingCollector<TKEY, TINDEX, TITEM, TSRC>>;
|
|
52
|
+
/**
|
|
53
|
+
* {@inheritdoc Collections.Collector.(getOrAdd:1)}
|
|
54
|
+
*/
|
|
55
|
+
getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>;
|
|
56
|
+
/**
|
|
57
|
+
* {@inheritdoc Collections.Collector.(getOrAdd:2)}
|
|
58
|
+
*/
|
|
59
|
+
getOrAdd(key: TKEY, cb: CollectibleFactoryCallback<TKEY, TINDEX, TITEM>): DetailedResult<TITEM, ResultMapResultDetail>;
|
|
60
|
+
/**
|
|
61
|
+
* Gets a read-only version of this collector as a
|
|
62
|
+
* {@link Collections.IReadOnlyConvertingResultMap | read-only map}.
|
|
63
|
+
* @returns
|
|
64
|
+
*/
|
|
65
|
+
toReadOnly(): IReadOnlyConvertingResultMap<TKEY, TITEM>;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=convertingCollector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertingCollector.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/convertingCollector.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAiB,cAAc,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,0BAA0B,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7F,OAA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AC,QAAQ,CAAC,WAAW,EAAE,kBAAkB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAE/D;;;;OAIG;gBACgB,MAAM,EAAE,qCAAqC,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC;IAS3F;;;;;OAKG;WACW,yBAAyB,CACrC,IAAI,SAAS,MAAM,GAAG,MAAM,EAC5B,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,KAAK,SAAS,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,EACrE,IAAI,GAAG,KAAK,EAEZ,MAAM,EAAE,qCAAqC,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,GACvE,MAAM,CAAC,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IAIzD;;OAEG;IACI,QAAQ,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,cAAc,CAAC,KAAK,EAAE,qBAAqB,CAAC;IAEpF;;OAEG;IACI,QAAQ,CACb,GAAG,EAAE,IAAI,EACT,EAAE,EAAE,0BAA0B,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,GAClD,cAAc,CAAC,KAAK,EAAE,qBAAqB,CAAC;IAe/C;;;;OAIG;IACI,UAAU,IAAI,4BAA4B,CAAC,IAAI,EAAE,KAAK,CAAC;CAG/D"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.ConvertingCollector = void 0;
|
|
25
|
+
const base_1 = require("../base");
|
|
26
|
+
const collector_1 = require("./collector");
|
|
27
|
+
const collectorConverter_1 = require("./collectorConverter");
|
|
28
|
+
/**
|
|
29
|
+
* A {@link Collections.Collector | Collector} with a {@link Collections.CollectorConverter | CollectorConverter}
|
|
30
|
+
* property that enables validated use of the underlying map with weakly-typed keys and values.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
class ConvertingCollector extends collector_1.Collector {
|
|
34
|
+
/**
|
|
35
|
+
* Constructs a new {@link Collections.ConvertingCollector | ConvertingCollector}
|
|
36
|
+
* from the supplied {@link Collections.IConvertingCollectorConstructorParams | parameters}.
|
|
37
|
+
* @param params - Required parameters for constructing the collector.
|
|
38
|
+
*/
|
|
39
|
+
constructor(params) {
|
|
40
|
+
var _a;
|
|
41
|
+
super({ factory: params.factory });
|
|
42
|
+
this._converters = params.converters;
|
|
43
|
+
this.converting = new collectorConverter_1.CollectorConverter({ collector: this, converters: params.converters });
|
|
44
|
+
for (const entry of (_a = params.entries) !== null && _a !== void 0 ? _a : []) {
|
|
45
|
+
this.getOrAdd(entry[0], entry[1]).orThrow();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new {@link Collections.ConvertingCollector | ConvertingCollector} instance from
|
|
50
|
+
* the supplied {@link Collections.IConvertingCollectorConstructorParams | parameters}.
|
|
51
|
+
* @param params - Required parameters for constructing the collector.
|
|
52
|
+
* @returns {@link Success} with the new collector if successful, {@link Failure} otherwise.
|
|
53
|
+
*/
|
|
54
|
+
static createConvertingCollector(params) {
|
|
55
|
+
return (0, base_1.captureResult)(() => new ConvertingCollector(params));
|
|
56
|
+
}
|
|
57
|
+
getOrAdd(key, itemOrCb) {
|
|
58
|
+
if (this._isFactoryCB(itemOrCb)) {
|
|
59
|
+
return this.converting.converters.convertKey(key).onSuccess((k) => {
|
|
60
|
+
return super.getOrAdd(k, itemOrCb);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return this.converting.converters.convertEntry([key, itemOrCb]).onSuccess(([k, v]) => {
|
|
64
|
+
return super.getOrAdd(k, v);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets a read-only version of this collector as a
|
|
69
|
+
* {@link Collections.IReadOnlyConvertingResultMap | read-only map}.
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
toReadOnly() {
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.ConvertingCollector = ConvertingCollector;
|
|
77
|
+
//# sourceMappingURL=convertingCollector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertingCollector.js","sourceRoot":"","sources":["../../../src/packlets/collections/convertingCollector.ts"],"names":[],"mappings":";AAAA;;;;;;;;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,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE;YACnF,OAAO,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC9B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA/ED,kDA+EC","sourcesContent":["/*\n * Copyright (c) 2025 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 { captureResult, DetailedResult, Result } from '../base';\nimport { CollectibleFactory, CollectibleFactoryCallback, ICollectible } from './collectible';\nimport { Collector } from './collector';\nimport { CollectorConverter } from './collectorConverter';\nimport { KeyValueEntry } from './common';\nimport { IReadOnlyConvertingResultMap } from './convertingResultMap';\nimport { KeyValueConverters } from './keyValueConverters';\nimport { ResultMapResultDetail } from './readonlyResultMap';\n\n/**\n * Parameters for constructing a {@link Collections.ConvertingCollector | ConvertingCollector}.\n * @public\n */\nexport interface IConvertingCollectorConstructorParams<\n TKEY extends string = string,\n TINDEX extends number = number,\n TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>,\n TSRC = TITEM\n> {\n /**\n * {@inheritdoc Collections.ICollectorConstructorParams.factory}\n */\n factory: CollectibleFactory<TKEY, TINDEX, TITEM, TSRC>;\n\n /**\n * {@inheritdoc Collections.ICollectorConverterCreateParams.converters}\n */\n converters: KeyValueConverters<TKEY, TSRC>;\n\n /**\n * {@inheritdoc Collections.ICollectorConstructorParams.entries}\n */\n entries?: KeyValueEntry<TKEY, TSRC>[];\n}\n\n/**\n * A {@link Collections.Collector | Collector} with a {@link Collections.CollectorConverter | CollectorConverter}\n * property that enables validated use of the underlying map with weakly-typed keys and values.\n * @public\n */\nexport class ConvertingCollector<\n TKEY extends string = string,\n TINDEX extends number = number,\n TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>,\n TSRC = TITEM\n> extends Collector<TKEY, TINDEX, TITEM, TSRC> {\n /**\n * A {@link Collections.CollectorConverter | CollectorConverter} which validates keys and values\n * before inserting them into this collector.\n */\n public readonly converting: CollectorConverter<TKEY, TINDEX, TITEM, TSRC>;\n\n protected readonly _converters: KeyValueConverters<TKEY, TSRC>;\n\n /**\n * Constructs a new {@link Collections.ConvertingCollector | ConvertingCollector}\n * from the supplied {@link Collections.IConvertingCollectorConstructorParams | parameters}.\n * @param params - Required parameters for constructing the collector.\n */\n public constructor(params: IConvertingCollectorConstructorParams<TKEY, TINDEX, TITEM, TSRC>) {\n super({ factory: params.factory });\n this._converters = params.converters;\n this.converting = new CollectorConverter({ collector: this, converters: params.converters });\n for (const entry of params.entries ?? []) {\n this.getOrAdd(entry[0], entry[1]).orThrow();\n }\n }\n\n /**\n * Creates a new {@link Collections.ConvertingCollector | ConvertingCollector} instance from\n * the supplied {@link Collections.IConvertingCollectorConstructorParams | parameters}.\n * @param params - Required parameters for constructing the collector.\n * @returns {@link Success} with the new collector if successful, {@link Failure} otherwise.\n */\n public static createConvertingCollector<\n TKEY extends string = string,\n TINDEX extends number = number,\n TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>,\n TSRC = TITEM\n >(\n params: IConvertingCollectorConstructorParams<TKEY, TINDEX, TITEM, TSRC>\n ): Result<ConvertingCollector<TKEY, TINDEX, TITEM, TSRC>> {\n return captureResult(() => new ConvertingCollector(params));\n }\n\n /**\n * {@inheritdoc Collections.Collector.(getOrAdd:1)}\n */\n public getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>;\n\n /**\n * {@inheritdoc Collections.Collector.(getOrAdd:2)}\n */\n public getOrAdd(\n key: TKEY,\n cb: CollectibleFactoryCallback<TKEY, TINDEX, TITEM>\n ): DetailedResult<TITEM, ResultMapResultDetail>;\n public getOrAdd(\n key: TKEY,\n itemOrCb: TSRC | CollectibleFactoryCallback<TKEY, TINDEX, TITEM>\n ): DetailedResult<TITEM, ResultMapResultDetail> {\n if (this._isFactoryCB(itemOrCb)) {\n return this.converting.converters.convertKey(key).onSuccess((k) => {\n return super.getOrAdd(k, itemOrCb);\n });\n }\n return this.converting.converters.convertEntry([key, itemOrCb]).onSuccess(([k, v]) => {\n return super.getOrAdd(k, v);\n });\n }\n\n /**\n * Gets a read-only version of this collector as a\n * {@link Collections.IReadOnlyConvertingResultMap | read-only map}.\n * @returns\n */\n public toReadOnly(): IReadOnlyConvertingResultMap<TKEY, TITEM> {\n return this;\n }\n}\n"]}
|
|
@@ -2,49 +2,49 @@ import { DetailedResult, Result } from '../base';
|
|
|
2
2
|
import { KeyValueEntry } from './common';
|
|
3
3
|
import { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';
|
|
4
4
|
import { ResultMap, ResultMapValueFactory } from './resultMap';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { IReadOnlyResultMapConverter, ResultMapConverter } from './resultMapConverter';
|
|
6
|
+
import { KeyValueConverters } from './keyValueConverters';
|
|
7
7
|
/**
|
|
8
|
-
* A read-only interface exposing non-mutating methods of a {@link Collections.
|
|
8
|
+
* A read-only interface exposing non-mutating methods of a {@link Collections.ConvertingResultMap | ConvertingResultMap}.
|
|
9
9
|
* @public
|
|
10
10
|
*/
|
|
11
|
-
export interface
|
|
11
|
+
export interface IReadOnlyConvertingResultMap<TK extends string = string, TV = unknown> extends IReadOnlyResultMap<TK, TV> {
|
|
12
12
|
/**
|
|
13
|
-
* {@inheritdoc Collections.
|
|
13
|
+
* {@inheritdoc Collections.ConvertingResultMap.converting}
|
|
14
14
|
*/
|
|
15
|
-
readonly
|
|
15
|
+
readonly converting: IReadOnlyResultMapConverter<TK, TV>;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Parameters for constructing a {@link Collections.ResultMap | ResultMap}.
|
|
19
19
|
* @public
|
|
20
20
|
*/
|
|
21
|
-
export interface
|
|
21
|
+
export interface IConvertingResultMapConstructorParams<TK extends string = string, TV = unknown> {
|
|
22
22
|
entries?: Iterable<KeyValueEntry<string, unknown>>;
|
|
23
|
-
|
|
23
|
+
converters: KeyValueConverters<TK, TV>;
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
* A {@link Collections.ResultMap | ResultMap} with a {@link Collections.
|
|
26
|
+
* A {@link Collections.ResultMap | ResultMap} with a {@link Collections.ResultMapConverter | validator}
|
|
27
27
|
* property that enables validated use of the underlying map with weakly-typed keys and values.
|
|
28
28
|
* @public
|
|
29
29
|
*/
|
|
30
|
-
export declare class
|
|
30
|
+
export declare class ConvertingResultMap<TK extends string = string, TV = unknown> extends ResultMap<TK, TV> implements IReadOnlyConvertingResultMap<TK, TV> {
|
|
31
31
|
/**
|
|
32
|
-
* A {@link Collections.
|
|
32
|
+
* A {@link Collections.ResultMapConverter | ResultMapConverter} which validates keys and values
|
|
33
33
|
* before inserting them into this collection.
|
|
34
34
|
*/
|
|
35
|
-
readonly
|
|
35
|
+
readonly converting: ResultMapConverter<TK, TV>;
|
|
36
36
|
/**
|
|
37
|
-
* Constructs a new {@link Collections.
|
|
37
|
+
* Constructs a new {@link Collections.ConvertingResultMap | ConvertingResultMap}.
|
|
38
38
|
* @param params - Required parameters for constructing the map.
|
|
39
39
|
*/
|
|
40
|
-
constructor(params:
|
|
40
|
+
constructor(params: IConvertingResultMapConstructorParams<TK, TV>);
|
|
41
41
|
/**
|
|
42
|
-
* Creates a new {@link Collections.
|
|
42
|
+
* Creates a new {@link Collections.ConvertingResultMap | ConvertingResultMap} instance.
|
|
43
43
|
* @param params - Required parameters for constructing the map.
|
|
44
44
|
* @returns `Success` with the new map if successful, `Failure` otherwise.
|
|
45
45
|
* @public
|
|
46
46
|
*/
|
|
47
|
-
static
|
|
47
|
+
static createConvertingResultMap<TK extends string = string, TV = unknown>(params: IConvertingResultMapConstructorParams<TK, TV>): Result<ConvertingResultMap<TK, TV>>;
|
|
48
48
|
/**
|
|
49
49
|
* {@inheritdoc Collections.ResultMap.add}
|
|
50
50
|
*/
|
|
@@ -68,6 +68,6 @@ export declare class ValidatingResultMap<TK extends string = string, TV = unknow
|
|
|
68
68
|
/**
|
|
69
69
|
* Gets a read-only version of this map.
|
|
70
70
|
*/
|
|
71
|
-
toReadOnly():
|
|
71
|
+
toReadOnly(): IReadOnlyConvertingResultMap<TK, TV>;
|
|
72
72
|
}
|
|
73
|
-
//# sourceMappingURL=
|
|
73
|
+
//# sourceMappingURL=convertingResultMap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertingResultMap.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/convertingResultMap.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAiB,cAAc,EAAkB,MAAM,EAAW,MAAM,SAAS,CAAC;AACzF,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAChF,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC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AAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAMzE;;OAEG;IACI,QAAQ,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAC9E;;OAEG;IACI,QAAQ,CAAC,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,qBAAqB,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAqB3G;;OAEG;IACI,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAMzE;;OAEG;IACI,MAAM,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAM5E;;OAEG;IACI,UAAU,IAAI,4BAA4B,CAAC,EAAE,EAAE,EAAE,CAAC;CAG1D"}
|
|
@@ -21,54 +21,54 @@
|
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.
|
|
24
|
+
exports.ConvertingResultMap = void 0;
|
|
25
25
|
const base_1 = require("../base");
|
|
26
26
|
const resultMap_1 = require("./resultMap");
|
|
27
|
-
const
|
|
27
|
+
const resultMapConverter_1 = require("./resultMapConverter");
|
|
28
28
|
/**
|
|
29
|
-
* A {@link Collections.ResultMap | ResultMap} with a {@link Collections.
|
|
29
|
+
* A {@link Collections.ResultMap | ResultMap} with a {@link Collections.ResultMapConverter | validator}
|
|
30
30
|
* property that enables validated use of the underlying map with weakly-typed keys and values.
|
|
31
31
|
* @public
|
|
32
32
|
*/
|
|
33
|
-
class
|
|
33
|
+
class ConvertingResultMap extends resultMap_1.ResultMap {
|
|
34
34
|
/**
|
|
35
|
-
* Constructs a new {@link Collections.
|
|
35
|
+
* Constructs a new {@link Collections.ConvertingResultMap | ConvertingResultMap}.
|
|
36
36
|
* @param params - Required parameters for constructing the map.
|
|
37
37
|
*/
|
|
38
38
|
constructor(params) {
|
|
39
39
|
var _a;
|
|
40
|
-
const entries = params.
|
|
40
|
+
const entries = params.converters.convertEntries([...((_a = params.entries) !== null && _a !== void 0 ? _a : [])]).orThrow();
|
|
41
41
|
super({ entries });
|
|
42
|
-
this.
|
|
42
|
+
this.converting = new resultMapConverter_1.ResultMapConverter({ map: this, converters: params.converters });
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
|
-
* Creates a new {@link Collections.
|
|
45
|
+
* Creates a new {@link Collections.ConvertingResultMap | ConvertingResultMap} instance.
|
|
46
46
|
* @param params - Required parameters for constructing the map.
|
|
47
47
|
* @returns `Success` with the new map if successful, `Failure` otherwise.
|
|
48
48
|
* @public
|
|
49
49
|
*/
|
|
50
|
-
static
|
|
51
|
-
return (0, base_1.captureResult)(() => new
|
|
50
|
+
static createConvertingResultMap(params) {
|
|
51
|
+
return (0, base_1.captureResult)(() => new ConvertingResultMap(params));
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
54
|
* {@inheritdoc Collections.ResultMap.add}
|
|
55
55
|
*/
|
|
56
56
|
add(key, value) {
|
|
57
|
-
return this.
|
|
57
|
+
return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => {
|
|
58
58
|
return super.add(entry[0], entry[1]);
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
getOrAdd(key, valueOrFactory) {
|
|
62
62
|
if (!this._isResultMapValueFactory(valueOrFactory)) {
|
|
63
|
-
return this.
|
|
63
|
+
return this.converting.converters.convertEntry([key, valueOrFactory]).onSuccess((entry) => {
|
|
64
64
|
return super.getOrAdd(entry[0], entry[1]);
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
else {
|
|
68
68
|
return super.get(key).onFailure(() => {
|
|
69
69
|
const value = valueOrFactory(key)
|
|
70
|
-
.onSuccess((value) => this.
|
|
71
|
-
.onSuccess(([
|
|
70
|
+
.onSuccess((value) => this.converting.converters.convertEntry([key, value]))
|
|
71
|
+
.onSuccess(([__key, value]) => (0, base_1.succeed)(value));
|
|
72
72
|
return value.success
|
|
73
73
|
? super.add(key, value.value)
|
|
74
74
|
: (0, base_1.failWithDetail)(value.message, 'invalid-value');
|
|
@@ -79,7 +79,7 @@ class ValidatingResultMap extends resultMap_1.ResultMap {
|
|
|
79
79
|
* {@inheritdoc Collections.ResultMap.set}
|
|
80
80
|
*/
|
|
81
81
|
set(key, value) {
|
|
82
|
-
return this.
|
|
82
|
+
return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => {
|
|
83
83
|
return super.set(entry[0], entry[1]);
|
|
84
84
|
});
|
|
85
85
|
}
|
|
@@ -87,7 +87,7 @@ class ValidatingResultMap extends resultMap_1.ResultMap {
|
|
|
87
87
|
* {@inheritdoc Collections.ResultMap.update}
|
|
88
88
|
*/
|
|
89
89
|
update(key, value) {
|
|
90
|
-
return this.
|
|
90
|
+
return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => {
|
|
91
91
|
return super.update(entry[0], entry[1]);
|
|
92
92
|
});
|
|
93
93
|
}
|
|
@@ -98,5 +98,5 @@ class ValidatingResultMap extends resultMap_1.ResultMap {
|
|
|
98
98
|
return this;
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
exports.
|
|
102
|
-
//# sourceMappingURL=
|
|
101
|
+
exports.ConvertingResultMap = ConvertingResultMap;
|
|
102
|
+
//# sourceMappingURL=convertingResultMap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertingResultMap.js","sourceRoot":"","sources":["../../../src/packlets/collections/convertingResultMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAyF;AAGzF,2CAA+D;AAC/D,6DAAuF;AAwBvF;;;;GAIG;AACH,MAAa,mBACX,SAAQ,qBAAi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AE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1C,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA7FD,kDA6FC","sourcesContent":["/*\n * Copyright (c) 2025 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 { captureResult, DetailedResult, failWithDetail, Result, succeed } from '../base';\nimport { KeyValueEntry } from './common';\nimport { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';\nimport { ResultMap, ResultMapValueFactory } from './resultMap';\nimport { IReadOnlyResultMapConverter, ResultMapConverter } from './resultMapConverter';\nimport { KeyValueConverters } from './keyValueConverters';\n\n/**\n * A read-only interface exposing non-mutating methods of a {@link Collections.ConvertingResultMap | ConvertingResultMap}.\n * @public\n */\nexport interface IReadOnlyConvertingResultMap<TK extends string = string, TV = unknown>\n extends IReadOnlyResultMap<TK, TV> {\n /**\n * {@inheritdoc Collections.ConvertingResultMap.converting}\n */\n readonly converting: IReadOnlyResultMapConverter<TK, TV>;\n}\n\n/**\n * Parameters for constructing a {@link Collections.ResultMap | ResultMap}.\n * @public\n */\nexport interface IConvertingResultMapConstructorParams<TK extends string = string, TV = unknown> {\n entries?: Iterable<KeyValueEntry<string, unknown>>;\n converters: KeyValueConverters<TK, TV>;\n}\n\n/**\n * A {@link Collections.ResultMap | ResultMap} with a {@link Collections.ResultMapConverter | validator}\n * property that enables validated use of the underlying map with weakly-typed keys and values.\n * @public\n */\nexport class ConvertingResultMap<TK extends string = string, TV = unknown>\n extends ResultMap<TK, TV>\n implements IReadOnlyConvertingResultMap<TK, TV>\n{\n /**\n * A {@link Collections.ResultMapConverter | ResultMapConverter} which validates keys and values\n * before inserting them into this collection.\n */\n public readonly converting: ResultMapConverter<TK, TV>;\n\n /**\n * Constructs a new {@link Collections.ConvertingResultMap | ConvertingResultMap}.\n * @param params - Required parameters for constructing the map.\n */\n public constructor(params: IConvertingResultMapConstructorParams<TK, TV>) {\n const entries = params.converters.convertEntries([...(params.entries ?? [])]).orThrow();\n super({ entries });\n this.converting = new ResultMapConverter<TK, TV>({ map: this, converters: params.converters });\n }\n\n /**\n * Creates a new {@link Collections.ConvertingResultMap | ConvertingResultMap} instance.\n * @param params - Required parameters for constructing the map.\n * @returns `Success` with the new map if successful, `Failure` otherwise.\n * @public\n */\n public static createConvertingResultMap<TK extends string = string, TV = unknown>(\n params: IConvertingResultMapConstructorParams<TK, TV>\n ): Result<ConvertingResultMap<TK, TV>> {\n return captureResult(() => new ConvertingResultMap(params));\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.add}\n */\n public add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => {\n return super.add(entry[0], entry[1]);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.(getOrAdd:1)}\n */\n public getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;\n /**\n * {@inheritdoc Collections.ResultMap.(getOrAdd:2)}\n */\n public getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;\n public getOrAdd(\n key: TK,\n valueOrFactory: TV | ResultMapValueFactory<TK, TV>\n ): DetailedResult<TV, ResultMapResultDetail> {\n if (!this._isResultMapValueFactory(valueOrFactory)) {\n return this.converting.converters.convertEntry([key, valueOrFactory]).onSuccess((entry) => {\n return super.getOrAdd(entry[0], entry[1]);\n });\n } else {\n return super.get(key).onFailure(() => {\n const value = valueOrFactory(key)\n .onSuccess((value) => this.converting.converters.convertEntry([key, value]))\n .onSuccess(([__key, value]) => succeed(value));\n return value.success\n ? super.add(key, value.value)\n : failWithDetail<TV, ResultMapResultDetail>(value.message, 'invalid-value');\n });\n }\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.set}\n */\n public set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => {\n return super.set(entry[0], entry[1]);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.update}\n */\n public update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => {\n return super.update(entry[0], entry[1]);\n });\n }\n\n /**\n * Gets a read-only version of this map.\n */\n public toReadOnly(): IReadOnlyConvertingResultMap<TK, TV> {\n return this;\n }\n}\n"]}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import * as Utils from './utils';
|
|
2
|
+
export * from './collectible';
|
|
3
|
+
export * from './collector';
|
|
4
|
+
export * from './collectorConverter';
|
|
5
|
+
export * from './collectors';
|
|
6
|
+
export * from './convertingCollector';
|
|
2
7
|
export * from './common';
|
|
8
|
+
export * from './keyValueConverters';
|
|
3
9
|
export * from './readonlyResultMap';
|
|
4
10
|
export * from './resultMap';
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
11
|
+
export * from './resultMapConverter';
|
|
12
|
+
export * from './convertingResultMap';
|
|
7
13
|
export { Utils };
|
|
8
14
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/index.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,cAAc,UAAU,CAAC;AACzB,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/index.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AACtC,cAAc,UAAU,CAAC;AACzB,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AAEtC,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -60,9 +60,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
60
60
|
exports.Utils = void 0;
|
|
61
61
|
const Utils = __importStar(require("./utils"));
|
|
62
62
|
exports.Utils = Utils;
|
|
63
|
+
__exportStar(require("./collectible"), exports);
|
|
64
|
+
__exportStar(require("./collector"), exports);
|
|
65
|
+
__exportStar(require("./collectorConverter"), exports);
|
|
66
|
+
__exportStar(require("./collectors"), exports);
|
|
67
|
+
__exportStar(require("./convertingCollector"), exports);
|
|
63
68
|
__exportStar(require("./common"), exports);
|
|
69
|
+
__exportStar(require("./keyValueConverters"), exports);
|
|
64
70
|
__exportStar(require("./readonlyResultMap"), exports);
|
|
65
71
|
__exportStar(require("./resultMap"), exports);
|
|
66
|
-
__exportStar(require("./
|
|
67
|
-
__exportStar(require("./
|
|
72
|
+
__exportStar(require("./resultMapConverter"), exports);
|
|
73
|
+
__exportStar(require("./convertingResultMap"), exports);
|
|
68
74
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/collections/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,+CAAiC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/collections/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,+CAAiC;AAcxB,sBAAK;AAZd,gDAA8B;AAC9B,8CAA4B;AAC5B,uDAAqC;AACrC,+CAA6B;AAC7B,wDAAsC;AACtC,2CAAyB;AACzB,uDAAqC;AACrC,sDAAoC;AACpC,8CAA4B;AAC5B,uDAAqC;AACrC,wDAAsC","sourcesContent":["/*\n * Copyright (c) 2025 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 Utils from './utils';\n\nexport * from './collectible';\nexport * from './collector';\nexport * from './collectorConverter';\nexport * from './collectors';\nexport * from './convertingCollector';\nexport * from './common';\nexport * from './keyValueConverters';\nexport * from './readonlyResultMap';\nexport * from './resultMap';\nexport * from './resultMapConverter';\nexport * from './convertingResultMap';\n\nexport { Utils };\n"]}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { DetailedResult, Result } from '../base';
|
|
2
|
+
import { Converter, ConverterFunc } from '../conversion';
|
|
3
|
+
import { Validator } from '../validation';
|
|
4
|
+
import { KeyValueEntry } from './common';
|
|
5
|
+
import { ResultMapResultDetail } from './readonlyResultMap';
|
|
6
|
+
/**
|
|
7
|
+
* Parameters for constructing a {@link Collections.KeyValueConverters | KeyValueConverters} instance.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export interface IKeyValueConverterConstructorParams<TK extends string = string, TV = unknown> {
|
|
11
|
+
/**
|
|
12
|
+
* Required key {@link Validator | validator}, {@link Converter | converter},
|
|
13
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
14
|
+
*/
|
|
15
|
+
key: Validator<TK, unknown> | Converter<TK, unknown> | ConverterFunc<TK, unknown>;
|
|
16
|
+
/**
|
|
17
|
+
* Required value {@link Validator | validator}, {@link Converter | converter},
|
|
18
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
19
|
+
*/
|
|
20
|
+
value: Validator<TV, unknown> | Converter<TV, unknown> | ConverterFunc<TV, unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* Optional entry {@link Validator | validator}, {@link Converter | converter},
|
|
23
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
24
|
+
* If no entry validator is provided, an entry is considered valid if both key and
|
|
25
|
+
* value are valid.
|
|
26
|
+
*/
|
|
27
|
+
entry?: Validator<KeyValueEntry<TK, TV>, unknown> | Converter<KeyValueEntry<TK, TV>, unknown> | ConverterFunc<KeyValueEntry<TK, TV>, unknown>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Helper class for converting strongly-typed keys, values, or entries
|
|
31
|
+
* from unknown values.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export declare class KeyValueConverters<TK extends string = string, TV = unknown> {
|
|
35
|
+
/**
|
|
36
|
+
* Required key {@link Validator | validator} or {@link Converter | converter}.
|
|
37
|
+
*/
|
|
38
|
+
readonly key: Validator<TK, unknown> | Converter<TK, unknown>;
|
|
39
|
+
/**
|
|
40
|
+
* Required value {@link Validator | validator} or {@link Converter | converter}.
|
|
41
|
+
*/
|
|
42
|
+
readonly value: Validator<TV, unknown> | Converter<TV, unknown>;
|
|
43
|
+
/**
|
|
44
|
+
* Optional entry {@link Validator | validator} or {@link Converter | converter}.
|
|
45
|
+
* If no entry validator is provided, an entry is considered valid if both key and
|
|
46
|
+
* value are valid.
|
|
47
|
+
*/
|
|
48
|
+
readonly entry?: Validator<KeyValueEntry<TK, TV>, unknown> | Converter<KeyValueEntry<TK, TV>, unknown>;
|
|
49
|
+
/**
|
|
50
|
+
* Constructs a new key-value validator.
|
|
51
|
+
* @param key - Required key {@link Validator | validator}, {@link Converter | converter},
|
|
52
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
53
|
+
* @param value - Required value {@link Validator | validator}, {@link Converter | converter},
|
|
54
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
55
|
+
* @param entry - Optional entry {@link Validator | validator}, {@link Converter | converter},
|
|
56
|
+
* or {@link Conversion.ConverterFunc | converter function}.. If no entry validator is provided,
|
|
57
|
+
* an entry is considered valid if both key and value are valid.
|
|
58
|
+
*/
|
|
59
|
+
constructor({ key, value, entry }: IKeyValueConverterConstructorParams<TK, TV>);
|
|
60
|
+
/**
|
|
61
|
+
* Converts a supplied unknown to a valid key value of type `<TK>`.
|
|
62
|
+
* @param key - The unknown to be converted.
|
|
63
|
+
* @returns `Success` with the converted key value and 'success' detail if the key is valid,
|
|
64
|
+
* or `Failure` with an error message and 'invalid-key' detail if the key is invalid.
|
|
65
|
+
*/
|
|
66
|
+
convertKey(key: unknown): DetailedResult<TK, ResultMapResultDetail>;
|
|
67
|
+
/**
|
|
68
|
+
* Converts a supplied unknown to a valid value of type `<TV>`.
|
|
69
|
+
* @param key - The unknown to be converted.
|
|
70
|
+
* @returns `Success` with the converted value and 'success' detail if the value is valid,
|
|
71
|
+
* or `Failure` with an error message and 'invalid-value' detail if the value is invalid.
|
|
72
|
+
*/
|
|
73
|
+
convertValue(key: unknown): DetailedResult<TV, ResultMapResultDetail>;
|
|
74
|
+
/**
|
|
75
|
+
* Converts a supplied unknown to a valid entry of type `[<TK>, <TV>]`.
|
|
76
|
+
* @param entry - The unknown to be converted.
|
|
77
|
+
* @returns `Success` with the converted entry and 'success' detail if the entry
|
|
78
|
+
* is valid, or `Failure` with an error message and 'invalid-key' or 'invalid-value' detail if
|
|
79
|
+
* the entry is invalid
|
|
80
|
+
*/
|
|
81
|
+
convertEntry(entry: unknown): DetailedResult<KeyValueEntry<TK, TV>, ResultMapResultDetail>;
|
|
82
|
+
/**
|
|
83
|
+
* Converts a supplied iterable of unknowns to valid key-value pairs.
|
|
84
|
+
* @param entries - The iterable of unknowns to be converted.
|
|
85
|
+
* @returns `Success` with an array of converted key-value pairs if all entries are valid,
|
|
86
|
+
* or `Failure` with an error message if any entry is invalid.
|
|
87
|
+
*/
|
|
88
|
+
convertEntries(entries: Iterable<unknown>): Result<KeyValueEntry<TK, TV>[]>;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=keyValueConverters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyValueConverters.d.ts","source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G;IACH,SAAgB,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IAEvE;;;;OAIG;IACH,SAAgB,KAAK,CAAC,EAClB,SAAS,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,OAAO,CAAC,GACzC,SAAS,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC;IAE9C;;;;;;;;;OASG;gBACgB,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,mCAAmC,CAAC,EAAE,EAAE,EAAE,CAAC;IAMrF;;;;;OAKG;IACI,UAAU,CAAC,GAAG,EAAE,OAAO,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAI1E;;;;;OAKG;IACI,YAAY,CAAC,GAAG,EAAE,OAAO,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAI5E;;;;;;OAMG;IACI,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,cAAc,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,qBAAqB,CAAC;IAkBjG;;;;;OAKG;IACI,cAAc,CAAC,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC;CAanF"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.KeyValueConverters = void 0;
|
|
25
|
+
const base_1 = require("../base");
|
|
26
|
+
const conversion_1 = require("../conversion");
|
|
27
|
+
/**
|
|
28
|
+
* Helper class for converting strongly-typed keys, values, or entries
|
|
29
|
+
* from unknown values.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
class KeyValueConverters {
|
|
33
|
+
/**
|
|
34
|
+
* Constructs a new key-value validator.
|
|
35
|
+
* @param key - Required key {@link Validator | validator}, {@link Converter | converter},
|
|
36
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
37
|
+
* @param value - Required value {@link Validator | validator}, {@link Converter | converter},
|
|
38
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
39
|
+
* @param entry - Optional entry {@link Validator | validator}, {@link Converter | converter},
|
|
40
|
+
* or {@link Conversion.ConverterFunc | converter function}.. If no entry validator is provided,
|
|
41
|
+
* an entry is considered valid if both key and value are valid.
|
|
42
|
+
*/
|
|
43
|
+
constructor({ key, value, entry }) {
|
|
44
|
+
this.key = typeof key === 'function' ? conversion_1.Converters.generic(key) : key;
|
|
45
|
+
this.value = typeof value === 'function' ? conversion_1.Converters.generic(value) : value;
|
|
46
|
+
this.entry = entry ? (typeof entry === 'function' ? conversion_1.Converters.generic(entry) : entry) : undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Converts a supplied unknown to a valid key value of type `<TK>`.
|
|
50
|
+
* @param key - The unknown to be converted.
|
|
51
|
+
* @returns `Success` with the converted key value and 'success' detail if the key is valid,
|
|
52
|
+
* or `Failure` with an error message and 'invalid-key' detail if the key is invalid.
|
|
53
|
+
*/
|
|
54
|
+
convertKey(key) {
|
|
55
|
+
return this.key.convert(key).withFailureDetail('invalid-key');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Converts a supplied unknown to a valid value of type `<TV>`.
|
|
59
|
+
* @param key - The unknown to be converted.
|
|
60
|
+
* @returns `Success` with the converted value and 'success' detail if the value is valid,
|
|
61
|
+
* or `Failure` with an error message and 'invalid-value' detail if the value is invalid.
|
|
62
|
+
*/
|
|
63
|
+
convertValue(key) {
|
|
64
|
+
return this.value.convert(key).withFailureDetail('invalid-value');
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Converts a supplied unknown to a valid entry of type `[<TK>, <TV>]`.
|
|
68
|
+
* @param entry - The unknown to be converted.
|
|
69
|
+
* @returns `Success` with the converted entry and 'success' detail if the entry
|
|
70
|
+
* is valid, or `Failure` with an error message and 'invalid-key' or 'invalid-value' detail if
|
|
71
|
+
* the entry is invalid
|
|
72
|
+
*/
|
|
73
|
+
convertEntry(entry) {
|
|
74
|
+
if (this.entry) {
|
|
75
|
+
return this.entry.convert(entry).withFailureDetail('invalid-value');
|
|
76
|
+
}
|
|
77
|
+
if (Array.isArray(entry) && entry.length === 2) {
|
|
78
|
+
const errors = new base_1.MessageAggregator();
|
|
79
|
+
const key = this.convertKey(entry[0]).aggregateError(errors).orDefault();
|
|
80
|
+
const value = this.convertValue(entry[1]).aggregateError(errors).orDefault();
|
|
81
|
+
if (key && value) {
|
|
82
|
+
return (0, base_1.succeedWithDetail)([key, value], 'success');
|
|
83
|
+
}
|
|
84
|
+
const detail = key ? 'invalid-value' : 'invalid-key';
|
|
85
|
+
return (0, base_1.failWithDetail)(`invalid entry: ${errors.toString()}`, detail);
|
|
86
|
+
}
|
|
87
|
+
/* c8 ignore next 2 */
|
|
88
|
+
return (0, base_1.failWithDetail)(`malformed entry: "${JSON.stringify(entry)}"`, 'invalid-value');
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Converts a supplied iterable of unknowns to valid key-value pairs.
|
|
92
|
+
* @param entries - The iterable of unknowns to be converted.
|
|
93
|
+
* @returns `Success` with an array of converted key-value pairs if all entries are valid,
|
|
94
|
+
* or `Failure` with an error message if any entry is invalid.
|
|
95
|
+
*/
|
|
96
|
+
convertEntries(entries) {
|
|
97
|
+
const errors = new base_1.MessageAggregator();
|
|
98
|
+
const converted = [];
|
|
99
|
+
for (const element of entries) {
|
|
100
|
+
this.convertEntry(element)
|
|
101
|
+
.onSuccess((e) => {
|
|
102
|
+
converted.push(e);
|
|
103
|
+
return (0, base_1.succeedWithDetail)(e, 'success');
|
|
104
|
+
})
|
|
105
|
+
.aggregateError(errors);
|
|
106
|
+
}
|
|
107
|
+
return errors.returnOrReport((0, base_1.succeed)(converted));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.KeyValueConverters = KeyValueConverters;
|
|
111
|
+
//# sourceMappingURL=keyValueConverters.js.map
|