@typedly/data 1.0.0 → 3.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
@@ -16,15 +16,27 @@
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)
32
+ - [`ValueConstructor`](#valueconstructor)
33
+ - [`ValueShape`](#valueshape)
26
34
  - [Type](#type)
35
+ - [`AsyncReturn`](#asyncreturn)
27
36
  - [`DataConstructorInput`](#dataconstructorinput)
37
+ - [`DataConstructorTuple`](#dataconstructortuple)
38
+ - [`IterValue`](#itervalue)
39
+ - [Full example usage](#full-example-usage)
28
40
  - [Contributing](#contributing)
29
41
  - [Support](#support)
30
42
  - [Code of Conduct](#code-of-conduct)
@@ -53,23 +65,112 @@ npm install @typedly/data --save-peer
53
65
  ```typescript
54
66
  import {
55
67
  // Interface.
68
+ DataAdapter,
56
69
  DataConstructor,
57
70
  DataShape,
71
+ ValueConstructor,
72
+ ValueShape,
58
73
  // Type.
59
- DataConstructorInput
74
+ AsyncReturn,
75
+ DataConstructorInput,
76
+ DataConstructorTuple,
77
+ IterValue
60
78
  } from '@typedly/data';
61
79
  ```
62
80
 
63
81
  ### Interface
64
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
+
65
93
  #### `DataConstructor`
66
94
 
67
95
  The constructor interface for data types.
68
96
 
69
97
  ```typescript
70
- import { DataConstructor } from '@typedly/data';
98
+ import { DataConstructor, DataShape } from '@typedly/data';
99
+
100
+ // Import DataShape and DataConstructor.
101
+ // Create a data class that implements `DataShape` of `Type`.
102
+ export class ProfileData<
103
+ Value extends { age: number, name: string }
104
+ > implements DataShape<Value> {
105
+
106
+ get value(): Value {
107
+ return {
108
+ age: this.#age,
109
+ name: this.#name
110
+ } as Value;
111
+ }
112
+
113
+ #age;
114
+ #name;
115
+ constructor(value: Value, ...args: any[]) {
116
+ console.log(`Instantiated DataConstructor`, value, ...args);
117
+ this.#age = value.age;
118
+ this.#name = value.name;
119
+ }
120
+
121
+ set(value: Value): this { this.validate(value); return this; }
122
+ clear(): this { return this; }
123
+ destroy(): this { return this; }
124
+ lock(): this { return this; };
125
+ validate(value: Value): boolean {
126
+ return true;
127
+ }
128
+ }
129
+
130
+ // Create `ProfileClass` with customizable data.
131
+ export class ProfileClass<
132
+ Value extends { age: number, name: string },
133
+ DataType extends DataShape<Value>,
134
+ Args extends any[]
135
+ > {
136
+
137
+ public get age(): Value['age'] {
138
+ return this.#data.value.age;
139
+ }
140
+
141
+ public get name(): Value['name'] {
142
+ return this.#data.value.name;
143
+ }
144
+
145
+ public get data() {
146
+ return this.#data;
147
+ }
148
+
149
+ #data: DataType;
150
+
151
+ constructor(value: Value, dataCtor: DataConstructor<Value, DataType>);
152
+ constructor(value: Value, dataCtor: [DataConstructor<Value, DataType>, ...Args]);
153
+ constructor(value: Value, dataCtor: any) {
154
+ // ...implementation
155
+ console.log(`DataConstructor`, value, dataCtor[1]);
156
+ this.#data = Array.isArray(dataCtor)
157
+ ? new dataCtor[0](value, ...dataCtor.slice(1))
158
+ : new dataCtor(value);
159
+ }
160
+ }
161
+
162
+ // Initialize.
163
+ // const frankProfile: ProfileClass<object, ProfileData<object>, any[]>
164
+ const frankProfile = new ProfileClass({ age: 37, name: 'Frank' }, ProfileData);
71
165
 
72
- export const ctor: DataConstructor<number, DataShape<number>, any[]> = TestData;
166
+ frankProfile.age; // 37
167
+ frankProfile.name; // Frank
168
+
169
+ // Set the data.
170
+ frankProfile.data.set({ age: 37, name: 'Frank' });
171
+ frankProfile.data.clear();
172
+ frankProfile.data.lock();
173
+ frankProfile.data.value;
73
174
  ```
74
175
 
75
176
  [Source](https://github.com/typedly/data/blob/main/src/lib/interface/data-constructor.interface.ts)
@@ -93,30 +194,184 @@ class TestData implements DataShape<number> {
93
194
 
94
195
  [Source](https://github.com/typedly/data/blob/main/src/lib/interface/data-shape.interface.ts)
95
196
 
197
+ #### `ValueConstructor`
198
+
199
+ ```typescript
200
+ import { ValueConstructor } from '@typedly/data';
201
+ ```
202
+
203
+ [Source](https://github.com/typedly/data/blob/main/src/lib/interface/value-constructor.interface.ts)
204
+
205
+ #### `ValueShape`
206
+
207
+ The shape of a `Value` type.
208
+
209
+ ```typescript
210
+ import { ValueShape } from '@typedly/data';
211
+ ```
212
+
213
+ [Source](https://github.com/typedly/data/blob/main/src/lib/interface/value-shape.interface.ts)
214
+
96
215
  ### Type
97
216
 
217
+ #### `AsyncReturn`
218
+
219
+ The conditional return type for async methods.
220
+
221
+ ```typescript
222
+ import { AsyncReturn } from '@typedly/data';
223
+ ```
224
+
225
+ [Source](https://github.com/typedly/data/blob/main/src/lib/type/async-return.type.ts)
226
+
98
227
  #### `DataConstructorInput`
99
228
 
100
229
  The input type for data constructors, with arguments support.
101
230
 
102
231
  ```typescript
103
- import { DataConstructorInput, DataShape } from '@typedly/data';
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
- }
232
+ import { DataConstructorInput } from '@typedly/data';
116
233
  ```
117
234
 
118
235
  [Source](https://github.com/typedly/data/blob/main/src/lib/type/data-constructor-input.type.ts)
119
236
 
237
+ #### `DataConstructorTuple`
238
+
239
+ The input type for data constructors, with arguments support.
240
+
241
+ ```typescript
242
+ import { DataConstructorTuple } from '@typedly/data';
243
+ ```
244
+
245
+ [Source](https://github.com/typedly/data/blob/main/src/lib/type/data-constructor-tuple.type.ts)
246
+
247
+ #### `IterValue`
248
+
249
+ The iterated value type.
250
+
251
+ ```typescript
252
+ import { IterValue } from '@typedly/data';
253
+ ```
254
+
255
+ [Source](https://github.com/typedly/data/blob/main/src/lib/type/iter-value.type.ts)
256
+
257
+ ### Full example usage
258
+
259
+ ```typescript
260
+ import { DataConstructor, DataShape, ValueConstructor, ValueShape } from '@typedly/data';
261
+
262
+ // Import ValueShape and ValueConstructor.
263
+ // Create a profile data value.
264
+ export class ProfileDataValue<
265
+ Type extends { age: number, name: string },
266
+ Args extends any[] = any[]
267
+ > implements ValueShape<Type> {
268
+ get value(): Type {
269
+ return {
270
+ age: this.#age,
271
+ name: this.#name
272
+ } as Type;
273
+ }
274
+
275
+ #age: Type['age'];
276
+ #name: Type['name'];
277
+
278
+ constructor(value: Type, ...args: Args) {
279
+ console.log(`Instantiated ValueConstructor`, value, ...args);
280
+ this.#age = value.age;
281
+ this.#name = value.name;
282
+ }
283
+
284
+ set(value: Type): this { return this; }
285
+ }
286
+
287
+ export class ProfileDataOfValue<
288
+ Value extends { age: number, name: string },
289
+ Args extends any[] = any[],
290
+ ValueInstance extends ValueShape<Value> = ProfileDataValue<Value, Args>,
291
+ > implements DataShape<Value> {
292
+
293
+ get value(): Value {
294
+ return {
295
+ } as Value;
296
+ }
297
+
298
+ get valueInstance(): ValueInstance {
299
+ return this.#value;
300
+ }
301
+
302
+ #value;
303
+ constructor(
304
+ value: Value,
305
+ valueCtor: ValueConstructor<Value, ValueInstance, Args> = ProfileDataValue<Value, Args> as any,
306
+ ...args: Args
307
+ ) {
308
+ console.log(`Instantiated DataConstructor`, value, ...args);
309
+ this.#value = new valueCtor(value, ...args);
310
+ }
311
+
312
+ set(value: Value): this { this.validate(value); return this; }
313
+ clear(): this { return this; }
314
+ destroy(): this { return this; }
315
+ lock(): this { return this; };
316
+ validate(value: Value): boolean {
317
+ return true;
318
+ }
319
+ }
320
+
321
+ // const profileDataOfValue: ProfileDataOfValue<{
322
+ // age: number;
323
+ // name: string;
324
+ // }, ProfileDataValue<{
325
+ // age: number;
326
+ // name: string;
327
+ // }, []>, []>
328
+ const profileDataOfValue = new ProfileDataOfValue({
329
+ age: 37,
330
+ name: 'Mark'
331
+ }, ProfileDataValue);
332
+
333
+ const dataSymbol = Symbol('data');
334
+
335
+ // Create `ProfileClass` with customizable data.
336
+ export class ProfileClass<
337
+ Value extends { age: number, name: string },
338
+ DataType extends DataShape<Value>,
339
+ Args extends any[]
340
+ > {
341
+
342
+ public get age(): Value['age'] {
343
+ return this.#data.value.age;
344
+ }
345
+
346
+ public get name(): Value['name'] {
347
+ return this.#data.value.name;
348
+ }
349
+
350
+ public get [dataSymbol]() {
351
+ return this.#data;
352
+ }
353
+
354
+ #data: DataType;
355
+
356
+ constructor(value: Value, dataCtor: DataConstructor<Value, DataType, Args>);
357
+ constructor(value: Value, dataCtor: [DataConstructor<Value, DataType, Args>, ...Args]);
358
+ constructor(value: Value, dataCtor: any) {
359
+ // ...implementation
360
+ console.log(`DataConstructor`, value, dataCtor[1]);
361
+ this.#data = Array.isArray(dataCtor)
362
+ ? new dataCtor[0](value, ...dataCtor.slice(1))
363
+ : new dataCtor(value);
364
+ }
365
+ }
366
+
367
+ const markProfile = new ProfileClass({ age: 37, name: 'Mark' }, ProfileDataOfValue);
368
+
369
+ markProfile.age // 37
370
+ markProfile.name // Mark
371
+ markProfile[dataSymbol].value // { age, name }
372
+ markProfile[dataSymbol].valueInstance.set({ age: 27, name: 'Frank' });
373
+ ```
374
+
120
375
  ## Contributing
121
376
 
122
377
  Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
@@ -209,7 +464,7 @@ MIT © typedly ([license][typedly-license])
209
464
 
210
465
  <!-- Package: typedly -->
211
466
  <!-- npm -->
212
- [typedly-npm-badge-svg]: https://badge.fury.io/js/@typedly%2Fdata.svg
467
+ [typedly-npm-badge-svg]: https://badge.fury.io/gh/typedly%2Fdata.svg
213
468
  [typedly-npm-badge]: https://badge.fury.io/js/@typedly%2Fdata
214
469
 
215
470
  <!-- GIT -->
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typedly/data",
3
- "version": "1.0.0",
3
+ "version": "3.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": "index.d.ts",
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": "./index.d.ts",
42
+ "types": "./types/typedly-data.d.ts",
43
43
  "default": "./fesm2022/typedly-data.mjs"
44
44
  }
45
45
  }
@@ -0,0 +1,101 @@
1
+ import { DataConstructor as DataConstructor$1 } from '@typedly/constructor';
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
+
36
+ /**
37
+ * @description The shape of a `Data` type.
38
+ * @export
39
+ * @interface DataShape
40
+ * @template T The value type.
41
+ * @template {boolean} [Async=false] The `Promise` return type for methods.
42
+ */
43
+ interface DataShape<T, Async extends boolean = false> {
44
+ value: T;
45
+ clear(): AsyncReturn<Async, this>;
46
+ destroy(): AsyncReturn<Async, this>;
47
+ lock(): this;
48
+ set(value: T): AsyncReturn<Async, this>;
49
+ [Symbol.toStringTag]?: string;
50
+ [Symbol.iterator]?(): IterableIterator<IterValue<T>>;
51
+ [Symbol.asyncIterator]?(): AsyncIterableIterator<IterValue<T>>;
52
+ }
53
+
54
+ /**
55
+ * @description The adapter interface for data types.
56
+ * @export
57
+ * @interface DataAdapter
58
+ * @template T The type of the data value.
59
+ * @template {boolean} [R=false] The type of the return values for methods.
60
+ * @extends {DataShape<T, R>}
61
+ */
62
+ interface DataAdapter<T, R extends boolean = false> extends DataShape<T, R> {
63
+ version?: string;
64
+ }
65
+
66
+ /**
67
+ * @description The constructor interface for data types.
68
+ * @export
69
+ * @interface DataConstructor
70
+ * @template Value
71
+ * @template {DataShape<Value>} Instance The instance.
72
+ * @template {readonly any[]} [Args=any[]]
73
+ * @extends {BaseDataConstructor<Value, DataShape<Value>, Instance, [...Args]>}
74
+ */
75
+ interface DataConstructor<Value, Instance extends DataShape<Value>, Args extends readonly any[] = any[]> extends DataConstructor$1<Value, DataShape<Value>, Instance, [...Args]> {
76
+ }
77
+
78
+ /**
79
+ * @description The value shape.
80
+ * @export
81
+ * @interface ValueShape
82
+ * @template Type The type of value.
83
+ */
84
+ interface ValueShape<Type> {
85
+ get value(): Type;
86
+ set(value: Type): this;
87
+ }
88
+
89
+ /**
90
+ * @description The constructor for value in data for advanced customization.
91
+ * @export
92
+ * @interface ValueConstructor
93
+ * @template Value The value type.
94
+ * @template {ValueShape<Value>} Instance The instance.
95
+ * @template {readonly any[]} [Args=any[]] Additional arguments passed to the constructor.
96
+ * @extends {BaseDataConstructor<Value, ValueShape<Value>, Instance, [...Args]>}
97
+ */
98
+ interface ValueConstructor<Value, Instance extends ValueShape<Value>, Args extends readonly any[] = any[]> extends DataConstructor$1<Value, ValueShape<Value>, Instance, [...Args]> {
99
+ }
100
+
101
+ export type { AsyncReturn, DataAdapter, DataConstructor, DataConstructorInput, DataConstructorTuple, DataShape, IterValue, ValueConstructor, ValueShape };
package/index.d.ts DELETED
@@ -1,37 +0,0 @@
1
- import { DataConstructor as DataConstructor$1 } from '@typedly/constructor';
2
-
3
- /**
4
- * @description The shape of a `Data` type.
5
- * @export
6
- * @interface DataShape
7
- * @template Value The value type.
8
- */
9
- interface DataShape<Value> {
10
- get value(): Value;
11
- clear(): this;
12
- destroy(): this;
13
- lock(): this;
14
- set(value: Value): this;
15
- }
16
-
17
- /**
18
- * @description The constructor interface for data types.
19
- * @export
20
- * @interface DataConstructor
21
- * @template Value
22
- * @template {DataShape<Value>} Instance The instance.
23
- * @template {readonly any[]} [Args=any[]]
24
- * @extends {BaseDataConstructor<Value, DataShape<Value>, Instance, [...Args]>}
25
- */
26
- interface DataConstructor<Value, Instance extends DataShape<Value>, Args extends readonly any[] = any[]> extends DataConstructor$1<Value, DataShape<Value>, Instance, [...Args]> {
27
- }
28
-
29
- /**
30
- * @description The input type for data constructors, with arguments support.
31
- * @export
32
- * @template Value The value type.
33
- * @template {DataShape<Value>} Instance The data instance type.
34
- */
35
- type DataConstructorInput<Value, Instance extends DataShape<Value>> = [DataConstructor<Value, Instance>, ...any[]] | DataConstructor<Value, Instance>;
36
-
37
- export type { DataConstructor, DataConstructorInput, DataShape };