@typedly/data 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 +72 -18
- package/package.json +3 -3
- package/{index.d.ts → types/typedly-data.d.ts} +57 -25
package/README.md
CHANGED
|
@@ -16,18 +16,26 @@
|
|
|
16
16
|
|
|
17
17
|
A **TypeScript** type definitions package for [**@typescript-package/data**](https://github.com/typescript-package/data).
|
|
18
18
|
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- **Adapter**: Optional data adapter to customize managing underlying data.
|
|
22
|
+
- **Shape**: The default shape for data of any `T` type with conditional async.
|
|
23
|
+
|
|
19
24
|
## Table of contents
|
|
20
25
|
|
|
21
26
|
- [Installation](#installation)
|
|
22
27
|
- [Api](#api)
|
|
23
28
|
- [Interface](#interface)
|
|
29
|
+
- [`DataAdapter`](#dataadapter)
|
|
24
30
|
- [`DataConstructor`](#dataconstructor)
|
|
25
31
|
- [`DataShape`](#datashape)
|
|
26
32
|
- [`ValueConstructor`](#valueconstructor)
|
|
27
33
|
- [`ValueShape`](#valueshape)
|
|
28
34
|
- [Type](#type)
|
|
35
|
+
- [`AsyncReturn`](#asyncreturn)
|
|
29
36
|
- [`DataConstructorInput`](#dataconstructorinput)
|
|
30
37
|
- [`DataConstructorTuple`](#dataconstructortuple)
|
|
38
|
+
- [`IterValue`](#itervalue)
|
|
31
39
|
- [Full example usage](#full-example-usage)
|
|
32
40
|
- [Contributing](#contributing)
|
|
33
41
|
- [Support](#support)
|
|
@@ -57,18 +65,31 @@ npm install @typedly/data --save-peer
|
|
|
57
65
|
```typescript
|
|
58
66
|
import {
|
|
59
67
|
// Interface.
|
|
68
|
+
DataAdapter,
|
|
60
69
|
DataConstructor,
|
|
61
70
|
DataShape,
|
|
62
71
|
ValueConstructor,
|
|
63
72
|
ValueShape,
|
|
64
73
|
// Type.
|
|
74
|
+
AsyncReturn,
|
|
65
75
|
DataConstructorInput,
|
|
66
76
|
DataConstructorTuple,
|
|
77
|
+
IterValue
|
|
67
78
|
} from '@typedly/data';
|
|
68
79
|
```
|
|
69
80
|
|
|
70
81
|
### Interface
|
|
71
82
|
|
|
83
|
+
#### `DataAdapter`
|
|
84
|
+
|
|
85
|
+
The adapter interface for data types.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { DataAdapter } from '@typedly/data';
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
[Source](https://github.com/typedly/data/blob/main/src/lib/interface/data-adapter.interface.ts)
|
|
92
|
+
|
|
72
93
|
#### `DataConstructor`
|
|
73
94
|
|
|
74
95
|
The constructor interface for data types.
|
|
@@ -82,7 +103,7 @@ export class ProfileData<
|
|
|
82
103
|
Value extends { age: number, name: string }
|
|
83
104
|
> implements DataShape<Value> {
|
|
84
105
|
|
|
85
|
-
get value(): Value {
|
|
106
|
+
public get value(): Value {
|
|
86
107
|
return {
|
|
87
108
|
age: this.#age,
|
|
88
109
|
name: this.#name
|
|
@@ -97,13 +118,12 @@ export class ProfileData<
|
|
|
97
118
|
this.#name = value.name;
|
|
98
119
|
}
|
|
99
120
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
121
|
+
public clear(): this { return this; }
|
|
122
|
+
public destroy(): this { return this; }
|
|
123
|
+
public lock(): this { return this; };
|
|
124
|
+
public getValue(): Value { return this.value; }
|
|
125
|
+
public setValue(value: Value): this { this.validate(value); return this; }
|
|
126
|
+
public validate(value: Value): boolean { return true; }
|
|
107
127
|
}
|
|
108
128
|
|
|
109
129
|
// Create `ProfileClass` with customizable data.
|
|
@@ -146,10 +166,16 @@ frankProfile.age; // 37
|
|
|
146
166
|
frankProfile.name; // Frank
|
|
147
167
|
|
|
148
168
|
// Set the data.
|
|
149
|
-
frankProfile.data.
|
|
169
|
+
frankProfile.data.setValue({ age: 37, name: 'Frank' });
|
|
150
170
|
frankProfile.data.clear();
|
|
151
171
|
frankProfile.data.lock();
|
|
152
172
|
frankProfile.data.value;
|
|
173
|
+
|
|
174
|
+
// Initialize with arguments.
|
|
175
|
+
const markProfile = new ProfileClass(
|
|
176
|
+
{ age: 27, name: 'Mark' },
|
|
177
|
+
[ProfileData, 'private', true]
|
|
178
|
+
);
|
|
153
179
|
```
|
|
154
180
|
|
|
155
181
|
[Source](https://github.com/typedly/data/blob/main/src/lib/interface/data-constructor.interface.ts)
|
|
@@ -161,13 +187,17 @@ The shape of a `Data` type.
|
|
|
161
187
|
```typescript
|
|
162
188
|
import { DataShape } from '@typedly/data';
|
|
163
189
|
|
|
164
|
-
class
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
190
|
+
// Create a simple data class with value as property.
|
|
191
|
+
export class TestData<Type> implements DataShape<Type> {
|
|
192
|
+
get value(): Type { return 27 as Type; }
|
|
193
|
+
clear(): this { return this; }
|
|
194
|
+
destroy(): this { return this; }
|
|
195
|
+
getValue(): Type { return 27 as Type; }
|
|
196
|
+
lock(): this { return this; };
|
|
197
|
+
setValue(value: Type): this { return this; }
|
|
198
|
+
constructor(value: Type, ...args: any[]) {
|
|
199
|
+
console.log(`Instantiated DataConstructor`, value, ...args);
|
|
200
|
+
}
|
|
171
201
|
}
|
|
172
202
|
```
|
|
173
203
|
|
|
@@ -193,6 +223,16 @@ import { ValueShape } from '@typedly/data';
|
|
|
193
223
|
|
|
194
224
|
### Type
|
|
195
225
|
|
|
226
|
+
#### `AsyncReturn`
|
|
227
|
+
|
|
228
|
+
The conditional return type for async methods.
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
import { AsyncReturn } from '@typedly/data';
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
[Source](https://github.com/typedly/data/blob/main/src/lib/type/async-return.type.ts)
|
|
235
|
+
|
|
196
236
|
#### `DataConstructorInput`
|
|
197
237
|
|
|
198
238
|
The input type for data constructors, with arguments support.
|
|
@@ -213,6 +253,16 @@ import { DataConstructorTuple } from '@typedly/data';
|
|
|
213
253
|
|
|
214
254
|
[Source](https://github.com/typedly/data/blob/main/src/lib/type/data-constructor-tuple.type.ts)
|
|
215
255
|
|
|
256
|
+
#### `IterValue`
|
|
257
|
+
|
|
258
|
+
The iterated value type.
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
import { IterValue } from '@typedly/data';
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
[Source](https://github.com/typedly/data/blob/main/src/lib/type/iter-value.type.ts)
|
|
265
|
+
|
|
216
266
|
### Full example usage
|
|
217
267
|
|
|
218
268
|
```typescript
|
|
@@ -268,7 +318,10 @@ export class ProfileDataOfValue<
|
|
|
268
318
|
this.#value = new valueCtor(value, ...args);
|
|
269
319
|
}
|
|
270
320
|
|
|
271
|
-
|
|
321
|
+
setValue(value: Value): this { this.validate(value); return this; }
|
|
322
|
+
getValue(): Value {
|
|
323
|
+
return this.#value.value;
|
|
324
|
+
}
|
|
272
325
|
clear(): this { return this; }
|
|
273
326
|
destroy(): this { return this; }
|
|
274
327
|
lock(): this { return this; };
|
|
@@ -342,10 +395,11 @@ If you find this package useful and would like to support its and general develo
|
|
|
342
395
|
Support via:
|
|
343
396
|
|
|
344
397
|
- [Stripe](https://donate.stripe.com/dR614hfDZcJE3wAcMM)
|
|
345
|
-
- [Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29)
|
|
398
|
+
- ~~[Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29)~~
|
|
346
399
|
- [GitHub](https://github.com/sponsors/angular-package/sponsorships?sponsor=sciborrudnicki&tier_id=83618)
|
|
347
400
|
- [DonorBox](https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=o)
|
|
348
401
|
- [Patreon](https://www.patreon.com/checkout/angularpackage?rid=0&fan_landing=true&view_as=public)
|
|
402
|
+
- [4Fund](https://4fund.com/bruubs)
|
|
349
403
|
|
|
350
404
|
or via Trust Wallet
|
|
351
405
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typedly/data",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"author": "wwwdev.io <dev@wwwdev.io>",
|
|
5
5
|
"description": "A TypeScript type definitions package for @typescript-package/data.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,13 +33,13 @@
|
|
|
33
33
|
}
|
|
34
34
|
],
|
|
35
35
|
"sideEffects": false,
|
|
36
|
-
"typings": "
|
|
36
|
+
"typings": "types/typedly-data.d.ts",
|
|
37
37
|
"exports": {
|
|
38
38
|
"./package.json": {
|
|
39
39
|
"default": "./package.json"
|
|
40
40
|
},
|
|
41
41
|
".": {
|
|
42
|
-
"types": "./
|
|
42
|
+
"types": "./types/typedly-data.d.ts",
|
|
43
43
|
"default": "./fesm2022/typedly-data.mjs"
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -1,17 +1,67 @@
|
|
|
1
1
|
import { DataConstructor as DataConstructor$1 } from '@typedly/constructor';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @description The conditional return type for async methods.
|
|
5
|
+
* @export
|
|
6
|
+
* @template Async The boolean flag indicating if the return type should be a Promise.
|
|
7
|
+
* @template T The type of the value to be returned.
|
|
8
|
+
*/
|
|
9
|
+
type AsyncReturn<Async, T> = Async extends true ? Promise<T> : T;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @description The type to provide data constructor with arguments.
|
|
13
|
+
* @export
|
|
14
|
+
* @template Value The value type.
|
|
15
|
+
* @template {DataShape<Value>} Instance The shape of the instance.
|
|
16
|
+
* @template {any[]} [Args=any[]] Additional Arguments passed to the instance.
|
|
17
|
+
*/
|
|
18
|
+
type DataConstructorTuple<Value, Instance extends DataShape<Value>, Args extends any[] = any[]> = [DataConstructor<Value, Instance, Args>, ...Args];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @description The input type for data constructors, with arguments support.
|
|
22
|
+
* @export
|
|
23
|
+
* @template Value The value type.
|
|
24
|
+
* @template {DataShape<Value>} Instance The data instance type.
|
|
25
|
+
* @template {any[]} [Args=any[]] The arguments to pass to instance.
|
|
26
|
+
*/
|
|
27
|
+
type DataConstructorInput<Value, Instance extends DataShape<Value>, Args extends any[] = any[]> = DataConstructor<Value, Instance, Args> | DataConstructorTuple<Value, Instance, Args>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @description The iterated value type.
|
|
31
|
+
* @export
|
|
32
|
+
* @template T The type of iterated value constrained by the `Iterable` type.
|
|
33
|
+
*/
|
|
34
|
+
type IterValue<T> = T extends Iterable<infer U> ? U : T;
|
|
35
|
+
|
|
3
36
|
/**
|
|
4
37
|
* @description The shape of a `Data` type.
|
|
5
38
|
* @export
|
|
6
39
|
* @interface DataShape
|
|
7
|
-
* @template
|
|
40
|
+
* @template T The value type.
|
|
41
|
+
* @template {boolean} [Async=false] The `Promise` return type for methods.
|
|
8
42
|
*/
|
|
9
|
-
interface DataShape<
|
|
10
|
-
|
|
11
|
-
clear(): this
|
|
12
|
-
destroy(): this
|
|
43
|
+
interface DataShape<T, Async extends boolean = false> {
|
|
44
|
+
value: T;
|
|
45
|
+
clear(): AsyncReturn<Async, this>;
|
|
46
|
+
destroy(): AsyncReturn<Async, this>;
|
|
47
|
+
getValue(): AsyncReturn<Async, T>;
|
|
13
48
|
lock(): this;
|
|
14
|
-
|
|
49
|
+
setValue(value: T): AsyncReturn<Async, this>;
|
|
50
|
+
[Symbol.toStringTag]?: string;
|
|
51
|
+
[Symbol.iterator]?(): IterableIterator<IterValue<T>>;
|
|
52
|
+
[Symbol.asyncIterator]?(): AsyncIterableIterator<IterValue<T>>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @description The adapter interface for data types.
|
|
57
|
+
* @export
|
|
58
|
+
* @interface DataAdapter
|
|
59
|
+
* @template T The type of the data value.
|
|
60
|
+
* @template {boolean} [R=false] The type of the return values for methods.
|
|
61
|
+
* @extends {DataShape<T, R>}
|
|
62
|
+
*/
|
|
63
|
+
interface DataAdapter<T, R extends boolean = false> extends DataShape<T, R> {
|
|
64
|
+
version?: string;
|
|
15
65
|
}
|
|
16
66
|
|
|
17
67
|
/**
|
|
@@ -49,22 +99,4 @@ interface ValueShape<Type> {
|
|
|
49
99
|
interface ValueConstructor<Value, Instance extends ValueShape<Value>, Args extends readonly any[] = any[]> extends DataConstructor$1<Value, ValueShape<Value>, Instance, [...Args]> {
|
|
50
100
|
}
|
|
51
101
|
|
|
52
|
-
|
|
53
|
-
* @description The type to provide data constructor with arguments.
|
|
54
|
-
* @export
|
|
55
|
-
* @template Value The value type.
|
|
56
|
-
* @template {DataShape<Value>} Instance The shape of the instance.
|
|
57
|
-
* @template {any[]} [Args=any[]] Additional Arguments passed to the instance.
|
|
58
|
-
*/
|
|
59
|
-
type DataConstructorTuple<Value, Instance extends DataShape<Value>, Args extends any[] = any[]> = [DataConstructor<Value, Instance, Args>, ...Args];
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @description The input type for data constructors, with arguments support.
|
|
63
|
-
* @export
|
|
64
|
-
* @template Value The value type.
|
|
65
|
-
* @template {DataShape<Value>} Instance The data instance type.
|
|
66
|
-
* @template {any[]} [Args=any[]] The arguments to pass to instance.
|
|
67
|
-
*/
|
|
68
|
-
type DataConstructorInput<Value, Instance extends DataShape<Value>, Args extends any[] = any[]> = DataConstructor<Value, Instance, Args> | DataConstructorTuple<Value, Instance, Args>;
|
|
69
|
-
|
|
70
|
-
export type { DataConstructor, DataConstructorInput, DataConstructorTuple, DataShape, ValueConstructor, ValueShape };
|
|
102
|
+
export type { AsyncReturn, DataAdapter, DataConstructor, DataConstructorInput, DataConstructorTuple, DataShape, IterValue, ValueConstructor, ValueShape };
|