@fgv/ts-utils
Advanced tools
Comparing version 4.3.0 to 4.4.0
@@ -5,2 +5,8 @@ { | ||
{ | ||
"version": "4.4.0", | ||
"tag": "@fgv/ts-utils_v4.4.0", | ||
"date": "Sat, 01 Feb 2025 17:13:10 GMT", | ||
"comments": {} | ||
}, | ||
{ | ||
"version": "4.3.0", | ||
@@ -7,0 +13,0 @@ "tag": "@fgv/ts-utils_v4.3.0", |
# Change Log - @fgv/ts-utils | ||
This log was last generated on Thu, 30 Jan 2025 00:35:17 GMT and should not be manually modified. | ||
This log was last generated on Sat, 01 Feb 2025 17:13:10 GMT and should not be manually modified. | ||
## 4.4.0 | ||
Sat, 01 Feb 2025 17:13:10 GMT | ||
_Version update only_ | ||
## 4.3.0 | ||
@@ -6,0 +11,0 @@ Thu, 30 Jan 2025 00:35:17 GMT |
@@ -5,7 +5,7 @@ import * as Collections from './packlets/collections'; | ||
import * as Validation from './packlets/validation'; | ||
import { ICollector, Collector, IReadOnlyResultMap, ResultMap, SimpleCollector, ConvertingResultMap } from './packlets/collections'; | ||
import { ICollectible, ICollector, Collector, ConvertingCollector, IReadOnlyResultMap, ResultMap, SimpleCollector, ConvertingResultMap } from './packlets/collections'; | ||
import { Converter, Converters, ObjectConverter, StringConverter } from './packlets/conversion'; | ||
import { Validator, Validators } from './packlets/validation'; | ||
export * from './packlets/base'; | ||
export { Collections, Collector, Conversion, Converter, Converters, Hash, ICollector, IReadOnlyResultMap, ObjectConverter, ResultMap, SimpleCollector, StringConverter, ConvertingResultMap, Validation, Validator, Validators }; | ||
export { Collections, Collector, Conversion, Converter, Converters, ConvertingCollector, Hash, ICollectible, ICollector, IReadOnlyResultMap, ObjectConverter, ResultMap, SimpleCollector, StringConverter, ConvertingResultMap, Validation, Validator, Validators }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -60,3 +60,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Validators = exports.Validation = exports.ConvertingResultMap = exports.StringConverter = exports.SimpleCollector = exports.ResultMap = exports.ObjectConverter = exports.Hash = exports.Converters = exports.Conversion = exports.Collector = exports.Collections = void 0; | ||
exports.Validators = exports.Validation = exports.ConvertingResultMap = exports.StringConverter = exports.SimpleCollector = exports.ResultMap = exports.ObjectConverter = exports.Hash = exports.ConvertingCollector = exports.Converters = exports.Conversion = exports.Collector = exports.Collections = void 0; | ||
const Collections = __importStar(require("./packlets/collections")); | ||
@@ -72,2 +72,3 @@ exports.Collections = Collections; | ||
Object.defineProperty(exports, "Collector", { enumerable: true, get: function () { return collections_1.Collector; } }); | ||
Object.defineProperty(exports, "ConvertingCollector", { enumerable: true, get: function () { return collections_1.ConvertingCollector; } }); | ||
Object.defineProperty(exports, "ResultMap", { enumerable: true, get: function () { return collections_1.ResultMap; } }); | ||
@@ -74,0 +75,0 @@ Object.defineProperty(exports, "SimpleCollector", { enumerable: true, get: function () { return collections_1.SimpleCollector; } }); |
import { Result } from '../base'; | ||
import { Converter, ConverterFunc } from '../conversion'; | ||
import { Validator } from '../validation'; | ||
/** | ||
@@ -9,3 +11,3 @@ * An item that can be collected by some {@link Collector | Collector}. | ||
readonly index: TINDEX | undefined; | ||
setIndex(index: TINDEX): Result<TINDEX>; | ||
setIndex(index: number): Result<TINDEX>; | ||
} | ||
@@ -24,2 +26,26 @@ /** | ||
/** | ||
* Parameters for constructing a new {@link Collections.ICollectible | ICollectible} instance with | ||
* a defined, strongly-typed index. | ||
* @public | ||
*/ | ||
export interface ICollectibleConstructorParamsWithIndex<TKEY extends string = string, TINDEX extends number = number> { | ||
key: TKEY; | ||
index: TINDEX; | ||
} | ||
/** | ||
* Parameters for constructing a new {@link Collections.ICollectible | ICollectible} instance with an | ||
* index converter. | ||
* @public | ||
*/ | ||
export interface ICollectibleConstructorParamsWithConverter<TKEY extends string = string, TINDEX extends number = number> { | ||
key: TKEY; | ||
index?: number; | ||
indexConverter: Validator<TINDEX, unknown> | Converter<TINDEX, unknown> | ConverterFunc<TINDEX, undefined>; | ||
} | ||
/** | ||
* Parameters for constructing a new {@link Collections.ICollectible | ICollectible} instance. | ||
* @public | ||
*/ | ||
export type ICollectibleConstructorParams<TKEY extends string = string, TINDEX extends number = number> = ICollectibleConstructorParamsWithIndex<TKEY, TINDEX> | ICollectibleConstructorParamsWithConverter<TKEY, TINDEX>; | ||
/** | ||
* Simple implementation of {@link Collections.ICollectible | ICollectible} which does not allow the index to be | ||
@@ -39,13 +65,19 @@ * changed once set. | ||
protected _index: TINDEX | undefined; | ||
protected readonly _indexConverter?: Validator<TINDEX, unknown> | Converter<TINDEX, unknown>; | ||
/** | ||
* Constructs a new {@link Collections.Collectible | Collectible} instance. | ||
* @param key - The key of the item to be collected. | ||
* @param index - Optional index of the item to be collected. | ||
* @param params - Required parameters for constructing the collectible. | ||
*/ | ||
constructor(key: TKEY, index?: TINDEX); | ||
constructor(params: ICollectibleConstructorParams<TKEY, TINDEX>); | ||
/** | ||
* Creates a new {@link Collections.Collectible | Collectible} instance from the supplied parameters. | ||
* @param params - Required parameters for constructing the collectible. | ||
* @returns `Success` with the new collectible if successful, or `Failure` with an error message if not. | ||
*/ | ||
static createCollectible<TKEY extends string = string, TINDEX extends number = number>(params: ICollectibleConstructorParams<TKEY, TINDEX>): Result<Collectible<TKEY, TINDEX>>; | ||
/** | ||
* {@link Collections.ICollectible.setIndex} | ||
*/ | ||
setIndex(index: TINDEX): Result<TINDEX>; | ||
setIndex(index: number): Result<TINDEX>; | ||
} | ||
//# sourceMappingURL=collectible.d.ts.map |
@@ -26,2 +26,3 @@ "use strict"; | ||
const base_1 = require("../base"); | ||
const conversion_1 = require("../conversion"); | ||
/** | ||
@@ -41,18 +42,51 @@ * Simple implementation of {@link Collections.ICollectible | ICollectible} which does not allow the index to be | ||
* Constructs a new {@link Collections.Collectible | Collectible} instance. | ||
* @param key - The key of the item to be collected. | ||
* @param index - Optional index of the item to be collected. | ||
* @param params - Required parameters for constructing the collectible. | ||
*/ | ||
constructor(key, index) { | ||
this.key = key; | ||
this._index = index; | ||
constructor(params) { | ||
this.key = params.key; | ||
if ('indexConverter' in params) { | ||
this._indexConverter = | ||
typeof params.indexConverter === 'function' | ||
? conversion_1.Converters.generic(params.indexConverter) | ||
: params.indexConverter; | ||
if (params.index !== undefined) { | ||
this._index = this._indexConverter.convert(params.index).orThrow(); | ||
} | ||
} | ||
else { | ||
this._index = params.index; | ||
} | ||
} | ||
/** | ||
* Creates a new {@link Collections.Collectible | Collectible} instance from the supplied parameters. | ||
* @param params - Required parameters for constructing the collectible. | ||
* @returns `Success` with the new collectible if successful, or `Failure` with an error message if not. | ||
*/ | ||
static createCollectible(params) { | ||
return (0, base_1.captureResult)(() => new Collectible(params)); | ||
} | ||
/** | ||
* {@link Collections.ICollectible.setIndex} | ||
*/ | ||
setIndex(index) { | ||
if (this._index !== undefined && index !== this.index) { | ||
let converted; | ||
if (this._indexConverter) { | ||
const { value, message } = this._indexConverter.convert(index); | ||
if (message) { | ||
return (0, base_1.fail)(message); | ||
} | ||
converted = value; | ||
} | ||
if (index === this.index) { | ||
return (0, base_1.succeed)(this.index); | ||
} | ||
else if (this._index !== undefined) { | ||
return (0, base_1.fail)(`index ${this.index} is immutable and cannot be changed to ${index}`); | ||
} | ||
this._index = index; | ||
return (0, base_1.succeed)(this._index); | ||
/* c8 ignore next 3 - should be impossible */ | ||
if (converted === undefined) { | ||
return (0, base_1.fail)(`index ${index} cannot be set on a Collectible without an index converter`); | ||
} | ||
this._index = converted; | ||
return (0, base_1.succeed)(converted); | ||
} | ||
@@ -59,0 +93,0 @@ } |
@@ -6,14 +6,44 @@ import { DetailedResult, Result } from '../base'; | ||
/** | ||
* Collects {@link Collections.ICollectible | ICollectible} items. Items in a collector are created by key and are assigned an index at the | ||
* time of addition. Items are immutable once added. | ||
* Additional success or failure details for mutating {@link ICollector | Collector} calls. | ||
* @public | ||
*/ | ||
export interface ICollector<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>, TSRC = TITEM> extends IReadOnlyResultMap<TKEY, TITEM> { | ||
export type CollectorResultDetail = ResultMapResultDetail | 'invalid-index'; | ||
/** | ||
* A read-only interface exposing non-mutating methods of a {@link Collections.ICollector | Collector}. | ||
* @public | ||
*/ | ||
export interface IReadOnlyCollector<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>> extends IReadOnlyResultMap<TKEY, TITEM> { | ||
/** | ||
* Gets the item at a specified index. | ||
* @param index - The index of the item to retrieve. | ||
* @returns Returs {@link Success | Success} with the item if it exists, or {@link Failure | Failure} with an error if the index is out of range. | ||
* @returns Returns {@link Success | Success} with the item if it exists, or {@link Failure | Failure} with an error if the index is out of range. | ||
*/ | ||
getAt(index: TINDEX): Result<TITEM>; | ||
getAt(index: number): Result<TITEM>; | ||
} | ||
/** | ||
* Collects {@link Collections.ICollectible | ICollectible} items. Items in a collector are created by key and are assigned an index at the | ||
* time of addition. Items are immutable once added. | ||
* @public | ||
*/ | ||
export interface ICollector<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>, TSRC = TITEM> extends IReadOnlyCollector<TKEY, TINDEX, TITEM> { | ||
/** | ||
* Adds an item to the collector using the default {@link Collections.CollectibleFactory | factory} | ||
* at a specified key, failing if an item with that key already exists. | ||
* @param key - The key of the item to add. | ||
* @param item - The source representation of the item to be added. | ||
* @returns Returns {@link Success | Success} with the item if it is added, or {@link Failure | Failure} with | ||
* an error if the item cannot be created and indexed. | ||
* @public | ||
*/ | ||
add(key: TKEY, item: TSRC): DetailedResult<TITEM, CollectorResultDetail>; | ||
add(key: TKEY, callback: CollectibleFactoryCallback<TKEY, TINDEX>): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* Adds an item to the collector using the default {@link Collections.CollectibleFactory | factory} | ||
* at a specified key, failing if an item with that key already exists. | ||
* @param item - The item to add. | ||
* @returns Returns {@link Success | Success} with the item if it is added, or {@link Failure | Failure} with an | ||
* error if the item cannot be created and indexed. | ||
*/ | ||
addItem(item: TITEM): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* Gets an item by key if it exists, or creates a new item and adds it using the default {@link Collections.CollectibleFactory | factory} if not. | ||
@@ -25,3 +55,3 @@ * @param key - The key of the item to retrieve. | ||
*/ | ||
getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>; | ||
getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
@@ -33,7 +63,15 @@ * Gets an item by key if it exists, or creates a new item and adds it using the specified {@link Collections.CollectibleFactoryCallback | factory callback} if not. | ||
*/ | ||
getOrAdd(key: TKEY, callback: CollectibleFactoryCallback<TKEY, TINDEX>): DetailedResult<TITEM, ResultMapResultDetail>; | ||
getOrAdd(key: TKEY, callback: CollectibleFactoryCallback<TKEY, TINDEX>): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* Gets an existing item with a key matching that of a supplied item, or adds the supplied | ||
* item to the collector if no item with that key exists. | ||
* @param item - The item to retrieve or add. | ||
* @returns Returns {@link Success | Success} with the item stored in the collector | ||
* or {@link Failure | Failure} with an error if the item cannot be created and indexed. | ||
*/ | ||
getOrAddItem(item: TITEM): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* Gets a {@link IReadOnlyResultMap | read-only map} which can access the items in the collector. | ||
*/ | ||
toReadOnly(): IReadOnlyResultMap<TKEY, TITEM>; | ||
toReadOnly(): IReadOnlyCollector<TKEY, TINDEX, TITEM>; | ||
} | ||
@@ -107,14 +145,30 @@ /** | ||
/** | ||
* {@inheritdoc ICollector.getAt} | ||
* {@inheritdoc Collections.IReadOnlyCollector.getAt} | ||
*/ | ||
getAt(index: TINDEX): Result<TITEM>; | ||
getAt(index: number): Result<TITEM>; | ||
/** | ||
* {@inheritdoc ICollector.(add:1)} | ||
*/ | ||
add(key: TKEY, item: TSRC): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc ICollector.(add:2)} | ||
*/ | ||
add(key: TKEY, cb: CollectibleFactoryCallback<TKEY, TINDEX, TITEM>): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc ICollector.addItem} | ||
*/ | ||
addItem(item: TITEM): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc ICollector.(getOrAdd:1)} | ||
*/ | ||
getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>; | ||
getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc ICollector.(getOrAdd:2)} | ||
*/ | ||
getOrAdd(key: TKEY, cb: CollectibleFactoryCallback<TKEY, TINDEX, TITEM>): DetailedResult<TITEM, ResultMapResultDetail>; | ||
getOrAdd(key: TKEY, cb: CollectibleFactoryCallback<TKEY, TINDEX, TITEM>): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc ICollector.getOrAddItem} | ||
*/ | ||
getOrAddItem(item: TITEM): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc IReadOnlyResultMap.has} | ||
@@ -126,3 +180,3 @@ */ | ||
*/ | ||
toReadOnly(): IReadOnlyResultMap<TKEY, TITEM>; | ||
toReadOnly(): IReadOnlyCollector<TKEY, TINDEX, TITEM>; | ||
/** | ||
@@ -133,4 +187,14 @@ * Gets an iterator over the map entries. | ||
[Symbol.iterator](): IterableIterator<KeyValueEntry<TKEY, TITEM>>; | ||
/** | ||
* A simple factory method for derived classes which directly store the supplied | ||
* object. | ||
* @param key - The key of the item to create. | ||
* @param index - The index of the item to create. | ||
* @param item - The source item to create. | ||
* @returns `Success` with the created item and a detail of 'success', or | ||
* `Failure` with an error message and appropriate detail. | ||
*/ | ||
protected static _simpleFactory<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>>(key: TKEY, index: number, item: TITEM): Result<TITEM>; | ||
protected _isFactoryCB(item: TSRC | CollectibleFactoryCallback<TKEY, TINDEX, TITEM>): item is CollectibleFactoryCallback<TKEY, TINDEX, TITEM>; | ||
} | ||
//# sourceMappingURL=collector.d.ts.map |
@@ -94,3 +94,3 @@ "use strict"; | ||
/** | ||
* {@inheritdoc ICollector.getAt} | ||
* {@inheritdoc Collections.IReadOnlyCollector.getAt} | ||
*/ | ||
@@ -103,2 +103,17 @@ getAt(index) { | ||
} | ||
add(key, itemOrCb) { | ||
if (this._byKey.has(key)) { | ||
return (0, base_1.failWithDetail)(`${key}: already exists.`, 'exists'); | ||
} | ||
return this.getOrAdd(key, itemOrCb); | ||
} | ||
/** | ||
* {@inheritdoc ICollector.addItem} | ||
*/ | ||
addItem(item) { | ||
if (this._byKey.has(item.key)) { | ||
return (0, base_1.failWithDetail)(`${item.key}: already exists.`, 'exists'); | ||
} | ||
return this.getOrAddItem(item); | ||
} | ||
getOrAdd(key, itemOrCb) { | ||
@@ -119,3 +134,3 @@ if (this._byKey.has(key)) { | ||
if (newItem.index !== this._byIndex.length) { | ||
return (0, base_1.failWithDetail)(`$[key}: index mismatch in created item - ${newItem.index} !== ${this._byIndex.length}`, 'invalid-value'); | ||
return (0, base_1.failWithDetail)(`$[key}: index mismatch in created item - ${newItem.index} !== ${this._byIndex.length}`, 'invalid-index'); | ||
} | ||
@@ -127,2 +142,17 @@ this._byKey.set(key, newItem); | ||
/** | ||
* {@inheritdoc ICollector.getOrAddItem} | ||
*/ | ||
getOrAddItem(item) { | ||
if (this._byKey.has(item.key)) { | ||
return (0, base_1.succeedWithDetail)(this._byKey.get(item.key), 'exists'); | ||
} | ||
const { message } = item.setIndex(this._byIndex.length); | ||
if (message !== undefined) { | ||
return (0, base_1.failWithDetail)(message, 'invalid-index'); | ||
} | ||
this._byKey.set(item.key, item); | ||
this._byIndex.push(item); | ||
return (0, base_1.succeedWithDetail)(item, 'added'); | ||
} | ||
/** | ||
* {@inheritdoc IReadOnlyResultMap.has} | ||
@@ -146,2 +176,14 @@ */ | ||
} | ||
/** | ||
* A simple factory method for derived classes which directly store the supplied | ||
* object. | ||
* @param key - The key of the item to create. | ||
* @param index - The index of the item to create. | ||
* @param item - The source item to create. | ||
* @returns `Success` with the created item and a detail of 'success', or | ||
* `Failure` with an error message and appropriate detail. | ||
*/ | ||
static _simpleFactory(key, index, item) { | ||
return item.setIndex(index).onSuccess(() => (0, base_1.succeed)(item)); | ||
} | ||
_isFactoryCB(item) { | ||
@@ -148,0 +190,0 @@ return typeof item === 'function'; |
@@ -6,5 +6,24 @@ import { DetailedResult } from '../base'; | ||
import { CollectibleFactoryCallback, ICollectible } from './collectible'; | ||
import { ICollector } from './collector'; | ||
import { CollectorResultDetail, ICollector } from './collector'; | ||
import { IReadOnlyResultMapConverter } from './resultMapConverter'; | ||
/** | ||
* A read-only interface exposing non-mutating methods of a | ||
* {@link Collections.CollectorConverter | CollectorConverter}. | ||
* @public | ||
*/ | ||
export interface IReadOnlyCollectorConverter<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY> = ICollectible<TKEY, TINDEX>> extends IReadOnlyResultMapConverter<TKEY, TITEM> { | ||
/** | ||
* {@inheritdoc Collections.CollectorConverter.map} | ||
*/ | ||
readonly map: IReadOnlyResultMap<TKEY, TITEM>; | ||
/** | ||
* {@inheritdoc Collections.Collector.get} | ||
*/ | ||
get(key: string): DetailedResult<TITEM, ResultMapResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.Collector.has} | ||
*/ | ||
has(key: string): boolean; | ||
} | ||
/** | ||
* Parameters for constructing a {@link Collections.CollectorConverter | CollectorConverter}. | ||
@@ -32,2 +51,10 @@ * @public | ||
/** | ||
* {@inheritdoc Collections.Collector.(add:1)} | ||
*/ | ||
add(key: string, value: unknown): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.Collector.(add:2)} | ||
*/ | ||
add(key: string, factory: ResultMapValueFactory<TKEY, TITEM>): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.Collector.get} | ||
@@ -39,7 +66,7 @@ */ | ||
*/ | ||
getOrAdd(key: string, value: unknown): DetailedResult<TITEM, ResultMapResultDetail>; | ||
getOrAdd(key: string, value: unknown): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.Collector.(getOrAdd:2)} | ||
*/ | ||
getOrAdd(key: string, factory: ResultMapValueFactory<TKEY, TITEM>): DetailedResult<TITEM, ResultMapResultDetail>; | ||
getOrAdd(key: string, factory: ResultMapValueFactory<TKEY, TITEM>): DetailedResult<TITEM, CollectorResultDetail>; | ||
/** | ||
@@ -52,3 +79,3 @@ * {@inheritdoc Collections.Collector.has} | ||
*/ | ||
toReadOnly(): IReadOnlyResultMapConverter<TKEY, TITEM>; | ||
toReadOnly(): IReadOnlyCollectorConverter<TKEY, TINDEX, TITEM>; | ||
/** | ||
@@ -55,0 +82,0 @@ * Determines if a value is a {@link Collections.CollectibleFactoryCallback | CollectibleFactoryCallback}. |
@@ -25,2 +25,3 @@ "use strict"; | ||
exports.CollectorConverter = void 0; | ||
const base_1 = require("../base"); | ||
/** | ||
@@ -43,2 +44,8 @@ * A {@link Collections.Collector | Collector} wrapper which validates weakly-typed keys | ||
} | ||
add(key, valueOrFactory) { | ||
if (this.has(key)) { | ||
return (0, base_1.failWithDetail)(`${key}: already exists`, 'exists'); | ||
} | ||
return this.getOrAdd(key, valueOrFactory); | ||
} | ||
/** | ||
@@ -54,10 +61,15 @@ * {@inheritdoc Collections.Collector.get} | ||
if (!this._isCollectibleFactoryCallback(valueOrFactory)) { | ||
return this.converters.convertEntry([key, valueOrFactory]).onSuccess(([vk, vs]) => { | ||
return this._collector.getOrAdd(vk, vs); | ||
}); | ||
const converted = this.converters.convertEntry([key, valueOrFactory]); | ||
if (converted.isFailure()) { | ||
return (0, base_1.failWithDetail)(converted.message, converted.detail); | ||
} | ||
const [vk, vs] = converted.value; | ||
return this._collector.getOrAdd(vk, vs); | ||
} | ||
else { | ||
return this.converters.convertKey(key).onSuccess((k) => { | ||
return this._collector.getOrAdd(k, valueOrFactory); | ||
}); | ||
const converted = this.converters.convertKey(key); | ||
if (converted.isFailure()) { | ||
return (0, base_1.failWithDetail)(converted.message, converted.detail); | ||
} | ||
return this._collector.getOrAdd(converted.value, valueOrFactory); | ||
} | ||
@@ -64,0 +76,0 @@ } |
@@ -30,4 +30,3 @@ import { Result } from '../base'; | ||
static createSimpleCollector<TITEM extends ICollectible<string, number>>(params?: ISimpleCollectorCreateParams<TITEM>): Result<SimpleCollector<TITEM>>; | ||
private static _factory; | ||
} | ||
//# sourceMappingURL=collectors.d.ts.map |
@@ -41,3 +41,3 @@ "use strict"; | ||
const entries = (_a = params === null || params === void 0 ? void 0 : params.items) === null || _a === void 0 ? void 0 : _a.map((item) => [item.key, item]); | ||
super({ factory: SimpleCollector._factory, entries }); | ||
super({ factory: collector_1.Collector._simpleFactory, entries }); | ||
} | ||
@@ -53,7 +53,4 @@ /** | ||
} | ||
static _factory(key, index, item) { | ||
return item.setIndex(index).onSuccess(() => (0, base_1.succeed)(item)); | ||
} | ||
} | ||
exports.SimpleCollector = SimpleCollector; | ||
//# sourceMappingURL=collectors.js.map |
@@ -1,10 +0,24 @@ | ||
import { DetailedResult, Result } from '../base'; | ||
import { CollectibleFactory, CollectibleFactoryCallback, ICollectible } from './collectible'; | ||
import { Result } from '../base'; | ||
import { CollectibleFactory, ICollectible } from './collectible'; | ||
import { Collector } from './collector'; | ||
import { CollectorConverter } from './collectorConverter'; | ||
import { CollectorConverter, IReadOnlyCollectorConverter } from './collectorConverter'; | ||
import { KeyValueEntry } from './common'; | ||
import { IReadOnlyConvertingResultMap } from './convertingResultMap'; | ||
import { KeyValueConverters } from './keyValueConverters'; | ||
import { ResultMapResultDetail } from './readonlyResultMap'; | ||
/** | ||
* A read-only interface exposing non-mutating methods of a | ||
* {@link Collections.ConvertingCollector | ConvertingCollector}. | ||
* @public | ||
*/ | ||
export interface IReadOnlyConvertingCollector<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>> extends IReadOnlyConvertingResultMap<TKEY, TITEM> { | ||
/** | ||
* {@inheritdoc Collections.ConvertingCollector.converting} | ||
*/ | ||
readonly converting: IReadOnlyCollectorConverter<TKEY, TINDEX, TITEM>; | ||
/** | ||
* {@inheritdoc Collections.Collector.getAt} | ||
*/ | ||
readonly getAt: (index: number) => Result<TITEM>; | ||
} | ||
/** | ||
* Parameters for constructing a {@link Collections.ConvertingCollector | ConvertingCollector}. | ||
@@ -53,10 +67,2 @@ * @public | ||
/** | ||
* {@inheritdoc Collections.Collector.(getOrAdd:1)} | ||
*/ | ||
getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.Collector.(getOrAdd:2)} | ||
*/ | ||
getOrAdd(key: TKEY, cb: CollectibleFactoryCallback<TKEY, TINDEX, TITEM>): DetailedResult<TITEM, ResultMapResultDetail>; | ||
/** | ||
* Gets a read-only version of this collector as a | ||
@@ -66,4 +72,4 @@ * {@link Collections.IReadOnlyConvertingResultMap | read-only map}. | ||
*/ | ||
toReadOnly(): IReadOnlyConvertingResultMap<TKEY, TITEM>; | ||
toReadOnly(): IReadOnlyConvertingCollector<TKEY, TINDEX, TITEM>; | ||
} | ||
//# sourceMappingURL=convertingCollector.d.ts.map |
@@ -57,12 +57,2 @@ "use strict"; | ||
} | ||
getOrAdd(key, itemOrCb) { | ||
if (this._isFactoryCB(itemOrCb)) { | ||
return this.converting.converters.convertKey(key).onSuccess((k) => { | ||
return super.getOrAdd(k, itemOrCb); | ||
}); | ||
} | ||
return this.converting.converters.convertEntry([key, itemOrCb]).onSuccess(([k, v]) => { | ||
return super.getOrAdd(k, v); | ||
}); | ||
} | ||
/** | ||
@@ -69,0 +59,0 @@ * Gets a read-only version of this collector as a |
@@ -1,7 +0,7 @@ | ||
import { DetailedResult, Result } from '../base'; | ||
import { Result } from '../base'; | ||
import { KeyValueEntry } from './common'; | ||
import { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap'; | ||
import { ResultMap, ResultMapValueFactory } from './resultMap'; | ||
import { KeyValueConverters } from './keyValueConverters'; | ||
import { IReadOnlyResultMap } from './readonlyResultMap'; | ||
import { ResultMap } from './resultMap'; | ||
import { IReadOnlyResultMapConverter, ResultMapConverter } from './resultMapConverter'; | ||
import { KeyValueConverters } from './keyValueConverters'; | ||
/** | ||
@@ -49,22 +49,2 @@ * A read-only interface exposing non-mutating methods of a {@link Collections.ConvertingResultMap | ConvertingResultMap}. | ||
/** | ||
* {@inheritdoc Collections.ResultMap.add} | ||
*/ | ||
add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.ResultMap.(getOrAdd:1)} | ||
*/ | ||
getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.ResultMap.(getOrAdd:2)} | ||
*/ | ||
getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.ResultMap.set} | ||
*/ | ||
set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>; | ||
/** | ||
* {@inheritdoc Collections.ResultMap.update} | ||
*/ | ||
update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>; | ||
/** | ||
* Gets a read-only version of this map. | ||
@@ -71,0 +51,0 @@ */ |
@@ -54,43 +54,2 @@ "use strict"; | ||
/** | ||
* {@inheritdoc Collections.ResultMap.add} | ||
*/ | ||
add(key, value) { | ||
return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => { | ||
return super.add(entry[0], entry[1]); | ||
}); | ||
} | ||
getOrAdd(key, valueOrFactory) { | ||
if (!this._isResultMapValueFactory(valueOrFactory)) { | ||
return this.converting.converters.convertEntry([key, valueOrFactory]).onSuccess((entry) => { | ||
return super.getOrAdd(entry[0], entry[1]); | ||
}); | ||
} | ||
else { | ||
return super.get(key).onFailure(() => { | ||
const value = valueOrFactory(key) | ||
.onSuccess((value) => this.converting.converters.convertEntry([key, value])) | ||
.onSuccess(([__key, value]) => (0, base_1.succeed)(value)); | ||
return value.success | ||
? super.add(key, value.value) | ||
: (0, base_1.failWithDetail)(value.message, 'invalid-value'); | ||
}); | ||
} | ||
} | ||
/** | ||
* {@inheritdoc Collections.ResultMap.set} | ||
*/ | ||
set(key, value) { | ||
return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => { | ||
return super.set(entry[0], entry[1]); | ||
}); | ||
} | ||
/** | ||
* {@inheritdoc Collections.ResultMap.update} | ||
*/ | ||
update(key, value) { | ||
return this.converting.converters.convertEntry([key, value]).onSuccess((entry) => { | ||
return super.update(entry[0], entry[1]); | ||
}); | ||
} | ||
/** | ||
* Gets a read-only version of this map. | ||
@@ -97,0 +56,0 @@ */ |
@@ -21,9 +21,2 @@ import { DetailedResult, Result } from '../base'; | ||
value: Validator<TV, unknown> | Converter<TV, unknown> | ConverterFunc<TV, unknown>; | ||
/** | ||
* Optional entry {@link Validator | validator}, {@link Converter | converter}, | ||
* or {@link Conversion.ConverterFunc | converter function}. | ||
* If no entry validator is provided, an entry is considered valid if both key and | ||
* value are valid. | ||
*/ | ||
entry?: Validator<KeyValueEntry<TK, TV>, unknown> | Converter<KeyValueEntry<TK, TV>, unknown> | ConverterFunc<KeyValueEntry<TK, TV>, unknown>; | ||
} | ||
@@ -45,8 +38,2 @@ /** | ||
/** | ||
* Optional entry {@link Validator | validator} or {@link Converter | converter}. | ||
* If no entry validator is provided, an entry is considered valid if both key and | ||
* value are valid. | ||
*/ | ||
readonly entry?: Validator<KeyValueEntry<TK, TV>, unknown> | Converter<KeyValueEntry<TK, TV>, unknown>; | ||
/** | ||
* Constructs a new key-value validator. | ||
@@ -57,7 +44,4 @@ * @param key - Required key {@link Validator | validator}, {@link Converter | converter}, | ||
* or {@link Conversion.ConverterFunc | converter function}. | ||
* @param entry - Optional entry {@link Validator | validator}, {@link Converter | converter}, | ||
* or {@link Conversion.ConverterFunc | converter function}.. If no entry validator is provided, | ||
* an entry is considered valid if both key and value are valid. | ||
*/ | ||
constructor({ key, value, entry }: IKeyValueConverterConstructorParams<TK, TV>); | ||
constructor({ key, value }: IKeyValueConverterConstructorParams<TK, TV>); | ||
/** | ||
@@ -64,0 +48,0 @@ * Converts a supplied unknown to a valid key value of type `<TK>`. |
@@ -39,10 +39,6 @@ "use strict"; | ||
* or {@link Conversion.ConverterFunc | converter function}. | ||
* @param entry - Optional entry {@link Validator | validator}, {@link Converter | converter}, | ||
* or {@link Conversion.ConverterFunc | converter function}.. If no entry validator is provided, | ||
* an entry is considered valid if both key and value are valid. | ||
*/ | ||
constructor({ key, value, entry }) { | ||
constructor({ key, value }) { | ||
this.key = typeof key === 'function' ? conversion_1.Converters.generic(key) : key; | ||
this.value = typeof value === 'function' ? conversion_1.Converters.generic(value) : value; | ||
this.entry = entry ? (typeof entry === 'function' ? conversion_1.Converters.generic(entry) : entry) : undefined; | ||
} | ||
@@ -75,5 +71,2 @@ /** | ||
convertEntry(entry) { | ||
if (this.entry) { | ||
return this.entry.convert(entry).withFailureDetail('invalid-value'); | ||
} | ||
if (Array.isArray(entry) && entry.length === 2) { | ||
@@ -80,0 +73,0 @@ const errors = new base_1.MessageAggregator(); |
{ | ||
"name": "@fgv/ts-utils", | ||
"version": "4.3.0", | ||
"version": "4.4.0", | ||
"description": "Assorted Typescript Utilities", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1093023
13132