@typedly/collection 4.0.0 → 6.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.
package/README.md CHANGED
@@ -1,16 +1,26 @@
1
1
  <a href="https://github.com/typescript-package">
2
2
  <img
3
3
  src="https://avatars.githubusercontent.com/u/189665258?s=150&u=712e292bae048947d1f7d2020d7d38875c40e63a&v=4"
4
- title="@typedly/collection - A TypeScript type definitions package for data collection."
4
+ title="@typedly/collection - A TypeScript type definitions package for data collections with customizable storage."
5
5
  />
6
6
  </a>
7
7
 
8
8
  ## @typedly/collection
9
9
 
10
10
  <!-- npm badge -->
11
- [![npm version][typedly-npm-badge-svg]][typedly-npm-badge]
12
- [![GitHub issues][typedly-badge-issues]][typedly-issues]
13
- [![GitHub license][typedly-badge-license]][typedly-license]
11
+ [![npm version][package-npm-badge-svg]][package-npm-badge]
12
+ [![GitHub issues][package-badge-issues]][package-issues]
13
+ [![GitHub license][package-badge-license]][package-license]
14
+
15
+ <!-- GitHub badges -->
16
+ [![GitHub issues][package-badge-issues]][package-issues]
17
+ [![GitHub forks][package-badge-forks]][package-forks]
18
+ [![GitHub stars][package-badge-stars]][package-stars]
19
+ [![GitHub license][package-badge-license]][package-license]
20
+
21
+ <!-- Sponsors -->
22
+ [![GitHub Sponsors][github-badge-sponsor]][github-sponsor-link]
23
+ [![Patreon Sponsors][patreon-badge]][patreon-link]
14
24
 
15
25
  A **TypeScript** type definitions package for data collections with customizable storage.
16
26
 
@@ -25,14 +35,20 @@ A **TypeScript** type definitions package for data collections with customizable
25
35
 
26
36
  ## Table of contents
27
37
 
38
+ - [Related packages](#related-packages)
28
39
  - [Installation](#installation)
29
40
  - [Api](#api)
30
- - Interface
31
- - [`CollectionAdapter`](#collectionadapter)
41
+ - [Adapter](#adapter)
32
42
  - [`CollectionAdapterConstructor`](#collectionadapterconstructor)
33
- - [`CollectionShape`](#collectionshape)
34
- - Type
43
+ - [`CollectionAdapter`](#collectionadapter)
44
+ - [Inference](#inference)
45
+ - [`InferCollectionType`](#infercollectiontype)
46
+ - [`InferElementFromSettings`](#inferelementfromsettings)
47
+ - [`InferElement`](#inferelement)
48
+ - [Main](#main)
35
49
  - [`CollectionConstructor`](#collectionconstructor)
50
+ - [`CollectionSettings`](#collectionsettings)
51
+ - [`CollectionShape`](#collectionshape)
36
52
  - [Contributing](#contributing)
37
53
  - [Support](#support)
38
54
  - [Code of Conduct](#code-of-conduct)
@@ -40,6 +56,25 @@ A **TypeScript** type definitions package for data collections with customizable
40
56
  - [Commit](#commit)
41
57
  - [Versioning](#versioning)
42
58
  - [License](#license)
59
+ - [Packages](#packages)
60
+
61
+ ## Related packages
62
+
63
+ ### Peer dependencies
64
+
65
+ - **[@typedly/adaptable-data](https://github.com/typedly/adaptable-data)**: A **TypeScript** type definitions package for configurable, composable, trait-driven adaptable data models with adapter support.
66
+ - **[@typedly/configurable-data](https://github.com/typedly/configurable-data)**: A **TypeScript** type definitions package for configurable, composable, trait-driven data models.
67
+ - **[@typedly/data-traits](https://github.com/typedly/data-traits)**: A **TypeScript** type definitions package for configurable data traits, providing various kinds of configurable data interfaces.
68
+ - **[@typedly/data](https://github.com/typedly/data)**: A **TypeScript** type definitions for [`@typescript-package/data`](https://github.com/typedly/data).
69
+
70
+ ### General
71
+
72
+ - **[@typedly/adaptable-data](https://github.com/typedly/adaptable-data)**: A **TypeScript** type definitions for data adapter.
73
+ - **[@typedly/adaptable-collection](https://github.com/typedly/adaptable-collection)**: A **TypeScript** type definitions package for adaptable collections with configuration and adapter support.
74
+ - **[@typedly/configurable-collection](https://github.com/typedly/configurable-collection)**: A **TypeScript** type definitions package for configurable collections with configuration.
75
+ - **[@typedly/collection](https://github.com/typedly/collection)**: A **TypeScript** type definitions package for data collections with customizable storage.
76
+ - **[@typescript-package/data](https://github.com/typescript-package/data)**: A lightweight **TypeScript** library for basic data management.
77
+ - **[@typescript-package/collection](https://github.com/typescript-package/collection)**: A lightweight **TypeScript** library for data collection.
43
78
 
44
79
  ## Installation
45
80
 
@@ -51,17 +86,28 @@ npm install @typedly/collection --save-peer
51
86
 
52
87
  ```typescript
53
88
  import {
54
- // Interface.
89
+ // Adapter.
55
90
  CollectionAdapter,
91
+
92
+ // Constructor.
56
93
  CollectionAdapterConstructor,
94
+ CollectionConstructor,
95
+
96
+ // Inference.
97
+ InferCollectionType,
98
+ InferElementFromSettings,
99
+ InferElement,
100
+
101
+ // Interface.
102
+ CollectionSettings,
57
103
  CollectionShape,
58
- // Type.
59
- CollectionConstructor
60
104
  } from '@typedly/collection';
61
105
  ```
62
106
 
63
107
  ### Interface
64
108
 
109
+ ### Adapter
110
+
65
111
  ### `CollectionAdapter`
66
112
 
67
113
  The adapter interface for collections.
@@ -75,223 +121,61 @@ import { CollectionAdapter } from '@typedly/collection';
75
121
  The interface of adapter constructor.
76
122
 
77
123
  ```typescript
78
- import { CollectionAdapter, CollectionAdapterConstructor } from '@typedly/collection';
79
- import { AsyncReturn } from '@typedly/data';
80
- /**
81
- * Example class with fake async returned types.
82
- */
83
- export class ExampleCollectionAdapter<
84
- E,
85
- T,
86
- R extends boolean = false,
87
- > implements CollectionAdapter<E, T, R> {
88
- public get async(): R {
89
- return this.#async;
90
- }
91
- public get size(): number {
92
- return this.#items.length;
93
- }
94
- public get value(): T {
95
- // Implementation depends on specific requirements.
96
- return {} as T;
97
- }
98
- version = "1.0.0";
99
- #async: R;
100
- #items: E[] = [];
101
- constructor({async}: {async: R}, ...elements: E[]) {
102
- this.#async = async;
103
- this.#items.push(...elements);
104
- }
105
- public add(...element: E[]): AsyncReturn<R, this> {
106
- this.#items.push(...element);
107
- return this as AsyncReturn<R, this>;
108
- }
109
- public clear(): AsyncReturn<R, this> {
110
- this.#items = [];
111
- return this as AsyncReturn<R, this>;
112
- }
113
- public delete(...element: E[]): AsyncReturn<R, boolean> {
114
- const index = this.#items.indexOf(element[0]);
115
- if (index !== -1) {
116
- this.#items.splice(index, 1);
117
- return true as AsyncReturn<R, boolean>;
118
- }
119
- return false as AsyncReturn<R, boolean>;
120
- }
121
- public destroy(): AsyncReturn<R, this> {
122
- this.#items = [];
123
- return this as AsyncReturn<R, this>;
124
- }
125
- public forEach(callbackfn: (element: E, element2: E, collection: CollectionAdapter<E, T, R>) => void, thisArg?: any): AsyncReturn<R, this> {
126
- this.#items.forEach((value: E) => {
127
- callbackfn.call(thisArg, value, value, this);
128
- });
129
- return this as AsyncReturn<R, this>;
130
- }
131
- public getValue(): AsyncReturn<R, T> {
132
- // Implementation depends on specific requirements.
133
- return {} as AsyncReturn<R, T>;
134
- }
135
- public has(element: E): AsyncReturn<R, boolean> {
136
- return this.#items.includes(element) as AsyncReturn<R, boolean>;
137
- }
138
- public lock(): this {
139
- // Implementation depends on specific requirements.
140
- return this;
141
- }
142
- public setValue(value: T): AsyncReturn<R, this> {
143
- // Implementation depends on specific requirements.
144
- return this as AsyncReturn<R, this>;
145
- }
146
- public unlock(): AsyncReturn<R, this> {
147
- // Implementation depends on specific requirements.
148
- return this as AsyncReturn<R, this>;
149
- }
150
- }
151
-
152
- // Create factory function for creating adapter instances.
153
- function createAdapter<
154
- E,
155
- T,
156
- R extends boolean = false,
157
- A extends CollectionAdapter<E, T, R> = CollectionAdapter<E, T, R>
158
- >(
159
- AdapterCtor: CollectionAdapterConstructor<E, T, R, { async: R }, A>,
160
- async: R,
161
- ...elements: E[]
162
- ): A {
163
- return new AdapterCtor({ async }, ...elements);
164
- }
165
-
166
- // ExampleCollectionAdapter<number, unknown, false>
167
- const adapter1 = createAdapter(ExampleCollectionAdapter, false, 1, 2, 3);
168
- // ExampleCollectionAdapter<string, unknown, true>
169
- const adapter2 = createAdapter(ExampleCollectionAdapter, true, 'a', 'b', 'c');
124
+ import { CollectionAdapterConstructor } from '@typedly/collection';
125
+ ```
126
+
127
+ ### Inference
128
+
129
+ ### `InferCollectionType`
130
+
131
+ Infer the collection type from the collection settings or adapter.
132
+
133
+ ```typescript
134
+ import { InferCollectionType } from '@typedly/collection';
135
+ ```
136
+
137
+ ### `InferElementFromSettings`
138
+
139
+ Type to infer the element type from collection settings or adapter.
170
140
 
141
+ ```typescript
142
+ import { InferElementFromSettings } from '@typedly/collection';
171
143
  ```
172
144
 
173
- ### `CollectionShape`
145
+ ### `InferElement`
174
146
 
175
- Represents a collection of elements.
147
+ Type to infer the element type from collection settings or adapter, with special handling for common collection types like Set, Array, and Map.
176
148
 
177
149
  ```typescript
178
- import { CollectionShape, IterValue } from '@typedly/collection';
179
-
180
- // Example class implementing CollectionShape.
181
- export class AnyCollection<
182
- E,
183
- T = Set<E>
184
- > implements CollectionShape<E, T, false> {
185
- async = false as false;
186
-
187
- // Data shape method.
188
- get value(): T {
189
- // Implementation depends on specific requirements.
190
- return this.#items;
191
- }
192
-
193
- // Example internal storage.
194
- #items: T;
195
-
196
- constructor(
197
- { async, value }: { async: false, value?: T },
198
- type?: new (...args: any[]) => T,
199
- ...elements: E[]
200
- ) {
201
- this.async = async;
202
- this.#items = type ? new type() : value ? value : {} as T;
203
- elements.forEach(element => (this.#items as any).add(element));
204
- }
205
-
206
- public clear(): this {
207
- // Implementation depends on specific requirements.
208
- return this;
209
- }
210
- public destroy(): this {
211
- // Implementation depends on specific requirements.
212
- return this;
213
- }
214
- public lock(): this {
215
- // Implementation depends on specific requirements.
216
- return this;
217
- }
218
- public getValue(): T {
219
- // Implementation depends on specific requirements.
220
- return this.#items;
221
- }
222
- public setValue(value: T): this {
223
- // Implementation depends on specific requirements.
224
- this.#items = value;
225
- return this;
226
- }
227
- public unlock(): this {
228
- // Implementation depends on specific requirements.
229
- return this;
230
- }
231
-
232
-
233
- add(element: E): this {
234
- (this.#items as any).add(element);
235
- return this;
236
- }
237
-
238
- delete(element: E): boolean {
239
- return (this.#items as any).delete(element);
240
- }
241
-
242
- forEach(callbackfn: (element: E, element2: E, collection: CollectionShape<E, T, false>) => void, thisArg?: any): this {
243
- (this.#items as any).forEach((value: E) => {
244
- callbackfn.call(thisArg, value, value, this);
245
- });
246
- return this;
247
- }
248
-
249
- has(element: E): boolean {
250
- return (this.#items as any).has(element);
251
- }
252
-
253
- get size(): number {
254
- return (this.#items as any).size;
255
- }
256
-
257
- get [Symbol.toStringTag](): string {
258
- return 'MyCollection';
259
- }
260
-
261
- [Symbol.iterator](): IterableIterator<IterValue<T>> {
262
- return (this.#items as any).values();
263
- }
264
- }
265
-
266
- const obj1 = {age: 27};
267
- const obj2 = {age: 37};
268
- const obj3 = {age: 47};
269
- const anyCollection1 = new AnyCollection<{age: number}, Set<{age: number}>>(
270
- { async: false, value: new Set([{age: 27}, {age: 37}, {age: 47}]) }
271
- )
272
- .add(obj1)
273
- .add(obj2)
274
- .add(obj3);
275
-
276
- console.log(`anyCollection1:`, anyCollection1.value);
277
-
278
- const anyCollection2 = new AnyCollection<{age: number}, Set<{age: number}>>(
279
- { async: false }, Set)
280
- .add(obj1)
281
- .add(obj2)
282
- .add(obj3);
283
-
284
- console.log(`anyCollection2:`, anyCollection2.value);
150
+ import { InferElement } from '@typedly/collection';
285
151
  ```
286
152
 
287
- ### Type
153
+ ### Main
288
154
 
289
155
  ### `CollectionConstructor`
290
156
 
157
+ The constructor type for `CollectionShape`.
158
+
291
159
  ```typescript
292
160
  import { CollectionConstructor } from '@typedly/collection';
293
161
  ```
294
162
 
163
+ ### `CollectionSettings`
164
+
165
+ Represents the settings for a collection.
166
+
167
+ ```typescript
168
+ import { CollectionSettings } from '@typedly/collection';
169
+ ```
170
+
171
+ ### `CollectionShape`
172
+
173
+ The `CollectionShape` interface defines the structure and behavior of a collection data structure, which can be implemented by various types of collections such as sets, arrays, or maps.
174
+
175
+ ```typescript
176
+ import { CollectionShape } from '@typedly/collection';
177
+ ```
178
+
295
179
  ## Contributing
296
180
 
297
181
  Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
@@ -302,20 +186,23 @@ If you find this package useful and would like to support its and general develo
302
186
 
303
187
  Support via:
304
188
 
305
- - [Stripe](https://donate.stripe.com/dR614hfDZcJE3wAcMM)
306
- - ~~[Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29)~~
307
- - [GitHub](https://github.com/sponsors/angular-package/sponsorships?sponsor=sciborrudnicki&tier_id=83618)
189
+ - [4Fund](https://4fund.com/bruubs)
308
190
  - [DonorBox](https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=o)
191
+ - [GitHub](https://github.com/sponsors/angular-package/sponsorships?sponsor=sciborrudnicki&tier_id=83618)
192
+ - [Ko-fi](https://ko-fi.com/sterblack)
193
+ - [OpenCollective](https://opencollective.com/sterblack)
309
194
  - [Patreon](https://www.patreon.com/checkout/angularpackage?rid=0&fan_landing=true&view_as=public)
310
- - [4Fund](https://4fund.com/bruubs)
195
+ - [PayPal](https://paypal.me/sterblack)
196
+ - [Stripe](https://donate.stripe.com/dR614hfDZcJE3wAcMM)
197
+ - ~~[Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29)~~
311
198
 
312
199
  or via Trust Wallet
313
200
 
314
- - [XLM](https://link.trustwallet.com/send?coin=148&address=GAFFFB7H3LG42O6JA63FJDRK4PP4JCNEOPHLGLLFH625X2KFYQ4UYVM4)
315
- - [USDT (BEP20)](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94&token_id=0x55d398326f99059fF775485246999027B3197955)
316
- - [ETH](https://link.trustwallet.com/send?coin=60&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94)
317
- - [BTC](https://link.trustwallet.com/send?coin=0&address=bc1qnf709336tfl57ta5mfkf4t9fndhx7agxvv9svn)
318
201
  - [BNB](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94)
202
+ - [BTC](https://link.trustwallet.com/send?coin=0&address=bc1qnf709336tfl57ta5mfkf4t9fndhx7agxvv9svn)
203
+ - [ETH](https://link.trustwallet.com/send?coin=60&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94)
204
+ - [USDT (BEP20)](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94&token_id=0x55d398326f99059fF775485246999027B3197955)
205
+ - [XLM](https://link.trustwallet.com/send?coin=148&address=GAFFFB7H3LG42O6JA63FJDRK4PP4JCNEOPHLGLLFH625X2KFYQ4UYVM4)
319
206
 
320
207
  ## Code of Conduct
321
208
 
@@ -352,7 +239,7 @@ How do I know when to release 1.0.0?
352
239
 
353
240
  ## License
354
241
 
355
- MIT © typedly ([license][typedly-license])
242
+ MIT © typedly ([license][package-license])
356
243
 
357
244
  ## Packages
358
245
 
@@ -369,23 +256,29 @@ MIT © typedly ([license][typedly-license])
369
256
  - **[@typedly/regexp](https://github.com/typedly/regexp)**: A **TypeScript** type definitions package for `RegExp`.
370
257
  - **[@typedly/symbol](https://github.com/typedly/symbol)**: A **TypeScript** type definitions package for various symbols.
371
258
 
259
+ <!-- Funding -->
260
+ [github-badge-sponsor]: https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/angular-package
261
+ [github-sponsor-link]: https://github.com/sponsors/angular-package
262
+ [patreon-badge]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Dangularpackage%26type%3Dpatrons&style=flat
263
+ [patreon-link]: https://www.patreon.com/join/angularpackage/checkout?fan_landing=true&rid=0
264
+
372
265
  <!-- This package: typedly -->
373
266
  <!-- GitHub: badges -->
374
- [typedly-badge-issues]: https://img.shields.io/github/issues/typedly/collection
375
- [typedly-badge-forks]: https://img.shields.io/github/forks/typedly/collection
376
- [typedly-badge-stars]: https://img.shields.io/github/stars/typedly/collection
377
- [typedly-badge-license]: https://img.shields.io/github/license/typedly/collection
267
+ [package-badge-issues]: https://img.shields.io/github/issues/typedly/collection
268
+ [package-badge-forks]: https://img.shields.io/github/forks/typedly/collection
269
+ [package-badge-stars]: https://img.shields.io/github/stars/typedly/collection
270
+ [package-badge-license]: https://img.shields.io/github/license/typedly/collection
378
271
  <!-- GitHub: badges links -->
379
- [typedly-issues]: https://github.com/typedly/collection/issues
380
- [typedly-forks]: https://github.com/typedly/collection/network
381
- [typedly-license]: https://github.com/typedly/collection/blob/master/LICENSE
382
- [typedly-stars]: https://github.com/typedly/collection/stargazers
272
+ [package-issues]: https://github.com/typedly/collection/issues
273
+ [package-forks]: https://github.com/typedly/collection/network
274
+ [package-license]: https://github.com/typedly/collection/blob/master/LICENSE
275
+ [package-stars]: https://github.com/typedly/collection/stargazers
383
276
  <!-- This package -->
384
277
 
385
278
  <!-- Package: typedly -->
386
279
  <!-- npm -->
387
- [typedly-npm-badge-svg]: https://badge.fury.io/js/@typedly%2Fcollection.svg
388
- [typedly-npm-badge]: https://badge.fury.io/js/@typedly%2Fcollection
280
+ [package-npm-badge-svg]: https://badge.fury.io/js/@typedly%2Fcollection.svg
281
+ [package-npm-badge]: https://badge.fury.io/js/@typedly%2Fcollection
389
282
 
390
283
  <!-- GIT -->
391
284
  [git-semver]: http://semver.org/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typedly/collection",
3
- "version": "4.0.0",
3
+ "version": "6.0.0",
4
4
  "author": "wwwdev.io <dev@wwwdev.io>",
5
5
  "description": "A TypeScript type definitions package for data collections with customizable storage.",
6
6
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "registry": "https://registry.npmjs.org"
10
10
  },
11
11
  "peerDependencies": {
12
- "@typedly/data": "^4.0.0"
12
+ "@typedly/adaptable-data": "^4.1.0"
13
13
  },
14
14
  "repository": {
15
15
  "type": "git",
@@ -29,7 +29,27 @@
29
29
  "url": "https://donate.stripe.com/dR614hfDZcJE3wAcMM"
30
30
  },
31
31
  {
32
- "type": "revolut",
32
+ "type": "Donorbox",
33
+ "url": "https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=o"
34
+ },
35
+ {
36
+ "type": "Open Collective",
37
+ "url": "https://opencollective.com/sterblack"
38
+ },
39
+ {
40
+ "type": "Ko-fi",
41
+ "url": "https://ko-fi.com/sterblack"
42
+ },
43
+ {
44
+ "type": "4Fund",
45
+ "url": "https://4fund.com/bruubs"
46
+ },
47
+ {
48
+ "type": "paypal",
49
+ "url": "https://paypal.me/sterblack"
50
+ },
51
+ {
52
+ "type": "individual",
33
53
  "url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29"
34
54
  }
35
55
  ],
@@ -1,103 +1,199 @@
1
- import { DataShape, AsyncReturn } from '@typedly/data';
1
+ import { IterableElement, DataShape, AsyncReturn, DataSettings, InferValue } from '@typedly/data';
2
+ import { Collection } from '@typedly/data-traits';
2
3
  import { ConstrainedConstructor } from '@typedly/constructor';
3
4
 
4
5
  /**
5
- * @description Represents a collection of elements.
6
+ * @description The `CollectionShape` interface defines the structure and behavior of a collection data structure, which can be implemented by various types of collections such as sets, arrays, or maps.
7
+ * It extends the `DataShape` interface, allowing it to inherit common data-related properties and methods while also introducing collection-specific functionalities.
6
8
  * @export
7
- * @interface Collection
8
- * @template E The type of elements in the collection.
9
- * @template [T=any] The type of the value in the collection, data of elements.
10
- * @template {boolean} [R=false] The `boolean` type to determine async methods.
11
- * @extends {DataShape<T, R>}
9
+ * @interface CollectionShape
10
+ * @template {Iterable<E>} T The iterable collection type.
11
+ * @template [E=IterableElement<T>] The type of elements in the collection inferred from the `T` if possible.
12
+ * @template {boolean} [R=false] The async behavior flag.
13
+ * @extends {DataShape<T, R>} The data-related functionalities defined in `DataShape`.
14
+ * @extends {CollectionTrait<E, R>} The base collection functionalities defined in `CollectionTrait`.
12
15
  */
13
- interface CollectionShape<E, T = any, R extends boolean = false> extends DataShape<T, R> {
14
- /**
15
- * @description Indicates whether the collection operates in asynchronous mode.
16
- * @readonly
17
- * @type {R}
18
- */
19
- readonly async: R;
20
- /**
21
- * @description The number of items in the collection.
22
- * @returns {number}
23
- */
24
- readonly size: number;
25
- /**
26
- * @description Adds elements to the collection.
27
- * @param {...E[]} element Element of type `T` to add.
28
- * @returns {AsyncReturn<R, this>} The collection instance `this`, or in `Promise`.
29
- */
30
- add(...element: E[]): AsyncReturn<R, this>;
31
- /**
32
- * @description Deletes elements from the collection.
33
- * @param {...E[]} element Element of type `T` to delete.
34
- * @returns {AsyncReturn<R, boolean>} `true` if the element was successfully deleted, otherwise `false`.
35
- */
36
- delete(...element: E[]): AsyncReturn<R, boolean>;
16
+ interface CollectionShape<T extends Iterable<E>, E = IterableElement<T>, R extends boolean = false> extends DataShape<T, R>, Collection<E, R> {
37
17
  /**
38
18
  * @description Executes a provided function once for each collection element.
39
- * @param {(value: E, value2: E, collection: CollectionShape<E, T, R>) => void} callbackfn Function to execute for each element.
40
- * @param {AsyncReturn<R, this>} [thisArg] Value to use as `this` when executing `callbackfn`.
19
+ * @param {(element: E, collection: this) => void} callbackfn Function to execute for each element.
20
+ * @param {?*} [thisArg] Value to use as `this` when executing `callbackfn`.
21
+ * @returns {AsyncReturn<R, this>}
41
22
  */
42
- forEach(callbackfn: (element: E, element2: E, collection: CollectionShape<E, T, R>) => void, thisArg?: any): AsyncReturn<R, this>;
23
+ forEach(callbackfn: (element: E, collection: this) => void, thisArg?: any): AsyncReturn<R, this>;
43
24
  /**
44
- * @description Checks if every item exists in the collection.
45
- * @param {...E[]} element Element of type `T` to check for existence.
46
- * @returns {AsyncReturn<R, boolean>} `true` if the element exists, otherwise `false`.
25
+ * @description Returns an iterable of the values in the collection.
26
+ * The type of the values is determined by the generic type `T`, which represents the iterable collection type.
27
+ * If `T` is not provided, it defaults to `Iterable<unknown>`, and the values will be of type `unknown`.
28
+ * @returns {IterableIterator<IterableElement<T>>}
47
29
  */
48
- has(...element: E[]): AsyncReturn<R, boolean>;
30
+ [Symbol.iterator](): IterableIterator<IterableElement<T>>;
49
31
  }
50
32
 
51
33
  /**
52
34
  * @description The adapter interface for collections.
53
35
  * @export
54
36
  * @interface CollectionAdapter
55
- * @template E The type of elements in the collection.
56
- * @template T The type of the value in the collection, data of elements.
57
- * @template R The `boolean` type to determine async methods.
58
- * @extends {CollectionShape<E, T>}
37
+ * @template {Iterable<E>} T The type of the collection.
38
+ * @template [E=IterableElement<T>] The type of elements in the collection.
39
+ * @template {boolean} [R=false] The async behavior flag.
40
+ * @extends {CollectionShape<T, E, R>} The collection shape interface.
41
+ * @since
42
+ * @version
43
+ * @author Ścibor Rudnicki <sciborrudnicki@wvvw.dev>
44
+ * @see {@link CollectionShape}
45
+ * @see {@link IterableElement}
46
+ * @example
47
+ * ```ts
48
+ * import { CollectionAdapter } from "@typedly/collection";
49
+ *
50
+ * class MyCollectionAdapter implements CollectionAdapter<string[], string, false> {
51
+ * readonly version = "1.0.0";
52
+ * // Implement the methods defined in CollectionShape...
53
+ * }
59
54
  */
60
- interface CollectionAdapter<E, T, R extends boolean = false> extends CollectionShape<E, T, R> {
61
- version: string;
55
+ interface CollectionAdapter<T extends Iterable<E>, E = IterableElement<T>, R extends boolean = false> extends CollectionShape<T, E, R> {
56
+ readonly version: string;
62
57
  }
63
58
 
64
59
  /**
65
60
  * @description The interface of adapter constructor.
66
61
  * @export
67
62
  * @interface CollectionAdapterConstructor
68
- * @template E Elements type.
63
+ * @template E Elements type of `T`.
64
+ * @template T Value type under which the elements are stored.
69
65
  * @template {boolean} [R=false] The boolean type indicates the async methods.
70
- * @template {C} [C={async?: R, value?: T}] The configuration object type for the constructor, which has an `async` property of type `R` and an optional `value` property of type `T`.
71
- * @template {CollectionAdapter<E, T, R>} [A=CollectionAdapter<E, T, R>]
66
+ * @template {CollectionAdapter<E, T, R>} [A=CollectionAdapter<E, T, R>] The adapter type.
72
67
  */
73
- interface CollectionAdapterConstructor<E, T, R extends boolean = false, C extends {
74
- async?: R;
75
- value?: T;
76
- } = {
77
- async?: R;
78
- value?: T;
79
- }, A extends CollectionAdapter<E, T, R> = CollectionAdapter<E, T, R>> {
80
- new ({ async, value }: C, ...elements: E[]): A;
68
+ interface CollectionAdapterConstructor<A extends CollectionAdapter<T, E, R>, E, T extends Iterable<E>, R extends boolean> {
69
+ new (...elements: E[]): A;
81
70
  }
82
71
 
83
72
  /**
84
- * @description The constructor type for CollectionShape.
73
+ * @description The constructor type for `CollectionShape`.
85
74
  * @export
86
- * @template E The type of the elements in the collection.
87
- * @template T The type of the value in the collection, data of elements.
75
+ * @interface CollectionConstructor
76
+ * @template {CollectionShape<T, E, R>} S The collection shape type.
77
+ * @template {Iterable<E>} [T=S extends CollectionShape<infer U, any, any> ? U : unknown] The type of the elements in the collection, inferred from the collection shape or defaults to `unknown` if not specified.
78
+ * @template [E=S extends CollectionShape<any, infer U, any> ? U : unknown] The element type inferred from the collection shape or defaults to `unknown` if not specified.
79
+ * @template {boolean} [R=S extends CollectionShape<any, any, infer U> ? U : false] The async behavior flag inferred from the collection shape or defaults to `false` if not specified.
80
+ * @extends {ConstrainedConstructor<CollectionShape<T, E, R>, S, E[]>}
81
+ */
82
+ interface CollectionConstructor<S extends CollectionShape<T, E, R>, T extends Iterable<E> = S extends CollectionShape<infer U, any, any> ? U : unknown, E = S extends CollectionShape<any, infer U, any> ? U : unknown, R extends boolean = S extends CollectionShape<any, any, infer U> ? U : false> extends ConstrainedConstructor<CollectionShape<T, E, R>, S, E[]> {
83
+ }
84
+
85
+ /**
86
+ * @description Represents the settings for a collection.
87
+ * @export
88
+ * @interface CollectionSettings
89
+ * @template {Iterable<E>} T The iterable type of the value in the collection.
90
+ * @template [E=IterableElement<T>] The type of the elements in the collection inferred from the `T` if possible.
88
91
  * @template {boolean} [R=false] The `boolean` type to determine async methods.
89
- * @template {G} [G={async?: R, value?: T}] The configuration object type for the constructor, which has an `async` property of type `R` and an optional `value` property of type `T`.
90
- * @template {CollectionShape<E, T, R>} [C=CollectionShape<E, T, R>]
92
+ * @extends {DataSettings<R>} The base settings for data.
91
93
  */
92
- type CollectionConstructor<E, T, R extends boolean = false, G extends {
93
- async?: R;
94
- value?: T;
95
- } = {
96
- async?: R;
94
+ interface CollectionSettings<T extends Iterable<E>, E = IterableElement<T>, R extends boolean = false> extends DataSettings<R> {
95
+ /**
96
+ * @description The initial value of the collection. The type of the value is determined by the generic type `T`. If not provided, it defaults to `undefined`.
97
+ * @type {?T}
98
+ */
97
99
  value?: T;
98
- }, C extends CollectionShape<E, T, R> = CollectionShape<E, T, R>> = ConstrainedConstructor<CollectionShape<E, T, R>, C, [
99
- G,
100
- ...E[]
101
- ]>;
100
+ /**
101
+ * @description The maximum number of items the collection can hold. If not provided, it defaults to `undefined`, indicating no limit.
102
+ * @default undefined
103
+ * @type {?number}
104
+ */
105
+ maxSize?: number;
106
+ /**
107
+ * @description The initial capacity of the collection. This is a hint for optimization and does not limit the number of items. If not provided, it defaults to `undefined`.
108
+ * @default undefined
109
+ * @type {?number}
110
+ */
111
+ capacity?: number;
112
+ /**
113
+ * @description Indicates whether the collection should automatically sort its elements. If `true`, the collection will maintain its elements in sorted order based on the provided `comparator` function. If not provided, it defaults to `false`.
114
+ * @default false
115
+ * @type {?boolean}
116
+ */
117
+ autoSort?: boolean;
118
+ /**
119
+ * @description A function that defines the sort order of the collection's elements. It should return a negative number if `a` should come before `b`, a positive number if `a` should come after `b`, or `0` if they are considered equal. If not provided, it defaults to `undefined`.
120
+ * @type {?(a: E, b: E) => number}
121
+ */
122
+ comparator?: (a: E, b: E) => number;
123
+ /**
124
+ * @description Indicates whether the collection should enforce uniqueness of its elements. If `true`, the collection will not allow duplicate elements. If not provided, it defaults to `false`.
125
+ * @default false
126
+ * @type {?boolean}
127
+ */
128
+ unique?: boolean;
129
+ /**
130
+ * @description Indicates whether the collection should be immutable. If `true`, the collection will not allow modifications after it has been created. If not provided, it defaults to `false`.
131
+ * @default false
132
+ * @type {?boolean}
133
+ */
134
+ immutable?: boolean;
135
+ /**
136
+ * @description Indicates whether the collection should log its actions. If `true`, the collection will log actions such as additions, removals, and updates. If not provided, it defaults to `false`.
137
+ * @default false
138
+ * @type {?boolean}
139
+ */
140
+ log?: boolean;
141
+ /**
142
+ * @description Indicates whether the collection should be lazily initialized. If `true`, the collection will delay initialization until it is first accessed. If not provided, it defaults to `false`.
143
+ * @default false
144
+ * @type {?boolean}
145
+ */
146
+ lazy?: boolean;
147
+ /**
148
+ * @description The maximum time, in milliseconds, that the collection will wait for an operation to complete before timing out. If not provided, it defaults to `undefined`, indicating no timeout.
149
+ * @type {?number}
150
+ */
151
+ timeout?: number;
152
+ /**
153
+ * @description The number of items to prefetch in the collection. If not provided, it defaults to `undefined`, indicating no prefetching.
154
+ * @type {?number}
155
+ */
156
+ prefetch?: number;
157
+ /**
158
+ * @description Indicates whether the collection should be locked. If `true`, the collection will not allow any modifications after it has been created. If not provided, it defaults to `false`.
159
+ * @default false
160
+ * @type {?boolean}
161
+ */
162
+ locked?: boolean;
163
+ /**
164
+ * @description The name of the collection. If not provided, it defaults to `undefined`.
165
+ * @type {?string}
166
+ */
167
+ name?: string;
168
+ /**
169
+ * @description Function to validate elements before adding
170
+ */
171
+ validator?: (element: E) => boolean;
172
+ }
173
+
174
+ /**
175
+ * @description Infer the collection type from the collection settings or adapter.
176
+ * @export
177
+ * @template C The collection settings type to infer the collection type from.
178
+ * @template [I=undefined] The collection shape type to infer the collection type from if it cannot be inferred from the collection settings.
179
+ * @template [F=unknown] The fallback type to use if the collection type cannot be inferred from the collection settings or the adapter, defaults to `unknown`.
180
+ */
181
+ type InferCollectionType<C, I = undefined, F = unknown> = C extends CollectionSettings<infer T, any, any> ? T : InferValue<I, F>;
182
+
183
+ /**
184
+ * @description Type to infer the element type from collection settings or adapter.
185
+ * @export
186
+ * @template C The collections settings type to infer the element type from.
187
+ * @template [F=unknown] The fallback type if neither settings nor adapter provide an element type.
188
+ */
189
+ type InferElementFromSettings<C, F = unknown> = C extends CollectionSettings<infer T, infer E, any> ? T extends Iterable<infer U> ? U : E : C extends CollectionSettings<any, infer E, any> ? E : F;
190
+
191
+ /**
192
+ * @description Type to infer the element type from collection settings or adapter, with special handling for common collection types like Set, Array, and Map.
193
+ * @export
194
+ * @template C The collections configuration type to infer the element type from.
195
+ * @template [F=unknown] The fallback type to use if the element type cannot be inferred from either the collection settings or the adapter.
196
+ */
197
+ type InferElement<C, I = undefined, F = unknown> = InferElementFromSettings<C, F> extends F ? I extends CollectionShape<any, infer E, any> ? E : F : InferElementFromSettings<C, F>;
102
198
 
103
- export type { CollectionAdapter, CollectionAdapterConstructor, CollectionConstructor, CollectionShape };
199
+ export type { CollectionAdapter, CollectionAdapterConstructor, CollectionConstructor, CollectionSettings, CollectionShape, InferCollectionType, InferElement, InferElementFromSettings };