@@ -19,12 +19,15 @@ A lightweight **TypeScript** library for basic data management.
19
19
20
20
- [ Installation] ( #installation )
21
21
- [ Api] ( #api )
22
- - [ ` Data ` ] ( #data )
23
- - [ ` DataCore ` ] ( #datacore )
24
- - [ ` Immutability ` ] ( #immutability )
25
- - [ ` NamedWeakData ` ] ( #namedweakdata )
26
- - [ ` Value ` ] ( #value )
27
- - [ ` WeakData ` ] ( #weakdata )
22
+ - ` abstract `
23
+ - [ ` Immutability ` ] ( #immutability )
24
+ - [ ` DataCore ` ] ( #datacore )
25
+ - ** Base**
26
+ - [ ` Data ` ] ( #data )
27
+ - [ ` Value ` ] ( #value )
28
+ - ** ` WeakData ` **
29
+ - [ ` IndexedWeakData ` ] ( #indexedweakdata )
30
+ - [ ` WeakData ` ] ( #weakdata )
28
31
- [ Immutability] ( #immutability )
29
32
- [ Sealed] ( #sealed )
30
33
- [ Frozen] ( #frozen )
@@ -52,13 +55,23 @@ import {
52
55
Immutability ,
53
56
54
57
// Class.
58
+ // Base.
55
59
Data ,
60
+ Value ,
61
+
62
+ // `WeakData`.
56
63
NamedWeakData ,
64
+ // Indexed.
65
+ IndexedWeakData ,
66
+ // Basic.
57
67
WeakData ,
58
- Value
59
68
} from ' @typescript-package/data' ;
60
69
```
61
70
71
+ ### ` DataCore `
72
+
73
+ The base abstraction with immutability for handling data-related classes.
74
+
62
75
### ` Data `
63
76
64
77
The ` Data ` class is a concrete class that wraps a value and provides methods for setting, retrieving, and destroying the value.
@@ -87,46 +100,45 @@ data.destroy();
87
100
console .log (data .value ); // Throws error or undefined (based on how it's handled)
88
101
```
89
102
90
- ### ` DataCore `
103
+ ### ` Value `
91
104
92
- The base abstraction with immutability for handling data-related classes.
105
+ The class to manage the value of generic type variable ` Type ` .
106
+
107
+ ``` typescript
108
+ import { Value } from ' @typescript-package/data' ;
109
+ ```
93
110
94
111
### ` Immutability `
95
112
96
113
Manages the immutability states of ` this ` current instance.
97
114
98
- ### ` NamedWeakData `
99
-
100
115
``` typescript
101
- import { NamedWeakData } from ' @typescript-package/data' ;
102
-
103
- // Define a class that extends NamedWeakData
104
- export class ProfileData extends NamedWeakData <number , ' age' | ' score' > {}
105
-
106
- // Create two instances with different names
107
- const ageData = new ProfileData (25 , ' age' );
108
- const scoreData = new ProfileData (90 , ' score' );
116
+ import { Immutability } from ' @typescript-package/data' ;
117
+ ```
109
118
110
- // Access the values stored in each instance using their respective names
111
- console .log (ageData .value ); // Outputs: 25
112
- console .log (scoreData .value ); // Outputs: 90
119
+ ### WeakData
113
120
114
- // You can also retrieve the data from another instance using the static method `getFrom`
115
- console .log (NamedWeakData .getFrom (ageData , ' age' )); // Outputs: 25
116
- console .log (NamedWeakData .getFrom (scoreData , ' score' )); // Outputs: 90
121
+ ### ` IndexedWeakData `
117
122
118
- // Setting new values
119
- ageData .set (30 );
120
- console .log (ageData .value ); // Outputs: 30
123
+ ``` typescript
124
+ import { IndexedWeakData } from ' @typescript-package/data' ;
121
125
122
- // Destroy an instance and clear its stored data
123
- ageData .destroy ();
124
- console .log (NamedWeakData .getFrom (ageData , ' age' )); // Outputs: undefined
126
+ // Create an interface.
127
+ export interface Profile {
128
+ id: number ,
129
+ age: number ;
130
+ score: number ;
131
+ }
125
132
126
- // Clear all stored values from the map
127
- scoreData .clear ();
128
- console .log (NamedWeakData .getFrom (scoreData , ' score' )); // Outputs: undefined
133
+ // Initialize multiple instances of `IndexedWeakData`.
134
+ export const profileData1 = new IndexedWeakData ({ id: 1 , age: 27 , score: 1100 } as Profile , ' id' );
135
+ export const profileData2 = new IndexedWeakData ({ id: 2 , age: 127 , score: 1200 } as Profile , ' id' );
136
+ export const profileData3 = new IndexedWeakData ({ id: 3 , age: 227 , score: 1300 } as Profile , ' id' );
137
+ export const profileData4 = new IndexedWeakData ({ id: 4 , age: 327 , score: 1400 } as Profile , ' id' );
129
138
139
+ // Get the value by using index.
140
+ console .log (` profileData1: ` , profileData1 .getByIndex (1 )); // Output: {id: 1, age: 27, score: 1100}
141
+ console .log (` profileData3: ` , profileData3 .getByIndex (3 )); // Output: {id: 3, age: 227, score: 1300}
130
142
```
131
143
132
144
### ` WeakData `
@@ -137,30 +149,26 @@ The `WeakData` class is a concrete class that stores data in a static `WeakMap`.
137
149
import { WeakData } from ' @typescript-package/data' ;
138
150
139
151
// Example subclass of WeakData
140
- class StringWeakData extends WeakData <string > {
152
+ export class StringWeakData extends WeakData <string > {
141
153
constructor (value : string ) {
142
154
super (value );
143
155
}
144
156
}
145
157
146
158
// Create a new instance of StringWeakData
147
- const data1 = new StringWeakData (" Hello, world!" );
159
+ export const data = new StringWeakData (" Hello, world!" );
148
160
149
161
// Access the current value
150
- console .log (data1 .value ); // Output: Hello, world!
162
+ console .log (data .value ); // Output: Hello, world!
151
163
152
164
// Update the value
153
- data1 .set (" New value" );
154
- console .log (data1 .value ); // Output: New value
165
+ data .set (" New value" );
166
+ console .log (data .value ); // Output: New value
155
167
156
168
// Destroy the value
157
- data1 .destroy ();
169
+ data .destroy ();
158
170
```
159
171
160
- ### ` Value `
161
-
162
- The class to manage the value of generic type variable ` Type ` .
163
-
164
172
## Immutability
165
173
166
174
### Sealed
0 commit comments