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