@fgv/ts-utils 4.3.0 → 4.5.0-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.
Files changed (112) hide show
  1. package/CHANGELOG.json +6 -0
  2. package/CHANGELOG.md +6 -1
  3. package/dist/ts-utils.d.ts +4248 -3531
  4. package/dist/tsdoc-metadata.json +1 -1
  5. package/lib/index.d.ts +3 -2
  6. package/lib/index.d.ts.map +1 -1
  7. package/lib/index.js +7 -3
  8. package/lib/index.js.map +1 -1
  9. package/lib/packlets/base/utils.d.ts.map +1 -1
  10. package/lib/packlets/base/utils.js +0 -1
  11. package/lib/packlets/base/utils.js.map +1 -1
  12. package/lib/packlets/collections/collectible.d.ts +82 -8
  13. package/lib/packlets/collections/collectible.d.ts.map +1 -1
  14. package/lib/packlets/collections/collectible.js +36 -11
  15. package/lib/packlets/collections/collectible.js.map +1 -1
  16. package/lib/packlets/collections/collector.d.ts +78 -79
  17. package/lib/packlets/collections/collector.d.ts.map +1 -1
  18. package/lib/packlets/collections/collector.js +83 -52
  19. package/lib/packlets/collections/collector.js.map +1 -1
  20. package/lib/packlets/collections/collectorValidator.d.ts +89 -0
  21. package/lib/packlets/collections/collectorValidator.d.ts.map +1 -0
  22. package/lib/packlets/collections/collectorValidator.js +98 -0
  23. package/lib/packlets/collections/collectorValidator.js.map +1 -0
  24. package/lib/packlets/collections/convertingCollector.d.ts +77 -36
  25. package/lib/packlets/collections/convertingCollector.d.ts.map +1 -1
  26. package/lib/packlets/collections/convertingCollector.js +71 -26
  27. package/lib/packlets/collections/convertingCollector.js.map +1 -1
  28. package/lib/packlets/collections/convertingCollectorValidator.d.ts +69 -0
  29. package/lib/packlets/collections/convertingCollectorValidator.d.ts.map +1 -0
  30. package/lib/packlets/collections/{collectorConverter.js → convertingCollectorValidator.js} +28 -15
  31. package/lib/packlets/collections/convertingCollectorValidator.js.map +1 -0
  32. package/lib/packlets/collections/index.d.ts +7 -5
  33. package/lib/packlets/collections/index.d.ts.map +1 -1
  34. package/lib/packlets/collections/index.js +7 -5
  35. package/lib/packlets/collections/index.js.map +1 -1
  36. package/lib/packlets/collections/keyValueConverters.d.ts +1 -17
  37. package/lib/packlets/collections/keyValueConverters.d.ts.map +1 -1
  38. package/lib/packlets/collections/keyValueConverters.js +1 -8
  39. package/lib/packlets/collections/keyValueConverters.js.map +1 -1
  40. package/lib/packlets/collections/readonlyResultMap.d.ts +0 -4
  41. package/lib/packlets/collections/readonlyResultMap.d.ts.map +1 -1
  42. package/lib/packlets/collections/readonlyResultMap.js.map +1 -1
  43. package/lib/packlets/collections/resultMap.d.ts +0 -4
  44. package/lib/packlets/collections/resultMap.d.ts.map +1 -1
  45. package/lib/packlets/collections/resultMap.js +0 -6
  46. package/lib/packlets/collections/resultMap.js.map +1 -1
  47. package/lib/packlets/collections/{resultMapConverter.d.ts → resultMapValidator.d.ts} +11 -11
  48. package/lib/packlets/collections/{resultMapConverter.d.ts.map → resultMapValidator.d.ts.map} +1 -1
  49. package/lib/packlets/collections/{resultMapConverter.js → resultMapValidator.js} +6 -6
  50. package/lib/packlets/collections/{resultMapConverter.js.map → resultMapValidator.js.map} +1 -1
  51. package/lib/packlets/collections/validatingCollector.d.ts +69 -0
  52. package/lib/packlets/collections/validatingCollector.d.ts.map +1 -0
  53. package/lib/packlets/collections/validatingCollector.js +67 -0
  54. package/lib/packlets/collections/validatingCollector.js.map +1 -0
  55. package/lib/packlets/collections/validatingConvertingCollector.d.ts +59 -0
  56. package/lib/packlets/collections/validatingConvertingCollector.d.ts.map +1 -0
  57. package/lib/packlets/collections/validatingConvertingCollector.js +68 -0
  58. package/lib/packlets/collections/validatingConvertingCollector.js.map +1 -0
  59. package/lib/packlets/collections/validatingResultMap.d.ts +53 -0
  60. package/lib/packlets/collections/validatingResultMap.d.ts.map +1 -0
  61. package/lib/packlets/collections/validatingResultMap.js +61 -0
  62. package/lib/packlets/collections/validatingResultMap.js.map +1 -0
  63. package/lib/packlets/file-tree/directoryItem.d.ts +47 -0
  64. package/lib/packlets/file-tree/directoryItem.d.ts.map +1 -0
  65. package/lib/packlets/file-tree/directoryItem.js +71 -0
  66. package/lib/packlets/file-tree/directoryItem.js.map +1 -0
  67. package/lib/packlets/file-tree/fileItem.d.ts +64 -0
  68. package/lib/packlets/file-tree/fileItem.d.ts.map +1 -0
  69. package/lib/packlets/file-tree/fileItem.js +93 -0
  70. package/lib/packlets/file-tree/fileItem.js.map +1 -0
  71. package/lib/packlets/file-tree/fileTree.d.ts +84 -0
  72. package/lib/packlets/file-tree/fileTree.d.ts.map +1 -0
  73. package/lib/packlets/file-tree/fileTree.js +135 -0
  74. package/lib/packlets/file-tree/fileTree.js.map +1 -0
  75. package/lib/packlets/file-tree/fileTreeAccessors.d.ts +134 -0
  76. package/lib/packlets/file-tree/fileTreeAccessors.d.ts.map +1 -0
  77. package/lib/packlets/file-tree/fileTreeAccessors.js +24 -0
  78. package/lib/packlets/file-tree/fileTreeAccessors.js.map +1 -0
  79. package/lib/packlets/file-tree/fsTree.d.ts +45 -0
  80. package/lib/packlets/file-tree/fsTree.d.ts.map +1 -0
  81. package/lib/packlets/file-tree/fsTree.js +116 -0
  82. package/lib/packlets/file-tree/fsTree.js.map +1 -0
  83. package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts +67 -0
  84. package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts.map +1 -0
  85. package/lib/packlets/file-tree/in-memory/inMemoryTree.js +150 -0
  86. package/lib/packlets/file-tree/in-memory/inMemoryTree.js.map +1 -0
  87. package/lib/packlets/file-tree/in-memory/index.d.ts +2 -0
  88. package/lib/packlets/file-tree/in-memory/index.d.ts.map +1 -0
  89. package/lib/packlets/file-tree/in-memory/index.js +39 -0
  90. package/lib/packlets/file-tree/in-memory/index.js.map +1 -0
  91. package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts +106 -0
  92. package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts.map +1 -0
  93. package/lib/packlets/file-tree/in-memory/treeBuilder.js +170 -0
  94. package/lib/packlets/file-tree/in-memory/treeBuilder.js.map +1 -0
  95. package/lib/packlets/file-tree/index.d.ts +8 -0
  96. package/lib/packlets/file-tree/index.d.ts.map +1 -0
  97. package/lib/packlets/file-tree/index.js +46 -0
  98. package/lib/packlets/file-tree/index.js.map +1 -0
  99. package/lib/packlets/hash/crcNormalizer.js +1 -1
  100. package/lib/packlets/hash/crcNormalizer.js.map +1 -1
  101. package/package.json +15 -15
  102. package/lib/packlets/collections/collectorConverter.d.ts +0 -59
  103. package/lib/packlets/collections/collectorConverter.d.ts.map +0 -1
  104. package/lib/packlets/collections/collectorConverter.js.map +0 -1
  105. package/lib/packlets/collections/collectors.d.ts +0 -32
  106. package/lib/packlets/collections/collectors.d.ts.map +0 -1
  107. package/lib/packlets/collections/collectors.js +0 -57
  108. package/lib/packlets/collections/collectors.js.map +0 -1
  109. package/lib/packlets/collections/convertingResultMap.d.ts +0 -73
  110. package/lib/packlets/collections/convertingResultMap.d.ts.map +0 -1
  111. package/lib/packlets/collections/convertingResultMap.js +0 -102
  112. package/lib/packlets/collections/convertingResultMap.js.map +0 -1
@@ -1,130 +1,129 @@
1
1
  import { DetailedResult, Result } from '../base';
2
- import { ICollectible, CollectibleFactoryCallback, CollectibleFactory } from './collectible';
2
+ import { CollectibleFactoryCallback, CollectibleKey, ICollectible } from './collectible';
3
3
  import { KeyValueEntry } from './common';
4
4
  import { IReadOnlyResultMap, ResultMapForEachCb, ResultMapResultDetail } from './readonlyResultMap';
5
5
  /**
6
- * Collects {@link Collections.ICollectible | ICollectible} items. Items in a collector are created by key and are assigned an index at the
7
- * time of addition. Items are immutable once added.
6
+ * Additional success or failure details for mutating collector calls.
8
7
  * @public
9
8
  */
10
- 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> {
9
+ export type CollectorResultDetail = ResultMapResultDetail | 'invalid-index';
10
+ /**
11
+ * A read-only interface exposing only the non-mutating methods of a {@link Collections.Collector | ICollector}.
12
+ * @public
13
+ */
14
+ export interface IReadOnlyCollector<TITEM extends ICollectible<any, any>> extends IReadOnlyResultMap<CollectibleKey<TITEM>, TITEM> {
11
15
  /**
12
16
  * Gets the item at a specified index.
13
17
  * @param index - The index of the item to retrieve.
14
- * @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.
15
- */
16
- getAt(index: TINDEX): Result<TITEM>;
17
- /**
18
- * 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.
19
- * @param key - The key of the item to retrieve.
20
- * @param item - The source representation of the item to be added if it does not exist.
21
- * @returns Returns {@link Success | Success} with the item if it exists or could be created, or {@link Failure | Failure} with an error if the
22
- * item cannot be created and indexed.
23
- */
24
- getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>;
25
- /**
26
- * 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.
27
- * @param key - The key of the item to retrieve.
28
- * @param callback - The factory callback to create the item if it does not exist.
29
- * @returns Returns {@link Success | Success} with the item if it exists, or {@link Failure | Failure} with an error if the item is not found.
30
- */
31
- getOrAdd(key: TKEY, callback: CollectibleFactoryCallback<TKEY, TINDEX>): DetailedResult<TITEM, ResultMapResultDetail>;
32
- /**
33
- * Gets a {@link IReadOnlyResultMap | read-only map} which can access the items in the collector.
18
+ * @returns Returns {@link Success | Success} with the item if it exists, or {@link Failure | Failure}
19
+ * with an error if the index is out of range.
34
20
  */
35
- toReadOnly(): IReadOnlyResultMap<TKEY, TITEM>;
21
+ getAt(index: number): Result<TITEM>;
36
22
  }
37
23
  /**
38
- * A simple {@link ICollector | ICollector} with non-branded `string` key and `number` index, and no transformation of source items.
39
- * @public
40
- */
41
- export type ISimpleCollector<TITEM extends ICollectible<string, number>> = ICollector<string, number, TITEM, TITEM>;
42
- /**
43
- * A {@link ICollector | Collector} with non-branded `string` key and `number` index, and transformation of source items.
24
+ * Parameters for constructing a {@link Collections.Collector | ICollector}.
44
25
  * @public
45
26
  */
46
- export type IConvertingCollector<TITEM extends ICollectible<string, number>, TSRC> = ICollector<string, number, TITEM, TSRC>;
27
+ export interface ICollectorConstructorParams<TITEM extends ICollectible<any, any>> {
28
+ items?: TITEM[];
29
+ }
47
30
  /**
48
- * Parameters for constructing a {@link ICollector | Collector}.
31
+ * A {@link Collections.Collector | Collector} that is a specialized collection
32
+ * which contains items of type {@link Collections.ICollectible | ICollectible},
33
+ * which have a unique key and a write-once index.
34
+ *
35
+ * Items are assigned an index sequentially as they are added to the collection.
36
+ * Once added, items are immutable - they cannot be removed or replaced.
49
37
  * @public
50
38
  */
51
- export interface ICollectorConstructorParams<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>, TSRC = TITEM> {
52
- /**
53
- * The default {@link Collections.CollectibleFactory | factory} to create items.
54
- */
55
- factory: CollectibleFactory<TKEY, TINDEX, TITEM, TSRC>;
39
+ export declare class Collector<TITEM extends ICollectible<any, any>> implements IReadOnlyCollector<TITEM> {
40
+ private readonly _byKey;
41
+ private readonly _byIndex;
56
42
  /**
57
- * An optional array of entries to add to the collector.
43
+ * {@inheritdoc Collections.ResultMap.size}
58
44
  */
59
- entries?: KeyValueEntry<TKEY, TSRC>[];
60
- }
61
- /**
62
- * A {@link ICollector | Collector} that collects {@link Collections.ICollectible | ICollectible} items. Items in a collector are created by key and are assigned an index at the
63
- * time of addition. Items are immutable once added. Keys and indexes might be branded types, and source items might be transformed on addition.
64
- * @public
65
- */
66
- export declare class Collector<TKEY extends string = string, TINDEX extends number = number, TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>, TSRC = TITEM> implements ICollector<TKEY, TINDEX, TITEM, TSRC> {
67
- private _byKey;
68
- private _byIndex;
69
- private _factory;
45
+ get size(): number;
70
46
  /**
71
- * {@inheritdoc IReadOnlyResultMap.inner}
47
+ * Constructs a new {@link Collections.Collector | Collector}.
48
+ * @param params - Optional {@link Collections.ICollectorConstructorParams | initialization parameters} used
49
+ * to construct the collector.
72
50
  */
73
- get inner(): ReadonlyMap<TKEY, TITEM>;
51
+ constructor(params?: ICollectorConstructorParams<TITEM>);
74
52
  /**
75
- * Constructor for derived classes.
76
- * @param params - Parameters for constructing the collector.
53
+ * Creates a new {@link Collections.Collector | Collector} instance.
54
+ * @param params - Optional {@link Collections.ICollectorConstructorParams | initialization parameters} used
55
+ * to create the collector.
56
+ * @returns Returns {@link Success | Success} with the new collector if it was created successfully,
57
+ * or {@link Failure | Failure} with an error if the collector could not be created.
77
58
  */
78
- protected constructor(params: ICollectorConstructorParams<TKEY, TINDEX, TITEM, TSRC>);
59
+ static createCollector<TITEM extends ICollectible<any, any>>(params?: ICollectorConstructorParams<TITEM>): Result<Collector<TITEM>>;
79
60
  /**
80
- * {@inheritdoc IReadOnlyResultMap.size}
61
+ * Adds an item to the collection, failing if a different item with the same key already exists. Note
62
+ * that adding an object that is already in the collection again will succeed without updating the collection.
63
+ * @param item - The item to add.
64
+ * @returns Returns {@link DetailedSuccess | Success} with the item and detail `added` if it was added
65
+ * or detail `exists` if the item was already in the map. Returns {@link DetailedFailure | Failure} with
66
+ * an error message and appropriate detail if the item could not be added.
81
67
  */
82
- get size(): number;
68
+ add(item: TITEM): DetailedResult<TITEM, CollectorResultDetail>;
83
69
  /**
84
- * {@inheritdoc IReadOnlyResultMap.keys}
70
+ * {@inheritdoc Collections.ResultMap.entries}
85
71
  */
86
- keys(): IterableIterator<TKEY>;
72
+ entries(): MapIterator<KeyValueEntry<CollectibleKey<TITEM>, TITEM>>;
87
73
  /**
88
- * {@inheritdoc IReadOnlyResultMap.values}
74
+ * {@inheritdoc Collections.ResultMap.forEach}
89
75
  */
90
- values(): IterableIterator<TITEM>;
76
+ forEach(callback: ResultMapForEachCb<CollectibleKey<TITEM>, TITEM>, arg?: unknown): void;
91
77
  /**
92
- * {@inheritdoc IReadOnlyResultMap.entries}
78
+ * {@inheritdoc Collections.ResultMap.get}
93
79
  */
94
- entries(): IterableIterator<[TKEY, TITEM]>;
80
+ get(key: CollectibleKey<TITEM>): DetailedResult<TITEM, ResultMapResultDetail>;
95
81
  /**
96
- * {@inheritdoc IReadOnlyResultMap.forEach}
82
+ * {@inheritdoc Collections.IReadOnlyCollector.getAt}
97
83
  */
98
- forEach(callback: ResultMapForEachCb<TKEY, TITEM>, arg?: unknown): void;
84
+ getAt(index: number): Result<TITEM>;
99
85
  /**
100
- * {@inheritdoc IReadOnlyResultMap.get}
86
+ * Gets an existing item with a key matching that of a supplied item, or adds the supplied
87
+ * item to the collector if no item with that key exists.
88
+ * @param item - The item to get or add.
89
+ * @returns Returns {@link DetailedSuccess | Success} with the item stored in the collector -
90
+ * detail `exists` indicates that an existing item return and detail `added` indicates that the
91
+ * item was added. Returns {@link DetailedFailure | Failure} with an error and appropriate
92
+ * detail if the item could not be added.
101
93
  */
102
- get(key: TKEY): DetailedResult<TITEM, ResultMapResultDetail>;
94
+ getOrAdd(item: TITEM): DetailedResult<TITEM, CollectorResultDetail>;
103
95
  /**
104
- * {@inheritdoc ICollector.getAt}
96
+ * Gets an existing item with a key matching the supplied key, or adds a new item to the collector
97
+ * using a factory callback if no item with that key exists.
98
+ * @param key - The key of the item to add.
99
+ * @param callback - The factory callback to create the item.
100
+ * @returns Returns {@link DetailedSuccess | Success} with the item stored in the collector -
101
+ * detail `exists` indicates that an existing item return and detail `added` indicates that the
102
+ * item was added. Returns {@link DetailedFailure | Failure} with an error and appropriate
103
+ * detail if the item could not be added.
105
104
  */
106
- getAt(index: TINDEX): Result<TITEM>;
105
+ getOrAdd(key: CollectibleKey<TITEM>, factory: CollectibleFactoryCallback<TITEM>): DetailedResult<TITEM, CollectorResultDetail>;
107
106
  /**
108
- * {@inheritdoc ICollector.(getOrAdd:1)}
107
+ * {@inheritdoc Collections.ResultMap.has}
109
108
  */
110
- getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>;
109
+ has(key: CollectibleKey<TITEM>): boolean;
111
110
  /**
112
- * {@inheritdoc ICollector.(getOrAdd:2)}
111
+ * {@inheritdoc Collections.ResultMap.keys}
113
112
  */
114
- getOrAdd(key: TKEY, cb: CollectibleFactoryCallback<TKEY, TINDEX, TITEM>): DetailedResult<TITEM, ResultMapResultDetail>;
113
+ keys(): IterableIterator<CollectibleKey<TITEM>>;
115
114
  /**
116
- * {@inheritdoc IReadOnlyResultMap.has}
115
+ * {@inheritdoc Collections.ResultMap.values}
117
116
  */
118
- has(key: TKEY): boolean;
117
+ values(): IterableIterator<TITEM>;
119
118
  /**
120
- * {@inheritdoc ICollector.toReadOnly}
119
+ * Gets a read-only version of this collector.
121
120
  */
122
- toReadOnly(): IReadOnlyResultMap<TKEY, TITEM>;
121
+ toReadOnly(): IReadOnlyCollector<TITEM>;
123
122
  /**
124
123
  * Gets an iterator over the map entries.
125
124
  * @returns An iterator over the map entries.
126
125
  */
127
- [Symbol.iterator](): IterableIterator<KeyValueEntry<TKEY, TITEM>>;
128
- protected _isFactoryCB(item: TSRC | CollectibleFactoryCallback<TKEY, TINDEX, TITEM>): item is CollectibleFactoryCallback<TKEY, TINDEX, TITEM>;
126
+ [Symbol.iterator](): IterableIterator<KeyValueEntry<CollectibleKey<TITEM>, TITEM>>;
127
+ protected _isItem(keyOrItem: CollectibleKey<TITEM> | TITEM): keyOrItem is TITEM;
129
128
  }
130
129
  //# sourceMappingURL=collector.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"collector.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/collector.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAE,MAAM,EAAoD,MAAM,SAAS,CAAC;AACnG,OAAO,EAAE,YAAY,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAC7F,OAAO,EAAE,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{ value: true });
24
24
  exports.Collector = void 0;
25
25
  const base_1 = require("../base");
26
26
  /**
27
- * A {@link ICollector | Collector} that collects {@link Collections.ICollectible | ICollectible} items. Items in a collector are created by key and are assigned an index at the
28
- * time of addition. Items are immutable once added. Keys and indexes might be branded types, and source items might be transformed on addition.
27
+ * A {@link Collections.Collector | Collector} that is a specialized collection
28
+ * which contains items of type {@link Collections.ICollectible | ICollectible},
29
+ * which have a unique key and a write-once index.
30
+ *
31
+ * Items are assigned an index sequentially as they are added to the collection.
32
+ * Once added, items are immutable - they cannot be removed or replaced.
29
33
  * @public
30
34
  */
31
35
  class Collector {
32
36
  /**
33
- * {@inheritdoc IReadOnlyResultMap.inner}
37
+ * {@inheritdoc Collections.ResultMap.size}
34
38
  */
35
- get inner() {
36
- return this._byKey;
39
+ get size() {
40
+ return this._byIndex.length;
37
41
  }
38
42
  /**
39
- * Constructor for derived classes.
40
- * @param params - Parameters for constructing the collector.
43
+ * Constructs a new {@link Collections.Collector | Collector}.
44
+ * @param params - Optional {@link Collections.ICollectorConstructorParams | initialization parameters} used
45
+ * to construct the collector.
41
46
  */
42
47
  constructor(params) {
43
48
  var _a;
44
49
  this._byKey = new Map();
45
50
  this._byIndex = [];
46
- this._factory = params.factory;
47
- (_a = params.entries) === null || _a === void 0 ? void 0 : _a.forEach((entry) => {
48
- this.getOrAdd(entry[0], entry[1]);
49
- });
51
+ for (const item of (_a = params === null || params === void 0 ? void 0 : params.items) !== null && _a !== void 0 ? _a : []) {
52
+ this.add(item).orThrow();
53
+ }
50
54
  }
51
55
  /**
52
- * {@inheritdoc IReadOnlyResultMap.size}
56
+ * Creates a new {@link Collections.Collector | Collector} instance.
57
+ * @param params - Optional {@link Collections.ICollectorConstructorParams | initialization parameters} used
58
+ * to create the collector.
59
+ * @returns Returns {@link Success | Success} with the new collector if it was created successfully,
60
+ * or {@link Failure | Failure} with an error if the collector could not be created.
53
61
  */
54
- get size() {
55
- return this._byKey.size;
62
+ static createCollector(params) {
63
+ return (0, base_1.captureResult)(() => new Collector(params));
56
64
  }
57
65
  /**
58
- * {@inheritdoc IReadOnlyResultMap.keys}
66
+ * Adds an item to the collection, failing if a different item with the same key already exists. Note
67
+ * that adding an object that is already in the collection again will succeed without updating the collection.
68
+ * @param item - The item to add.
69
+ * @returns Returns {@link DetailedSuccess | Success} with the item and detail `added` if it was added
70
+ * or detail `exists` if the item was already in the map. Returns {@link DetailedFailure | Failure} with
71
+ * an error message and appropriate detail if the item could not be added.
59
72
  */
60
- keys() {
61
- return this._byKey.keys();
62
- }
63
- /**
64
- * {@inheritdoc IReadOnlyResultMap.values}
65
- */
66
- values() {
67
- return this._byKey.values();
73
+ add(item) {
74
+ const existing = this._byKey.get(item.key);
75
+ if (existing === item) {
76
+ return (0, base_1.succeedWithDetail)(item, 'exists');
77
+ }
78
+ else if (existing) {
79
+ return (0, base_1.failWithDetail)(`${item.key}: already exists`, 'exists');
80
+ }
81
+ const indexResult = item.setIndex(this._byIndex.length);
82
+ if (indexResult.isFailure()) {
83
+ return (0, base_1.failWithDetail)(`${item.key}: ${indexResult.message}`, 'invalid-index');
84
+ }
85
+ else if (item.index !== this._byIndex.length) {
86
+ return (0, base_1.failWithDetail)(`${item.key}: index mismatch - built item has ${item.index}`, 'invalid-index');
87
+ }
88
+ this._byKey.set(item.key, item);
89
+ this._byIndex.push(item);
90
+ return (0, base_1.succeedWithDetail)(item, 'added');
68
91
  }
69
92
  /**
70
- * {@inheritdoc IReadOnlyResultMap.entries}
93
+ * {@inheritdoc Collections.ResultMap.entries}
71
94
  */
72
95
  entries() {
73
96
  return this._byKey.entries();
74
97
  }
75
98
  /**
76
- * {@inheritdoc IReadOnlyResultMap.forEach}
99
+ * {@inheritdoc Collections.ResultMap.forEach}
77
100
  */
78
101
  forEach(callback, arg) {
79
102
  for (const [key, value] of this._byKey.entries()) {
@@ -81,17 +104,14 @@ class Collector {
81
104
  }
82
105
  }
83
106
  /**
84
- * {@inheritdoc IReadOnlyResultMap.get}
107
+ * {@inheritdoc Collections.ResultMap.get}
85
108
  */
86
109
  get(key) {
87
110
  const item = this._byKey.get(key);
88
- if (item === undefined) {
89
- return (0, base_1.failWithDetail)(`${key}: not found.`, 'not-found');
90
- }
91
- return (0, base_1.succeedWithDetail)(item, 'success');
111
+ return item ? (0, base_1.succeedWithDetail)(item, 'exists') : (0, base_1.failWithDetail)(`${key}: not found`, 'not-found');
92
112
  }
93
113
  /**
94
- * {@inheritdoc ICollector.getAt}
114
+ * {@inheritdoc Collections.IReadOnlyCollector.getAt}
95
115
  */
96
116
  getAt(index) {
97
117
  if (index < 0 || index >= this._byIndex.length) {
@@ -99,35 +119,46 @@ class Collector {
99
119
  }
100
120
  return (0, base_1.succeed)(this._byIndex[index]);
101
121
  }
102
- getOrAdd(key, itemOrCb) {
103
- if (this._byKey.has(key)) {
104
- return (0, base_1.succeedWithDetail)(this._byKey.get(key), 'exists');
122
+ getOrAdd(keyOrItem, factory) {
123
+ const key = !this._isItem(keyOrItem) ? keyOrItem : keyOrItem.key;
124
+ const existing = this._byKey.get(key);
125
+ if (existing) {
126
+ return (0, base_1.succeedWithDetail)(existing, 'exists');
105
127
  }
106
- const build = this._isFactoryCB(itemOrCb)
107
- ? itemOrCb(key, this._byIndex.length)
108
- : this._factory(key, this._byIndex.length, itemOrCb);
109
- if (build.isFailure()) {
110
- return (0, base_1.failWithDetail)(build.message, 'invalid-value');
128
+ const itemResult = !this._isItem(keyOrItem)
129
+ ? factory(keyOrItem, this._byIndex.length)
130
+ : (0, base_1.succeed)(keyOrItem);
131
+ if (itemResult.isFailure()) {
132
+ return (0, base_1.failWithDetail)(`${key}: ${itemResult.message}`, 'invalid-value');
111
133
  }
112
- const newItem = build.value;
113
- if (newItem.key !== key) {
114
- return (0, base_1.failWithDetail)(`$[key}: key mismatch in created item - ${newItem.key} !== ${key}`, 'invalid-key');
134
+ const item = itemResult.value;
135
+ if (item.key !== key) {
136
+ return (0, base_1.failWithDetail)(`${key}: key mismatch - built item has ${key}`, 'invalid-key');
115
137
  }
116
- if (newItem.index !== this._byIndex.length) {
117
- return (0, base_1.failWithDetail)(`$[key}: index mismatch in created item - ${newItem.index} !== ${this._byIndex.length}`, 'invalid-value');
118
- }
119
- this._byKey.set(key, newItem);
120
- this._byIndex.push(newItem);
121
- return (0, base_1.succeedWithDetail)(newItem, 'added');
138
+ return itemResult.withFailureDetail('invalid-index').onSuccess((item) => {
139
+ return this.add(item);
140
+ });
122
141
  }
123
142
  /**
124
- * {@inheritdoc IReadOnlyResultMap.has}
143
+ * {@inheritdoc Collections.ResultMap.has}
125
144
  */
126
145
  has(key) {
127
146
  return this._byKey.has(key);
128
147
  }
129
148
  /**
130
- * {@inheritdoc ICollector.toReadOnly}
149
+ * {@inheritdoc Collections.ResultMap.keys}
150
+ */
151
+ keys() {
152
+ return this._byKey.keys();
153
+ }
154
+ /**
155
+ * {@inheritdoc Collections.ResultMap.values}
156
+ */
157
+ values() {
158
+ return this._byKey.values();
159
+ }
160
+ /**
161
+ * Gets a read-only version of this collector.
131
162
  */
132
163
  toReadOnly() {
133
164
  return this;
@@ -139,8 +170,8 @@ class Collector {
139
170
  [Symbol.iterator]() {
140
171
  return this._byKey[Symbol.iterator]();
141
172
  }
142
- _isFactoryCB(item) {
143
- return typeof item === 'function';
173
+ _isItem(keyOrItem) {
174
+ return typeof keyOrItem !== 'string';
144
175
  }
145
176
  }
146
177
  exports.Collector = Collector;
@@ -1 +1 @@
1
- {"version":3,"file":"collector.js","sourceRoot":"","sources":["../../../src/packlets/collections/collector.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAmG;AA2FnG;;;;GAIG;AACH,MAAa,SAAS;IAWpB;;OAEG;IACH,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,YAAsB,MAA8D;;QAClF,IAAI,CAAC,MAAM,GAAG,IAAI,GAAG,EAAe,CAAC;QACrC,IAAI,CAAC,QAAQ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AAQ,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EACvF,eAAe,CAChB,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAC9B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC5B,OAAO,IAAA,wBAAiB,EAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC7C,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAS;QAClB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACI,CAAC,MAAM,CAAC,QAAQ,CAAC;QACtB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;IACxC,CAAC;IAES,YAAY,CACpB,IAA4D;QAE5D,OAAO,OAAO,IAAI,KAAK,UAAU,CAAC;IACpC,CAAC;CACF;AA/JD,8BA+JC","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 { DetailedResult, Result, fail, failWithDetail, succeed, succeedWithDetail } from '../base';\nimport { ICollectible, CollectibleFactoryCallback, CollectibleFactory } from './collectible';\nimport { KeyValueEntry } from './common';\nimport { IReadOnlyResultMap, ResultMapForEachCb, ResultMapResultDetail } from './readonlyResultMap';\n\n/**\n * Collects {@link Collections.ICollectible | ICollectible} items. Items in a collector are created by key and are assigned an index at the\n * time of addition. Items are immutable once added.\n * @public\n */\nexport interface ICollector<\n TKEY extends string = string,\n TINDEX extends number = number,\n TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>,\n TSRC = TITEM\n> extends IReadOnlyResultMap<TKEY, TITEM> {\n /**\n * Gets the item at a specified index.\n * @param index - The index of the item to retrieve.\n * @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.\n */\n getAt(index: TINDEX): Result<TITEM>;\n\n /**\n * 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.\n * @param key - The key of the item to retrieve.\n * @param item - The source representation of the item to be added if it does not exist.\n * @returns Returns {@link Success | Success} with the item if it exists or could be created, or {@link Failure | Failure} with an error if the\n * item cannot be created and indexed.\n */\n getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>;\n\n /**\n * 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.\n * @param key - The key of the item to retrieve.\n * @param callback - The factory callback to create the item if it does not exist.\n * @returns Returns {@link Success | Success} with the item if it exists, or {@link Failure | Failure} with an error if the item is not found.\n */\n getOrAdd(\n key: TKEY,\n callback: CollectibleFactoryCallback<TKEY, TINDEX>\n ): DetailedResult<TITEM, ResultMapResultDetail>;\n\n /**\n * Gets a {@link IReadOnlyResultMap | read-only map} which can access the items in the collector.\n */\n toReadOnly(): IReadOnlyResultMap<TKEY, TITEM>;\n}\n\n/**\n * A simple {@link ICollector | ICollector} with non-branded `string` key and `number` index, and no transformation of source items.\n * @public\n */\nexport type ISimpleCollector<TITEM extends ICollectible<string, number>> = ICollector<\n string,\n number,\n TITEM,\n TITEM\n>;\n\n/**\n * A {@link ICollector | Collector} with non-branded `string` key and `number` index, and transformation of source items.\n * @public\n */\nexport type IConvertingCollector<TITEM extends ICollectible<string, number>, TSRC> = ICollector<\n string,\n number,\n TITEM,\n TSRC\n>;\n\n/**\n * Parameters for constructing a {@link ICollector | Collector}.\n * @public\n */\nexport interface ICollectorConstructorParams<\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 * The default {@link Collections.CollectibleFactory | factory} to create items.\n */\n factory: CollectibleFactory<TKEY, TINDEX, TITEM, TSRC>;\n /**\n * An optional array of entries to add to the collector.\n */\n entries?: KeyValueEntry<TKEY, TSRC>[];\n}\n\n/**\n * A {@link ICollector | Collector} that collects {@link Collections.ICollectible | ICollectible} items. Items in a collector are created by key and are assigned an index at the\n * time of addition. Items are immutable once added. Keys and indexes might be branded types, and source items might be transformed on addition.\n * @public\n */\nexport class Collector<\n TKEY extends string = string,\n TINDEX extends number = number,\n TITEM extends ICollectible<TKEY, TINDEX> = ICollectible<TKEY, TINDEX>,\n TSRC = TITEM\n> implements ICollector<TKEY, TINDEX, TITEM, TSRC>\n{\n private _byKey: Map<TKEY, TITEM>;\n private _byIndex: TITEM[];\n private _factory: CollectibleFactory<TKEY, TINDEX, TITEM, TSRC>;\n\n /**\n * {@inheritdoc IReadOnlyResultMap.inner}\n */\n public get inner(): ReadonlyMap<TKEY, TITEM> {\n return this._byKey;\n }\n\n /**\n * Constructor for derived classes.\n * @param params - Parameters for constructing the collector.\n */\n protected constructor(params: ICollectorConstructorParams<TKEY, TINDEX, TITEM, TSRC>) {\n this._byKey = new Map<TKEY, TITEM>();\n this._byIndex = [];\n this._factory = params.factory;\n params.entries?.forEach((entry) => {\n this.getOrAdd(entry[0], entry[1]);\n });\n }\n\n /**\n * {@inheritdoc IReadOnlyResultMap.size}\n */\n public get size(): number {\n return this._byKey.size;\n }\n\n /**\n * {@inheritdoc IReadOnlyResultMap.keys}\n */\n public keys(): IterableIterator<TKEY> {\n return this._byKey.keys();\n }\n\n /**\n * {@inheritdoc IReadOnlyResultMap.values}\n */\n public values(): IterableIterator<TITEM> {\n return this._byKey.values();\n }\n\n /**\n * {@inheritdoc IReadOnlyResultMap.entries}\n */\n public entries(): IterableIterator<[TKEY, TITEM]> {\n return this._byKey.entries();\n }\n\n /**\n * {@inheritdoc IReadOnlyResultMap.forEach}\n */\n public forEach(callback: ResultMapForEachCb<TKEY, TITEM>, arg?: unknown): void {\n for (const [key, value] of this._byKey.entries()) {\n callback(value, key, this, arg);\n }\n }\n\n /**\n * {@inheritdoc IReadOnlyResultMap.get}\n */\n public get(key: TKEY): DetailedResult<TITEM, ResultMapResultDetail> {\n const item = this._byKey.get(key);\n if (item === undefined) {\n return failWithDetail<TITEM, ResultMapResultDetail>(`${key}: not found.`, 'not-found');\n }\n return succeedWithDetail(item, 'success');\n }\n\n /**\n * {@inheritdoc ICollector.getAt}\n */\n public getAt(index: TINDEX): Result<TITEM> {\n if (index < 0 || index >= this._byIndex.length) {\n return fail(`${index}: out of range.`);\n }\n return succeed(this._byIndex[index]);\n }\n\n /**\n * {@inheritdoc ICollector.(getOrAdd:1)}\n */\n public getOrAdd(key: TKEY, item: TSRC): DetailedResult<TITEM, ResultMapResultDetail>;\n /**\n * {@inheritdoc ICollector.(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._byKey.has(key)) {\n return succeedWithDetail(this._byKey.get(key)!, 'exists');\n }\n const build = this._isFactoryCB(itemOrCb)\n ? itemOrCb(key, this._byIndex.length)\n : this._factory(key, this._byIndex.length, itemOrCb);\n if (build.isFailure()) {\n return failWithDetail<TITEM, ResultMapResultDetail>(build.message, 'invalid-value');\n }\n const newItem = build.value;\n\n if (newItem.key !== key) {\n return failWithDetail(\n `$[key}: key mismatch in created item - ${newItem.key} !== ${key}`,\n 'invalid-key'\n );\n }\n if (newItem.index !== this._byIndex.length) {\n return failWithDetail(\n `$[key}: index mismatch in created item - ${newItem.index} !== ${this._byIndex.length}`,\n 'invalid-value'\n );\n }\n this._byKey.set(key, newItem);\n this._byIndex.push(newItem);\n return succeedWithDetail(newItem, 'added');\n }\n\n /**\n * {@inheritdoc IReadOnlyResultMap.has}\n */\n public has(key: TKEY): boolean {\n return this._byKey.has(key);\n }\n\n /**\n * {@inheritdoc ICollector.toReadOnly}\n */\n public toReadOnly(): IReadOnlyResultMap<TKEY, TITEM> {\n return this;\n }\n\n /**\n * Gets an iterator over the map entries.\n * @returns An iterator over the map entries.\n */\n public [Symbol.iterator](): IterableIterator<KeyValueEntry<TKEY, TITEM>> {\n return this._byKey[Symbol.iterator]();\n }\n\n protected _isFactoryCB(\n item: TSRC | CollectibleFactoryCallback<TKEY, TINDEX, TITEM>\n ): item is CollectibleFactoryCallback<TKEY, TINDEX, TITEM> {\n return typeof item === 'function';\n }\n}\n"]}
1
+ {"version":3,"file":"collector.js","sourceRoot":"","sources":["../../../src/packlets/collections/collector.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAQiB;AAuCjB;;;;;;;;GAQG;AACH,MAAa,SAAS;IAQpB;;OAEG;IACH,IAAW,IAAI;QACb,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IAC9B,CA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B,CAAC;IAED;;OAEG;IACI,IAAI;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC5B,CAAC;IAED;;OAEG;IACI,MAAM;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAC9B,CAAC;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACI,CAAC,MAAM,CAAC,QAAQ,CAAC;QACtB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;IACxC,CAAC;IAES,OAAO,CAAC,SAAwC;QACxD,OAAO,OAAO,SAAS,KAAK,QAAQ,CAAC;IACvC,CAAC;CACF;AAlMD,8BAkMC","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 {\n captureResult,\n DetailedResult,\n failWithDetail,\n Result,\n fail,\n succeed,\n succeedWithDetail\n} from '../base';\nimport { CollectibleFactoryCallback, CollectibleKey, ICollectible } from './collectible';\nimport { KeyValueEntry } from './common';\nimport { IReadOnlyResultMap, ResultMapForEachCb, ResultMapResultDetail } from './readonlyResultMap';\n\n/**\n * Additional success or failure details for mutating collector calls.\n * @public\n */\nexport type CollectorResultDetail = ResultMapResultDetail | 'invalid-index';\n\n/**\n * A read-only interface exposing only the non-mutating methods of a {@link Collections.Collector | ICollector}.\n * @public\n */\nexport interface IReadOnlyCollector<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n TITEM extends ICollectible<any, any>\n> extends IReadOnlyResultMap<CollectibleKey<TITEM>, TITEM> {\n /**\n * Gets the item at a specified index.\n * @param index - The index of the item to retrieve.\n * @returns Returns {@link Success | Success} with the item if it exists, or {@link Failure | Failure}\n * with an error if the index is out of range.\n */\n getAt(index: number): Result<TITEM>;\n}\n\n/**\n * Parameters for constructing a {@link Collections.Collector | ICollector}.\n * @public\n */\nexport interface ICollectorConstructorParams<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n TITEM extends ICollectible<any, any>\n> {\n items?: TITEM[];\n}\n\n/**\n * A {@link Collections.Collector | Collector} that is a specialized collection\n * which contains items of type {@link Collections.ICollectible | ICollectible},\n * which have a unique key and a write-once index.\n *\n * Items are assigned an index sequentially as they are added to the collection.\n * Once added, items are immutable - they cannot be removed or replaced.\n * @public\n */\nexport class Collector<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n TITEM extends ICollectible<any, any>\n> implements IReadOnlyCollector<TITEM>\n{\n private readonly _byKey: Map<CollectibleKey<TITEM>, TITEM>;\n private readonly _byIndex: TITEM[];\n\n /**\n * {@inheritdoc Collections.ResultMap.size}\n */\n public get size(): number {\n return this._byIndex.length;\n }\n\n /**\n * Constructs a new {@link Collections.Collector | Collector}.\n * @param params - Optional {@link Collections.ICollectorConstructorParams | initialization parameters} used\n * to construct the collector.\n */\n public constructor(params?: ICollectorConstructorParams<TITEM>) {\n this._byKey = new Map<CollectibleKey<TITEM>, TITEM>();\n this._byIndex = [];\n for (const item of params?.items ?? []) {\n this.add(item).orThrow();\n }\n }\n\n /**\n * Creates a new {@link Collections.Collector | Collector} instance.\n * @param params - Optional {@link Collections.ICollectorConstructorParams | initialization parameters} used\n * to create the collector.\n * @returns Returns {@link Success | Success} with the new collector if it was created successfully,\n * or {@link Failure | Failure} with an error if the collector could not be created.\n */\n public static createCollector<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n TITEM extends ICollectible<any, any>\n >(params?: ICollectorConstructorParams<TITEM>): Result<Collector<TITEM>> {\n return captureResult(() => new Collector(params));\n }\n\n /**\n * Adds an item to the collection, failing if a different item with the same key already exists. Note\n * that adding an object that is already in the collection again will succeed without updating the collection.\n * @param item - The item to add.\n * @returns Returns {@link DetailedSuccess | Success} with the item and detail `added` if it was added\n * or detail `exists` if the item was already in the map. Returns {@link DetailedFailure | Failure} with\n * an error message and appropriate detail if the item could not be added.\n */\n public add(item: TITEM): DetailedResult<TITEM, CollectorResultDetail> {\n const existing = this._byKey.get(item.key);\n if (existing === item) {\n return succeedWithDetail(item, 'exists');\n } else if (existing) {\n return failWithDetail(`${item.key}: already exists`, 'exists');\n }\n const indexResult = item.setIndex(this._byIndex.length);\n if (indexResult.isFailure()) {\n return failWithDetail(`${item.key}: ${indexResult.message}`, 'invalid-index');\n } else if (item.index !== this._byIndex.length) {\n return failWithDetail(`${item.key}: index mismatch - built item has ${item.index}`, 'invalid-index');\n }\n this._byKey.set(item.key, item);\n this._byIndex.push(item);\n return succeedWithDetail(item, 'added');\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.entries}\n */\n public entries(): MapIterator<KeyValueEntry<CollectibleKey<TITEM>, TITEM>> {\n return this._byKey.entries();\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.forEach}\n */\n public forEach(callback: ResultMapForEachCb<CollectibleKey<TITEM>, TITEM>, arg?: unknown): void {\n for (const [key, value] of this._byKey.entries()) {\n callback(value, key, this, arg);\n }\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.get}\n */\n public get(key: CollectibleKey<TITEM>): DetailedResult<TITEM, ResultMapResultDetail> {\n const item = this._byKey.get(key);\n return item ? succeedWithDetail(item, 'exists') : failWithDetail(`${key}: not found`, 'not-found');\n }\n\n /**\n * {@inheritdoc Collections.IReadOnlyCollector.getAt}\n */\n public getAt(index: number): Result<TITEM> {\n if (index < 0 || index >= this._byIndex.length) {\n return fail(`${index}: out of range.`);\n }\n return succeed(this._byIndex[index]);\n }\n\n /**\n * Gets an existing item with a key matching that of a supplied item, or adds the supplied\n * item to the collector if no item with that key exists.\n * @param item - The item to get or add.\n * @returns Returns {@link DetailedSuccess | Success} with the item stored in the collector -\n * detail `exists` indicates that an existing item return and detail `added` indicates that the\n * item was added. Returns {@link DetailedFailure | Failure} with an error and appropriate\n * detail if the item could not be added.\n */\n public getOrAdd(item: TITEM): DetailedResult<TITEM, CollectorResultDetail>;\n\n /**\n * Gets an existing item with a key matching the supplied key, or adds a new item to the collector\n * using a factory callback if no item with that key exists.\n * @param key - The key of the item to add.\n * @param callback - The factory callback to create the item.\n * @returns Returns {@link DetailedSuccess | Success} with the item stored in the collector -\n * detail `exists` indicates that an existing item return and detail `added` indicates that the\n * item was added. Returns {@link DetailedFailure | Failure} with an error and appropriate\n * detail if the item could not be added.\n */\n public getOrAdd(\n key: CollectibleKey<TITEM>,\n factory: CollectibleFactoryCallback<TITEM>\n ): DetailedResult<TITEM, CollectorResultDetail>;\n\n public getOrAdd(\n keyOrItem: CollectibleKey<TITEM> | TITEM,\n factory?: CollectibleFactoryCallback<TITEM>\n ): DetailedResult<TITEM, CollectorResultDetail> {\n const key = !this._isItem(keyOrItem) ? keyOrItem : keyOrItem.key;\n const existing = this._byKey.get(key);\n if (existing) {\n return succeedWithDetail(existing, 'exists');\n }\n\n const itemResult = !this._isItem(keyOrItem)\n ? factory!(keyOrItem, this._byIndex.length)\n : succeed(keyOrItem);\n if (itemResult.isFailure()) {\n return failWithDetail(`${key}: ${itemResult.message}`, 'invalid-value');\n }\n const item = itemResult.value;\n\n if (item.key !== key) {\n return failWithDetail(`${key}: key mismatch - built item has ${key}`, 'invalid-key');\n }\n\n return itemResult.withFailureDetail<CollectorResultDetail>('invalid-index').onSuccess((item) => {\n return this.add(item);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.has}\n */\n public has(key: CollectibleKey<TITEM>): boolean {\n return this._byKey.has(key);\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.keys}\n */\n public keys(): IterableIterator<CollectibleKey<TITEM>> {\n return this._byKey.keys();\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.values}\n */\n public values(): IterableIterator<TITEM> {\n return this._byKey.values();\n }\n\n /**\n * Gets a read-only version of this collector.\n */\n public toReadOnly(): IReadOnlyCollector<TITEM> {\n return this;\n }\n\n /**\n * Gets an iterator over the map entries.\n * @returns An iterator over the map entries.\n */\n public [Symbol.iterator](): IterableIterator<KeyValueEntry<CollectibleKey<TITEM>, TITEM>> {\n return this._byKey[Symbol.iterator]();\n }\n\n protected _isItem(keyOrItem: CollectibleKey<TITEM> | TITEM): keyOrItem is TITEM {\n return typeof keyOrItem !== 'string';\n }\n}\n"]}
@@ -0,0 +1,89 @@
1
+ import { DetailedResult } from '../base';
2
+ import { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';
3
+ import { ResultMapValueFactory } from './resultMap';
4
+ import { KeyValueConverters } from './keyValueConverters';
5
+ import { Collector, CollectorResultDetail } from './collector';
6
+ import { IReadOnlyResultMapValidator } from './resultMapValidator';
7
+ import { CollectibleKey, ICollectible } from './collectible';
8
+ /**
9
+ * A read-only interface exposing non-mutating methods of a
10
+ * {@link Collections.CollectorValidator | CollectorValidator}.
11
+ * @public
12
+ */
13
+ export interface IReadOnlyCollectorValidator<TITEM extends ICollectible<any, any>> extends IReadOnlyResultMapValidator<CollectibleKey<TITEM>, TITEM> {
14
+ /**
15
+ * {@inheritdoc Collections.ConvertingCollectorValidator.map}
16
+ */
17
+ readonly map: IReadOnlyResultMap<CollectibleKey<TITEM>, TITEM>;
18
+ /**
19
+ * {@inheritdoc Collections.Collector.get}
20
+ */
21
+ get(key: string): DetailedResult<TITEM, ResultMapResultDetail>;
22
+ /**
23
+ * {@inheritdoc Collections.ResultMap.has}
24
+ */
25
+ has(key: string): boolean;
26
+ /**
27
+ * {@inheritdoc Collections.Collector.(getOrAdd:2)}
28
+ */
29
+ getOrAdd(key: string, factory: ResultMapValueFactory<CollectibleKey<TITEM>, TITEM>): DetailedResult<TITEM, CollectorResultDetail>;
30
+ }
31
+ /**
32
+ * Parameters for constructing a {@link Collections.CollectorValidator | CollectorValidator}.
33
+ * @public
34
+ */
35
+ export interface ICollectorValidatorCreateParams<TITEM extends ICollectible<any, any>> {
36
+ readonly collector: Collector<TITEM>;
37
+ readonly converters: KeyValueConverters<CollectibleKey<TITEM>, TITEM>;
38
+ }
39
+ /**
40
+ * A {@link Collections.Collector | Collector} wrapper which validates weakly-typed keys
41
+ * and values before calling the wrapped collector.
42
+ * @public
43
+ */
44
+ export declare class CollectorValidator<TITEM extends ICollectible<any, any>> implements IReadOnlyCollectorValidator<TITEM> {
45
+ readonly converters: KeyValueConverters<CollectibleKey<TITEM>, TITEM>;
46
+ get map(): IReadOnlyResultMap<CollectibleKey<TITEM>, TITEM>;
47
+ protected _collector: Collector<TITEM>;
48
+ /**
49
+ * Constructs a new {@link Collections.ConvertingCollectorValidator | ConvertingCollectorValidator}.
50
+ * @param params - Required parameters for constructing the collector validator.
51
+ */
52
+ constructor(params: ICollectorValidatorCreateParams<TITEM>);
53
+ /**
54
+ * {@inheritdoc Collections.Collector.add}
55
+ */
56
+ add(item: unknown): DetailedResult<TITEM, CollectorResultDetail>;
57
+ /**
58
+ * {@inheritdoc Collections.Collector.get}
59
+ */
60
+ get(key: string): DetailedResult<TITEM, ResultMapResultDetail>;
61
+ /**
62
+ * {@inheritdoc Collections.Collector.(getOrAdd:2)}
63
+ */
64
+ getOrAdd(key: string, factory: ResultMapValueFactory<CollectibleKey<TITEM>, TITEM>): DetailedResult<TITEM, CollectorResultDetail>;
65
+ /**
66
+ * {@inheritdoc Collections.Collector.(getOrAdd:1)}
67
+ * @param item - The item to add to the collector.
68
+ */
69
+ getOrAdd(item: unknown): DetailedResult<TITEM, CollectorResultDetail>;
70
+ /**
71
+ * {@inheritdoc Collections.ResultMap.has}
72
+ */
73
+ has(key: string): boolean;
74
+ /**
75
+ * {@inheritdoc Collections.Collector.toReadOnly}
76
+ */
77
+ toReadOnly(): IReadOnlyCollectorValidator<TITEM>;
78
+ /**
79
+ * Helper to convert a value, returning a {@link DetailedResult | DetailedResult}
80
+ * and formatting the error message.
81
+ * @param value - The value to convert.
82
+ * @returns {@link DetailedSuccess | DetailedSuccess} with the converted value
83
+ * and detail `success` if conversion is successful, or
84
+ * {@link DetailedFailure | DetailedFailure} with the error message and detail `invalid-value`
85
+ * if conversion fails.
86
+ */
87
+ protected _convertValue(value: unknown): DetailedResult<TITEM, CollectorResultDetail>;
88
+ }
89
+ //# sourceMappingURL=collectorValidator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collectorValidator.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/collectorValidator.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAChF,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7D;;;;GAIG;AACH,MAAM,WAAW,2BAA2B,CAE1C,KAAK,SAAS,YAA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