@typedly/collection 4.0.0 → 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.
package/README.md CHANGED
@@ -27,12 +27,15 @@ A **TypeScript** type definitions package for data collections with customizable
27
27
 
28
28
  - [Installation](#installation)
29
29
  - [Api](#api)
30
- - Interface
31
- - [`CollectionAdapter`](#collectionadapter)
30
+ - Constructor
32
31
  - [`CollectionAdapterConstructor`](#collectionadapterconstructor)
33
- - [`CollectionShape`](#collectionshape)
34
- - Type
35
32
  - [`CollectionConstructor`](#collectionconstructor)
33
+ - [`ConfigurableCollectionAdapterConstructor`](#configurablecollectionadapterconstructor)
34
+ - [`ConfigurableCollectionConstructor`](#configurablecollectionconstructor)
35
+ - Main
36
+ - [`CollectionAdapter`](#collectionadapter)
37
+ - [`CollectionSettings`](#collectionsettings)
38
+ - [`CollectionShape`](#collectionshape)
36
39
  - [Contributing](#contributing)
37
40
  - [Support](#support)
38
41
  - [Code of Conduct](#code-of-conduct)
@@ -51,24 +54,20 @@ npm install @typedly/collection --save-peer
51
54
 
52
55
  ```typescript
53
56
  import {
57
+ // Constructor.
58
+ CollectionAdapterConstructor,
59
+ CollectionConstructor,
60
+ ConfigurableCollectionAdapterConstructor,
54
61
  // Interface.
55
62
  CollectionAdapter,
56
- CollectionAdapterConstructor,
63
+ CollectionSettings,
57
64
  CollectionShape,
58
- // Type.
59
- CollectionConstructor
60
65
  } from '@typedly/collection';
61
66
  ```
62
67
 
63
68
  ### Interface
64
69
 
65
- ### `CollectionAdapter`
66
-
67
- The adapter interface for collections.
68
-
69
- ```typescript
70
- import { CollectionAdapter } from '@typedly/collection';
71
- ```
70
+ ### Constructor
72
71
 
73
72
  ### `CollectionAdapterConstructor`
74
73
 
@@ -92,14 +91,13 @@ export class ExampleCollectionAdapter<
92
91
  return this.#items.length;
93
92
  }
94
93
  public get value(): T {
95
- // Implementation depends on specific requirements.
96
- return {} as T;
94
+ return this.#items as T;
97
95
  }
98
96
  version = "1.0.0";
99
97
  #async: R;
100
98
  #items: E[] = [];
101
- constructor({async}: {async: R}, ...elements: E[]) {
102
- this.#async = async;
99
+ constructor(...elements: E[]) {
100
+ this.#async = false as R;
103
101
  this.#items.push(...elements);
104
102
  }
105
103
  public add(...element: E[]): AsyncReturn<R, this> {
@@ -129,22 +127,19 @@ export class ExampleCollectionAdapter<
129
127
  return this as AsyncReturn<R, this>;
130
128
  }
131
129
  public getValue(): AsyncReturn<R, T> {
132
- // Implementation depends on specific requirements.
133
- return {} as AsyncReturn<R, T>;
130
+ return this.#items as AsyncReturn<R, T>;
134
131
  }
135
132
  public has(element: E): AsyncReturn<R, boolean> {
136
133
  return this.#items.includes(element) as AsyncReturn<R, boolean>;
137
134
  }
138
135
  public lock(): this {
139
- // Implementation depends on specific requirements.
140
136
  return this;
141
137
  }
142
138
  public setValue(value: T): AsyncReturn<R, this> {
143
- // Implementation depends on specific requirements.
139
+ this.#items = value as unknown as E[];
144
140
  return this as AsyncReturn<R, this>;
145
141
  }
146
142
  public unlock(): AsyncReturn<R, this> {
147
- // Implementation depends on specific requirements.
148
143
  return this as AsyncReturn<R, this>;
149
144
  }
150
145
  }
@@ -156,18 +151,52 @@ function createAdapter<
156
151
  R extends boolean = false,
157
152
  A extends CollectionAdapter<E, T, R> = CollectionAdapter<E, T, R>
158
153
  >(
159
- AdapterCtor: CollectionAdapterConstructor<E, T, R, { async: R }, A>,
160
- async: R,
154
+ AdapterCtor: CollectionAdapterConstructor<E, T, R, A>,
161
155
  ...elements: E[]
162
156
  ): A {
163
- return new AdapterCtor({ async }, ...elements);
157
+ return new AdapterCtor(...elements);
164
158
  }
165
159
 
166
160
  // ExampleCollectionAdapter<number, unknown, false>
167
- const adapter1 = createAdapter(ExampleCollectionAdapter, false, 1, 2, 3);
161
+ const adapter1 = createAdapter(ExampleCollectionAdapter, 1, 2, 3);
168
162
  // ExampleCollectionAdapter<string, unknown, true>
169
- const adapter2 = createAdapter(ExampleCollectionAdapter, true, 'a', 'b', 'c');
163
+ const adapter2 = createAdapter(ExampleCollectionAdapter, 'a', 'b', 'c');
164
+ ```
165
+
166
+ ### `CollectionConstructor`
167
+
168
+ ```typescript
169
+ import { CollectionConstructor } from '@typedly/collection';
170
+ ```
171
+
172
+ ### `ConfigurableCollectionAdapterConstructor`
173
+
174
+ ```typescript
175
+ import { ConfigurableCollectionAdapterConstructor } from '@typedly/collection';
176
+ ```
177
+
178
+ ### `ConfigurableCollectionConstructor`
179
+
180
+ ```typescript
181
+ import { ConfigurableCollectionConstructor } from '@typedly/collection';
182
+ ```
183
+
184
+ ### Main
185
+
186
+ ### `CollectionAdapter`
187
+
188
+ The adapter interface for collections.
189
+
190
+ ```typescript
191
+ import { CollectionAdapter } from '@typedly/collection';
192
+ ```
193
+
194
+ ### `CollectionSettings`
195
+
196
+ Represents the settings for a collection.
170
197
 
198
+ ```typescript
199
+ import { CollectionSettings } from '@typedly/collection';
171
200
  ```
172
201
 
173
202
  ### `CollectionShape`
@@ -182,15 +211,12 @@ export class AnyCollection<
182
211
  E,
183
212
  T = Set<E>
184
213
  > implements CollectionShape<E, T, false> {
185
- async = false as false;
214
+ get size(): number { return (this.#items as any).size; }
215
+ get value(): T { return this.#items; }
216
+ get [Symbol.toStringTag](): string { return 'AnyCollection'; }
186
217
 
187
- // Data shape method.
188
- get value(): T {
189
- // Implementation depends on specific requirements.
190
- return this.#items;
191
- }
218
+ async = false as false;
192
219
 
193
- // Example internal storage.
194
220
  #items: T;
195
221
 
196
222
  constructor(
@@ -203,61 +229,19 @@ export class AnyCollection<
203
229
  elements.forEach(element => (this.#items as any).add(element));
204
230
  }
205
231
 
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
-
232
+ add(element: E): this { (this.#items as any).add(element); return this; }
233
+ clear(): this { return this; }
234
+ delete(element: E): boolean { return (this.#items as any).delete(element); }
235
+ destroy(): this { return this; }
242
236
  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
- });
237
+ (this.#items as any).forEach((value: E) => callbackfn.call(thisArg, value, value, this));
246
238
  return this;
247
239
  }
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
-
240
+ has(element: E): boolean { return (this.#items as any).has(element); }
241
+ lock(): this { return this; }
242
+ getValue(): T { return this.#items; }
243
+ setValue(value: T): this { this.#items = value; return this; }
244
+ unlock(): this { return this; }
261
245
  [Symbol.iterator](): IterableIterator<IterValue<T>> {
262
246
  return (this.#items as any).values();
263
247
  }
@@ -284,14 +268,6 @@ const anyCollection2 = new AnyCollection<{age: number}, Set<{age: number}>>(
284
268
  console.log(`anyCollection2:`, anyCollection2.value);
285
269
  ```
286
270
 
287
- ### Type
288
-
289
- ### `CollectionConstructor`
290
-
291
- ```typescript
292
- import { CollectionConstructor } from '@typedly/collection';
293
- ```
294
-
295
271
  ## Contributing
296
272
 
297
273
  Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
@@ -308,6 +284,7 @@ Support via:
308
284
  - [DonorBox](https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=o)
309
285
  - [Patreon](https://www.patreon.com/checkout/angularpackage?rid=0&fan_landing=true&view_as=public)
310
286
  - [4Fund](https://4fund.com/bruubs)
287
+ - [PayPal](https://paypal.me/sterblack)
311
288
 
312
289
  or via Trust Wallet
313
290
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typedly/collection",
3
- "version": "4.0.0",
3
+ "version": "5.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",
@@ -1,6 +1,100 @@
1
1
  import { DataShape, AsyncReturn } from '@typedly/data';
2
2
  import { ConstrainedConstructor } from '@typedly/constructor';
3
3
 
4
+ /**
5
+ * @description Represents the settings for a collection.
6
+ * @export
7
+ * @interface CollectionSettings
8
+ * @template E The type of the elements in the collection.
9
+ * @template T The type of the value in the collection.
10
+ * @template {boolean} [R=false] The `boolean` type to determine async methods.
11
+ */
12
+ interface CollectionSettings<E, T, R extends boolean = false> {
13
+ /**
14
+ * @description The asynchronous mode of the collection. If `true`, collection methods will return Promises.
15
+ * @default false
16
+ * @type {?R}
17
+ */
18
+ async?: R;
19
+ /**
20
+ * @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`.
21
+ * @type {?T}
22
+ */
23
+ value?: T;
24
+ /**
25
+ * @description The maximum number of items the collection can hold. If not provided, it defaults to `undefined`, indicating no limit.
26
+ * @default undefined
27
+ * @type {?number}
28
+ */
29
+ maxSize?: number;
30
+ /**
31
+ * @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`.
32
+ * @default undefined
33
+ * @type {?number}
34
+ */
35
+ capacity?: number;
36
+ /**
37
+ * @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`.
38
+ * @default false
39
+ * @type {?boolean}
40
+ */
41
+ autoSort?: boolean;
42
+ /**
43
+ * @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`.
44
+ * @type {?(a: E, b: E) => number}
45
+ */
46
+ comparator?: (a: E, b: E) => number;
47
+ /**
48
+ * @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`.
49
+ * @default false
50
+ * @type {?boolean}
51
+ */
52
+ unique?: boolean;
53
+ /**
54
+ * @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`.
55
+ * @default false
56
+ * @type {?boolean}
57
+ */
58
+ immutable?: boolean;
59
+ /**
60
+ * @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`.
61
+ * @default false
62
+ * @type {?boolean}
63
+ */
64
+ log?: boolean;
65
+ /**
66
+ * @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`.
67
+ * @default false
68
+ * @type {?boolean}
69
+ */
70
+ lazy?: boolean;
71
+ /**
72
+ * @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.
73
+ * @type {?number}
74
+ */
75
+ timeout?: number;
76
+ /**
77
+ * @description The number of items to prefetch in the collection. If not provided, it defaults to `undefined`, indicating no prefetching.
78
+ * @type {?number}
79
+ */
80
+ prefetch?: number;
81
+ /**
82
+ * @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`.
83
+ * @default false
84
+ * @type {?boolean}
85
+ */
86
+ locked?: boolean;
87
+ /**
88
+ * @description The name of the collection. If not provided, it defaults to `undefined`.
89
+ * @type {?string}
90
+ */
91
+ name?: string;
92
+ /**
93
+ * @description Function to validate elements before adding
94
+ */
95
+ validator?: (element: E) => boolean;
96
+ }
97
+
4
98
  /**
5
99
  * @description Represents a collection of elements.
6
100
  * @export
@@ -17,6 +111,12 @@ interface CollectionShape<E, T = any, R extends boolean = false> extends DataSha
17
111
  * @type {R}
18
112
  */
19
113
  readonly async: R;
114
+ /**
115
+ * @description The configuration settings of the collection.
116
+ * @readonly
117
+ * @type {?CollectionSettings<E, T, R>}
118
+ */
119
+ readonly configuration?: CollectionSettings<E, T, R>;
20
120
  /**
21
121
  * @description The number of items in the collection.
22
122
  * @returns {number}
@@ -46,6 +146,23 @@ interface CollectionShape<E, T = any, R extends boolean = false> extends DataSha
46
146
  * @returns {AsyncReturn<R, boolean>} `true` if the element exists, otherwise `false`.
47
147
  */
48
148
  has(...element: E[]): AsyncReturn<R, boolean>;
149
+ /**
150
+ * @description Sets the asynchronous mode of the collection.
151
+ * @param {R} async The boolean type to determine async methods.
152
+ * @returns {this} The collection instance `this`.
153
+ */
154
+ setAsync?(async: R): this;
155
+ /**
156
+ * @description Converts the collection to an array of elements.
157
+ * @returns {AsyncReturn<R, E[]>} The array of elements, or in `Promise` if `R` is `true`.
158
+ */
159
+ toArray?(): AsyncReturn<R, E[]>;
160
+ /**
161
+ * @description
162
+ * @param {R} async
163
+ * @returns {CollectionShape<E, T, R>}
164
+ */
165
+ with?(async: R): CollectionShape<E, T, R>;
49
166
  }
50
167
 
51
168
  /**
@@ -65,19 +182,13 @@ interface CollectionAdapter<E, T, R extends boolean = false> extends CollectionS
65
182
  * @description The interface of adapter constructor.
66
183
  * @export
67
184
  * @interface CollectionAdapterConstructor
68
- * @template E Elements type.
185
+ * @template E Elements type of `T`.
186
+ * @template T Value type under which the elements are stored.
69
187
  * @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>]
188
+ * @template {CollectionAdapter<E, T, R>} [A=CollectionAdapter<E, T, R>] The adapter type.
72
189
  */
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;
190
+ interface CollectionAdapterConstructor<E, T, R extends boolean = false, A extends CollectionAdapter<E, T, R> = CollectionAdapter<E, T, R>> {
191
+ new (...elements: E[]): A;
81
192
  }
82
193
 
83
194
  /**
@@ -85,19 +196,25 @@ interface CollectionAdapterConstructor<E, T, R extends boolean = false, C extend
85
196
  * @export
86
197
  * @template E The type of the elements in the collection.
87
198
  * @template T The type of the value in the collection, data of elements.
88
- * @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>]
199
+ * @template {boolean} [R=false] The boolean type indicates the async methods.
200
+ * @template {CollectionShape<E, T, R>} [S=CollectionShape<E, T, R>] The collection shape type.
91
201
  */
92
- type CollectionConstructor<E, T, R extends boolean = false, G extends {
93
- async?: R;
94
- value?: T;
95
- } = {
96
- async?: R;
97
- value?: T;
98
- }, C extends CollectionShape<E, T, R> = CollectionShape<E, T, R>> = ConstrainedConstructor<CollectionShape<E, T, R>, C, [
99
- G,
202
+ interface CollectionConstructor<E, T, R extends boolean = false, S extends CollectionShape<E, T, R> = CollectionShape<E, T, R>> extends ConstrainedConstructor<CollectionShape<E, T, R>, S, [
100
203
  ...E[]
101
- ]>;
204
+ ]> {
205
+ }
206
+
207
+ /**
208
+ * @description The interface of adapter constructor with configurable async mode.
209
+ * @export
210
+ * @interface ConfigurableCollectionAdapterConstructor
211
+ * @template E Elements type of `T`.
212
+ * @template T Value type under which the elements are stored.
213
+ * @template {CollectionSettings<E, T, any>} [C=CollectionSettings<E, T, any>]
214
+ * @template {CollectionAdapter<E, T, C['async']>} [A=CollectionAdapter<E, T, C['async']>]
215
+ */
216
+ interface ConfigurableCollectionAdapterConstructor<E, T, C extends CollectionSettings<E, T, any> = CollectionSettings<E, T, any>, A extends CollectionAdapter<E, T, C['async']> = CollectionAdapter<E, T, C['async']>> {
217
+ new (settings: C, ...elements: E[]): A;
218
+ }
102
219
 
103
- export type { CollectionAdapter, CollectionAdapterConstructor, CollectionConstructor, CollectionShape };
220
+ export type { CollectionAdapter, CollectionAdapterConstructor, CollectionConstructor, CollectionSettings, CollectionShape, ConfigurableCollectionAdapterConstructor };