@typedly/collection 3.0.0 → 4.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
@@ -29,6 +29,7 @@ A **TypeScript** type definitions package for data collections with customizable
29
29
  - [Api](#api)
30
30
  - Interface
31
31
  - [`CollectionAdapter`](#collectionadapter)
32
+ - [`CollectionAdapterConstructor`](#collectionadapterconstructor)
32
33
  - [`CollectionShape`](#collectionshape)
33
34
  - Type
34
35
  - [`CollectionConstructor`](#collectionconstructor)
@@ -52,6 +53,7 @@ npm install @typedly/collection --save-peer
52
53
  import {
53
54
  // Interface.
54
55
  CollectionAdapter,
56
+ CollectionAdapterConstructor,
55
57
  CollectionShape,
56
58
  // Type.
57
59
  CollectionConstructor
@@ -68,29 +70,137 @@ The adapter interface for collections.
68
70
  import { CollectionAdapter } from '@typedly/collection';
69
71
  ```
70
72
 
73
+ ### `CollectionAdapterConstructor`
74
+
75
+ The interface of adapter constructor.
76
+
77
+ ```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');
170
+
171
+ ```
172
+
71
173
  ### `CollectionShape`
72
174
 
175
+ Represents a collection of elements.
176
+
73
177
  ```typescript
74
178
  import { CollectionShape, IterValue } from '@typedly/collection';
75
179
 
180
+ // Example class implementing CollectionShape.
76
181
  export class AnyCollection<
77
- T,
78
- V = Set<T>
79
- > implements CollectionShape<T, V, false> {
182
+ E,
183
+ T = Set<E>
184
+ > implements CollectionShape<E, T, false> {
185
+ async = false as false;
186
+
80
187
  // Data shape method.
81
- get value(): V {
188
+ get value(): T {
82
189
  // Implementation depends on specific requirements.
83
- return {} as V;
190
+ return this.#items;
84
191
  }
85
192
 
86
193
  // Example internal storage.
87
- #items: V = new Set<T>() as V;
88
-
89
- constructor(initialItems?: T[], type: new (...args: any[]) => V = Set as any) {
90
- this.#items = new type();
91
- if (initialItems) {
92
- initialItems.forEach(item => (this.#items as any).add(item));
93
- }
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));
94
204
  }
95
205
 
96
206
  public clear(): this {
@@ -105,8 +215,13 @@ export class AnyCollection<
105
215
  // Implementation depends on specific requirements.
106
216
  return this;
107
217
  }
108
- public set(value: V): this {
218
+ public getValue(): T {
219
+ // Implementation depends on specific requirements.
220
+ return this.#items;
221
+ }
222
+ public setValue(value: T): this {
109
223
  // Implementation depends on specific requirements.
224
+ this.#items = value;
110
225
  return this;
111
226
  }
112
227
  public unlock(): this {
@@ -115,23 +230,23 @@ export class AnyCollection<
115
230
  }
116
231
 
117
232
 
118
- add(element: T): this {
233
+ add(element: E): this {
119
234
  (this.#items as any).add(element);
120
235
  return this;
121
236
  }
122
237
 
123
- delete(element: T): boolean {
238
+ delete(element: E): boolean {
124
239
  return (this.#items as any).delete(element);
125
240
  }
126
241
 
127
- forEach(callbackfn: (element: T, element2: T, collection: CollectionShape<T, V, false>) => void, thisArg?: any): this {
128
- (this.#items as any).forEach((value: T) => {
242
+ forEach(callbackfn: (element: E, element2: E, collection: CollectionShape<E, T, false>) => void, thisArg?: any): this {
243
+ (this.#items as any).forEach((value: E) => {
129
244
  callbackfn.call(thisArg, value, value, this);
130
245
  });
131
246
  return this;
132
247
  }
133
248
 
134
- has(element: T): boolean {
249
+ has(element: E): boolean {
135
250
  return (this.#items as any).has(element);
136
251
  }
137
252
 
@@ -143,7 +258,7 @@ export class AnyCollection<
143
258
  return 'MyCollection';
144
259
  }
145
260
 
146
- [Symbol.iterator](): IterableIterator<IterValue<V>> {
261
+ [Symbol.iterator](): IterableIterator<IterValue<T>> {
147
262
  return (this.#items as any).values();
148
263
  }
149
264
  }
@@ -151,10 +266,22 @@ export class AnyCollection<
151
266
  const obj1 = {age: 27};
152
267
  const obj2 = {age: 37};
153
268
  const obj3 = {age: 47};
154
- const anyCollection = new AnyCollection<{age: number}, WeakSet<{age: number}>>([], WeakSet)
269
+ const anyCollection1 = new AnyCollection<{age: number}, Set<{age: number}>>(
270
+ { async: false, value: new Set([{age: 27}, {age: 37}, {age: 47}]) }
271
+ )
155
272
  .add(obj1)
156
273
  .add(obj2)
157
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);
158
285
  ```
159
286
 
160
287
  ### Type
@@ -176,10 +303,11 @@ If you find this package useful and would like to support its and general develo
176
303
  Support via:
177
304
 
178
305
  - [Stripe](https://donate.stripe.com/dR614hfDZcJE3wAcMM)
179
- - [Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29)
306
+ - ~~[Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29)~~
180
307
  - [GitHub](https://github.com/sponsors/angular-package/sponsorships?sponsor=sciborrudnicki&tier_id=83618)
181
308
  - [DonorBox](https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=o)
182
309
  - [Patreon](https://www.patreon.com/checkout/angularpackage?rid=0&fan_landing=true&view_as=public)
310
+ - [4Fund](https://4fund.com/bruubs)
183
311
 
184
312
  or via Trust Wallet
185
313
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typedly/collection",
3
- "version": "3.0.0",
3
+ "version": "4.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": "^3.0.0"
12
+ "@typedly/data": "^4.0.0"
13
13
  },
14
14
  "repository": {
15
15
  "type": "git",
@@ -11,6 +11,17 @@ import { ConstrainedConstructor } from '@typedly/constructor';
11
11
  * @extends {DataShape<T, R>}
12
12
  */
13
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;
14
25
  /**
15
26
  * @description Adds elements to the collection.
16
27
  * @param {...E[]} element Element of type `T` to add.
@@ -35,11 +46,6 @@ interface CollectionShape<E, T = any, R extends boolean = false> extends DataSha
35
46
  * @returns {AsyncReturn<R, boolean>} `true` if the element exists, otherwise `false`.
36
47
  */
37
48
  has(...element: E[]): AsyncReturn<R, boolean>;
38
- /**
39
- * @description The number of items in the collection.
40
- * @returns {number}
41
- */
42
- readonly size: number;
43
49
  }
44
50
 
45
51
  /**
@@ -55,14 +61,43 @@ interface CollectionAdapter<E, T, R extends boolean = false> extends CollectionS
55
61
  version: string;
56
62
  }
57
63
 
64
+ /**
65
+ * @description The interface of adapter constructor.
66
+ * @export
67
+ * @interface CollectionAdapterConstructor
68
+ * @template E Elements type.
69
+ * @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>]
72
+ */
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;
81
+ }
82
+
58
83
  /**
59
84
  * @description The constructor type for CollectionShape.
60
85
  * @export
61
86
  * @template E The type of the elements in the collection.
62
87
  * @template T The type of the value in the collection, data of elements.
63
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`.
64
90
  * @template {CollectionShape<E, T, R>} [C=CollectionShape<E, T, R>]
65
91
  */
66
- type CollectionConstructor<E, T, R extends boolean = false, C extends CollectionShape<E, T, R> = CollectionShape<E, T, R>> = ConstrainedConstructor<CollectionShape<E, T, R>, C, E[]>;
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,
100
+ ...E[]
101
+ ]>;
67
102
 
68
- export type { CollectionAdapter, CollectionConstructor, CollectionShape };
103
+ export type { CollectionAdapter, CollectionAdapterConstructor, CollectionConstructor, CollectionShape };