@typedly/data 1.0.0 → 2.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 +231 -17
- package/index.d.ts +35 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,8 +23,12 @@ A **TypeScript** type definitions package for [**@typescript-package/data**](htt
|
|
|
23
23
|
- [Interface](#interface)
|
|
24
24
|
- [`DataConstructor`](#dataconstructor)
|
|
25
25
|
- [`DataShape`](#datashape)
|
|
26
|
+
- [`ValueConstructor`](#valueconstructor)
|
|
27
|
+
- [`ValueShape`](#valueshape)
|
|
26
28
|
- [Type](#type)
|
|
27
29
|
- [`DataConstructorInput`](#dataconstructorinput)
|
|
30
|
+
- [`DataConstructorTuple`](#dataconstructortuple)
|
|
31
|
+
- [Full example usage](#full-example-usage)
|
|
28
32
|
- [Contributing](#contributing)
|
|
29
33
|
- [Support](#support)
|
|
30
34
|
- [Code of Conduct](#code-of-conduct)
|
|
@@ -55,8 +59,11 @@ import {
|
|
|
55
59
|
// Interface.
|
|
56
60
|
DataConstructor,
|
|
57
61
|
DataShape,
|
|
62
|
+
ValueConstructor,
|
|
63
|
+
ValueShape,
|
|
58
64
|
// Type.
|
|
59
|
-
DataConstructorInput
|
|
65
|
+
DataConstructorInput,
|
|
66
|
+
DataConstructorTuple,
|
|
60
67
|
} from '@typedly/data';
|
|
61
68
|
```
|
|
62
69
|
|
|
@@ -67,9 +74,82 @@ import {
|
|
|
67
74
|
The constructor interface for data types.
|
|
68
75
|
|
|
69
76
|
```typescript
|
|
70
|
-
import { DataConstructor } from '@typedly/data';
|
|
77
|
+
import { DataConstructor, DataShape } from '@typedly/data';
|
|
78
|
+
|
|
79
|
+
// Import DataShape and DataConstructor.
|
|
80
|
+
// Create a data class that implements `DataShape` of `Type`.
|
|
81
|
+
export class ProfileData<
|
|
82
|
+
Value extends { age: number, name: string }
|
|
83
|
+
> implements DataShape<Value> {
|
|
84
|
+
|
|
85
|
+
get value(): Value {
|
|
86
|
+
return {
|
|
87
|
+
age: this.#age,
|
|
88
|
+
name: this.#name
|
|
89
|
+
} as Value;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#age;
|
|
93
|
+
#name;
|
|
94
|
+
constructor(value: Value, ...args: any[]) {
|
|
95
|
+
console.log(`Instantiated DataConstructor`, value, ...args);
|
|
96
|
+
this.#age = value.age;
|
|
97
|
+
this.#name = value.name;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
set(value: Value): this { this.validate(value); return this; }
|
|
101
|
+
clear(): this { return this; }
|
|
102
|
+
destroy(): this { return this; }
|
|
103
|
+
lock(): this { return this; };
|
|
104
|
+
validate(value: Value): boolean {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Create `ProfileClass` with customizable data.
|
|
110
|
+
export class ProfileClass<
|
|
111
|
+
Value extends { age: number, name: string },
|
|
112
|
+
DataType extends DataShape<Value>,
|
|
113
|
+
Args extends any[]
|
|
114
|
+
> {
|
|
115
|
+
|
|
116
|
+
public get age(): Value['age'] {
|
|
117
|
+
return this.#data.value.age;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public get name(): Value['name'] {
|
|
121
|
+
return this.#data.value.name;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public get data() {
|
|
125
|
+
return this.#data;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#data: DataType;
|
|
129
|
+
|
|
130
|
+
constructor(value: Value, dataCtor: DataConstructor<Value, DataType>);
|
|
131
|
+
constructor(value: Value, dataCtor: [DataConstructor<Value, DataType>, ...Args]);
|
|
132
|
+
constructor(value: Value, dataCtor: any) {
|
|
133
|
+
// ...implementation
|
|
134
|
+
console.log(`DataConstructor`, value, dataCtor[1]);
|
|
135
|
+
this.#data = Array.isArray(dataCtor)
|
|
136
|
+
? new dataCtor[0](value, ...dataCtor.slice(1))
|
|
137
|
+
: new dataCtor(value);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Initialize.
|
|
142
|
+
// const frankProfile: ProfileClass<object, ProfileData<object>, any[]>
|
|
143
|
+
const frankProfile = new ProfileClass({ age: 37, name: 'Frank' }, ProfileData);
|
|
71
144
|
|
|
72
|
-
|
|
145
|
+
frankProfile.age; // 37
|
|
146
|
+
frankProfile.name; // Frank
|
|
147
|
+
|
|
148
|
+
// Set the data.
|
|
149
|
+
frankProfile.data.set({ age: 37, name: 'Frank' });
|
|
150
|
+
frankProfile.data.clear();
|
|
151
|
+
frankProfile.data.lock();
|
|
152
|
+
frankProfile.data.value;
|
|
73
153
|
```
|
|
74
154
|
|
|
75
155
|
[Source](https://github.com/typedly/data/blob/main/src/lib/interface/data-constructor.interface.ts)
|
|
@@ -93,6 +173,24 @@ class TestData implements DataShape<number> {
|
|
|
93
173
|
|
|
94
174
|
[Source](https://github.com/typedly/data/blob/main/src/lib/interface/data-shape.interface.ts)
|
|
95
175
|
|
|
176
|
+
#### `ValueConstructor`
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { ValueConstructor } from '@typedly/data';
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
[Source](https://github.com/typedly/data/blob/main/src/lib/interface/value-constructor.interface.ts)
|
|
183
|
+
|
|
184
|
+
#### `ValueShape`
|
|
185
|
+
|
|
186
|
+
The shape of a `Value` type.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { ValueShape } from '@typedly/data';
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
[Source](https://github.com/typedly/data/blob/main/src/lib/interface/value-shape.interface.ts)
|
|
193
|
+
|
|
96
194
|
### Type
|
|
97
195
|
|
|
98
196
|
#### `DataConstructorInput`
|
|
@@ -100,23 +198,139 @@ class TestData implements DataShape<number> {
|
|
|
100
198
|
The input type for data constructors, with arguments support.
|
|
101
199
|
|
|
102
200
|
```typescript
|
|
103
|
-
import { DataConstructorInput
|
|
104
|
-
|
|
105
|
-
function createData<Value, DataType extends DataShape<Value>>(
|
|
106
|
-
input: DataConstructorInput<Value, DataType>,
|
|
107
|
-
value: Value
|
|
108
|
-
): DataType {
|
|
109
|
-
if (Array.isArray(input)) {
|
|
110
|
-
const [Ctor, ...args] = input;
|
|
111
|
-
return new Ctor(value, ...args);
|
|
112
|
-
} else {
|
|
113
|
-
return new input(value);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
201
|
+
import { DataConstructorInput } from '@typedly/data';
|
|
116
202
|
```
|
|
117
203
|
|
|
118
204
|
[Source](https://github.com/typedly/data/blob/main/src/lib/type/data-constructor-input.type.ts)
|
|
119
205
|
|
|
206
|
+
#### `DataConstructorTuple`
|
|
207
|
+
|
|
208
|
+
The input type for data constructors, with arguments support.
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
import { DataConstructorTuple } from '@typedly/data';
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
[Source](https://github.com/typedly/data/blob/main/src/lib/type/data-constructor-tuple.type.ts)
|
|
215
|
+
|
|
216
|
+
### Full example usage
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
import { DataConstructor, DataShape, ValueConstructor, ValueShape } from '@typedly/data';
|
|
220
|
+
|
|
221
|
+
// Import ValueShape and ValueConstructor.
|
|
222
|
+
// Create a profile data value.
|
|
223
|
+
export class ProfileDataValue<
|
|
224
|
+
Type extends { age: number, name: string },
|
|
225
|
+
Args extends any[] = any[]
|
|
226
|
+
> implements ValueShape<Type> {
|
|
227
|
+
get value(): Type {
|
|
228
|
+
return {
|
|
229
|
+
age: this.#age,
|
|
230
|
+
name: this.#name
|
|
231
|
+
} as Type;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
#age: Type['age'];
|
|
235
|
+
#name: Type['name'];
|
|
236
|
+
|
|
237
|
+
constructor(value: Type, ...args: Args) {
|
|
238
|
+
console.log(`Instantiated ValueConstructor`, value, ...args);
|
|
239
|
+
this.#age = value.age;
|
|
240
|
+
this.#name = value.name;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
set(value: Type): this { return this; }
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export class ProfileDataOfValue<
|
|
247
|
+
Value extends { age: number, name: string },
|
|
248
|
+
Args extends any[] = any[],
|
|
249
|
+
ValueInstance extends ValueShape<Value> = ProfileDataValue<Value, Args>,
|
|
250
|
+
> implements DataShape<Value> {
|
|
251
|
+
|
|
252
|
+
get value(): Value {
|
|
253
|
+
return {
|
|
254
|
+
} as Value;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
get valueInstance(): ValueInstance {
|
|
258
|
+
return this.#value;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
#value;
|
|
262
|
+
constructor(
|
|
263
|
+
value: Value,
|
|
264
|
+
valueCtor: ValueConstructor<Value, ValueInstance, Args> = ProfileDataValue<Value, Args> as any,
|
|
265
|
+
...args: Args
|
|
266
|
+
) {
|
|
267
|
+
console.log(`Instantiated DataConstructor`, value, ...args);
|
|
268
|
+
this.#value = new valueCtor(value, ...args);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
set(value: Value): this { this.validate(value); return this; }
|
|
272
|
+
clear(): this { return this; }
|
|
273
|
+
destroy(): this { return this; }
|
|
274
|
+
lock(): this { return this; };
|
|
275
|
+
validate(value: Value): boolean {
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// const profileDataOfValue: ProfileDataOfValue<{
|
|
281
|
+
// age: number;
|
|
282
|
+
// name: string;
|
|
283
|
+
// }, ProfileDataValue<{
|
|
284
|
+
// age: number;
|
|
285
|
+
// name: string;
|
|
286
|
+
// }, []>, []>
|
|
287
|
+
const profileDataOfValue = new ProfileDataOfValue({
|
|
288
|
+
age: 37,
|
|
289
|
+
name: 'Mark'
|
|
290
|
+
}, ProfileDataValue);
|
|
291
|
+
|
|
292
|
+
const dataSymbol = Symbol('data');
|
|
293
|
+
|
|
294
|
+
// Create `ProfileClass` with customizable data.
|
|
295
|
+
export class ProfileClass<
|
|
296
|
+
Value extends { age: number, name: string },
|
|
297
|
+
DataType extends DataShape<Value>,
|
|
298
|
+
Args extends any[]
|
|
299
|
+
> {
|
|
300
|
+
|
|
301
|
+
public get age(): Value['age'] {
|
|
302
|
+
return this.#data.value.age;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
public get name(): Value['name'] {
|
|
306
|
+
return this.#data.value.name;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
public get [dataSymbol]() {
|
|
310
|
+
return this.#data;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
#data: DataType;
|
|
314
|
+
|
|
315
|
+
constructor(value: Value, dataCtor: DataConstructor<Value, DataType, Args>);
|
|
316
|
+
constructor(value: Value, dataCtor: [DataConstructor<Value, DataType, Args>, ...Args]);
|
|
317
|
+
constructor(value: Value, dataCtor: any) {
|
|
318
|
+
// ...implementation
|
|
319
|
+
console.log(`DataConstructor`, value, dataCtor[1]);
|
|
320
|
+
this.#data = Array.isArray(dataCtor)
|
|
321
|
+
? new dataCtor[0](value, ...dataCtor.slice(1))
|
|
322
|
+
: new dataCtor(value);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const markProfile = new ProfileClass({ age: 37, name: 'Mark' }, ProfileDataOfValue);
|
|
327
|
+
|
|
328
|
+
markProfile.age // 37
|
|
329
|
+
markProfile.name // Mark
|
|
330
|
+
markProfile[dataSymbol].value // { age, name }
|
|
331
|
+
markProfile[dataSymbol].valueInstance.set({ age: 27, name: 'Frank' });
|
|
332
|
+
```
|
|
333
|
+
|
|
120
334
|
## Contributing
|
|
121
335
|
|
|
122
336
|
Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
|
|
@@ -209,7 +423,7 @@ MIT © typedly ([license][typedly-license])
|
|
|
209
423
|
|
|
210
424
|
<!-- Package: typedly -->
|
|
211
425
|
<!-- npm -->
|
|
212
|
-
[typedly-npm-badge-svg]: https://badge.fury.io/
|
|
426
|
+
[typedly-npm-badge-svg]: https://badge.fury.io/gh/typedly%2Fdata.svg
|
|
213
427
|
[typedly-npm-badge]: https://badge.fury.io/js/@typedly%2Fdata
|
|
214
428
|
|
|
215
429
|
<!-- GIT -->
|
package/index.d.ts
CHANGED
|
@@ -26,12 +26,45 @@ interface DataShape<Value> {
|
|
|
26
26
|
interface DataConstructor<Value, Instance extends DataShape<Value>, Args extends readonly any[] = any[]> extends DataConstructor$1<Value, DataShape<Value>, Instance, [...Args]> {
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* @description The value shape.
|
|
31
|
+
* @export
|
|
32
|
+
* @interface ValueShape
|
|
33
|
+
* @template Type The type of value.
|
|
34
|
+
*/
|
|
35
|
+
interface ValueShape<Type> {
|
|
36
|
+
get value(): Type;
|
|
37
|
+
set(value: Type): this;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @description The constructor for value in data for advanced customization.
|
|
42
|
+
* @export
|
|
43
|
+
* @interface ValueConstructor
|
|
44
|
+
* @template Value The value type.
|
|
45
|
+
* @template {ValueShape<Value>} Instance The instance.
|
|
46
|
+
* @template {readonly any[]} [Args=any[]] Additional arguments passed to the constructor.
|
|
47
|
+
* @extends {BaseDataConstructor<Value, ValueShape<Value>, Instance, [...Args]>}
|
|
48
|
+
*/
|
|
49
|
+
interface ValueConstructor<Value, Instance extends ValueShape<Value>, Args extends readonly any[] = any[]> extends DataConstructor$1<Value, ValueShape<Value>, Instance, [...Args]> {
|
|
50
|
+
}
|
|
51
|
+
|
|
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
|
+
|
|
29
61
|
/**
|
|
30
62
|
* @description The input type for data constructors, with arguments support.
|
|
31
63
|
* @export
|
|
32
64
|
* @template Value The value type.
|
|
33
65
|
* @template {DataShape<Value>} Instance The data instance type.
|
|
66
|
+
* @template {any[]} [Args=any[]] The arguments to pass to instance.
|
|
34
67
|
*/
|
|
35
|
-
type DataConstructorInput<Value, Instance extends DataShape<Value
|
|
68
|
+
type DataConstructorInput<Value, Instance extends DataShape<Value>, Args extends any[] = any[]> = DataConstructor<Value, Instance, Args> | DataConstructorTuple<Value, Instance, Args>;
|
|
36
69
|
|
|
37
|
-
export type { DataConstructor, DataConstructorInput, DataShape };
|
|
70
|
+
export type { DataConstructor, DataConstructorInput, DataConstructorTuple, DataShape, ValueConstructor, ValueShape };
|