Skip to content

Commit 17d2ad5

Browse files
Merge pull request #3 from typescript-package/develop
v2.0.0
2 parents 616e4c9 + c0aaa8b commit 17d2ad5

15 files changed

+419
-80
lines changed

README.md

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ A lightweight **TypeScript** library for basic data management.
1919

2020
- [Installation](#installation)
2121
- [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)
2831
- [Immutability](#immutability)
2932
- [Sealed](#sealed)
3033
- [Frozen](#frozen)
@@ -52,13 +55,23 @@ import {
5255
Immutability,
5356

5457
// Class.
58+
// Base.
5559
Data,
60+
Value,
61+
62+
// `WeakData`.
5663
NamedWeakData,
64+
// Indexed.
65+
IndexedWeakData,
66+
// Basic.
5767
WeakData,
58-
Value
5968
} from '@typescript-package/data';
6069
```
6170

71+
### `DataCore`
72+
73+
The base abstraction with immutability for handling data-related classes.
74+
6275
### `Data`
6376

6477
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();
87100
console.log(data.value); // Throws error or undefined (based on how it's handled)
88101
```
89102

90-
### `DataCore`
103+
### `Value`
91104

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+
```
93110

94111
### `Immutability`
95112

96113
Manages the immutability states of `this` current instance.
97114

98-
### `NamedWeakData`
99-
100115
```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+
```
109118

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
113120

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`
117122

118-
// Setting new values
119-
ageData.set(30);
120-
console.log(ageData.value); // Outputs: 30
123+
```typescript
124+
import { IndexedWeakData } from '@typescript-package/data';
121125

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+
}
125132

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');
129138

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}
130142
```
131143

132144
### `WeakData`
@@ -137,30 +149,26 @@ The `WeakData` class is a concrete class that stores data in a static `WeakMap`.
137149
import { WeakData } from '@typescript-package/data';
138150

139151
// Example subclass of WeakData
140-
class StringWeakData extends WeakData<string> {
152+
export class StringWeakData extends WeakData<string> {
141153
constructor(value: string) {
142154
super(value);
143155
}
144156
}
145157

146158
// Create a new instance of StringWeakData
147-
const data1 = new StringWeakData("Hello, world!");
159+
export const data = new StringWeakData("Hello, world!");
148160

149161
// Access the current value
150-
console.log(data1.value); // Output: Hello, world!
162+
console.log(data.value); // Output: Hello, world!
151163

152164
// 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
155167

156168
// Destroy the value
157-
data1.destroy();
169+
data.destroy();
158170
```
159171

160-
### `Value`
161-
162-
The class to manage the value of generic type variable `Type`.
163-
164172
## Immutability
165173

166174
### Sealed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typescript-package/data",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"author": "wwwdev.io <dev@wwwdev.io>",
55
"description": "A lightweight TypeScript library for basic data management.",
66
"license": "MIT",
@@ -27,7 +27,7 @@
2727
"@typescript-package/data",
2828
"Data",
2929
"Immutability",
30-
"NamedWeakData",
30+
"IndexedWeakData",
3131
"Value",
3232
"WeakData"
3333
],

src/lib/data-core.abstract.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ export abstract class DataCore<Type> extends Immutability {
2828
*/
2929
public abstract get value(): Type;
3030

31+
/**
32+
* @description Clears the value by setting to `undefined` or `null`.
33+
* @public
34+
* @abstract
35+
* @returns {this} Returns `this` current instance.
36+
*/
37+
public abstract clear(): this;
38+
3139
/**
3240
* @description Abstract method to clear or remove the stored data value.
3341
* @public
3442
* @abstract
35-
* @returns {this} Returns current instance.
43+
* @returns {this} Returns `this` current instance.
3644
*/
3745
public abstract destroy(): this;
3846

@@ -43,7 +51,7 @@ export abstract class DataCore<Type> extends Immutability {
4351
*/
4452
public override lock(): this {
4553
Immutability.deepFreeze(this.value);
46-
super.lock();
54+
super.lock();
4755
return this;
4856
}
4957

@@ -52,7 +60,7 @@ export abstract class DataCore<Type> extends Immutability {
5260
* @public
5361
* @abstract
5462
* @param {Type} value The data of `Type` to set.
55-
* @returns {this} Returns current instance.
63+
* @returns {this} Returns `this` current instance.
5664
*/
5765
public abstract set(value: Type): this;
5866
}

src/lib/data.class.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Data<Type> extends DataCore<Type> {
3737
#value;
3838

3939
/**
40-
* Creates an instance of `Data` child class.
40+
* Creates an instance of `Data`.
4141
* @constructor
4242
* @param {Type} value Initial data value of generic type variable `Type`.
4343
*/
@@ -46,10 +46,20 @@ export class Data<Type> extends DataCore<Type> {
4646
this.#value = new Value(value);
4747
}
4848

49+
/**
50+
* @description Clears the value to `null`.
51+
* @public
52+
* @returns {this} Returns `this` current instance.
53+
*/
54+
public clear(): this {
55+
this.#value.set(null as unknown as Type);
56+
return this;
57+
}
58+
4959
/**
5060
* @description Destroys the `Value` object by setting it to `null`.
5161
* @public
52-
* @returns {this} Returns the current instance.
62+
* @returns {this} Returns `this` current instance.
5363
*/
5464
public destroy(): this {
5565
this.#value = null as any;
@@ -60,7 +70,7 @@ export class Data<Type> extends DataCore<Type> {
6070
* @description Sets the data value.
6171
* @public
6272
* @param {Type} value The data of `Type` to set.
63-
* @returns {this} Returns the current instance.
73+
* @returns {this} Returns `this` current instance.
6474
*/
6575
public set(value: Type) {
6676
super.validate();

src/lib/immutability.abstract.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,46 @@ export abstract class Immutability {
1919
return object;
2020
}
2121

22+
/**
23+
* @description
24+
* @public
25+
* @readonly
26+
* @type {boolean}
27+
*/
28+
public get frozen() {
29+
return this.isFrozen();
30+
}
31+
32+
/**
33+
* @description
34+
* @public
35+
* @readonly
36+
* @type {boolean}
37+
*/
38+
public get locked() {
39+
return this.#locked;
40+
}
41+
42+
/**
43+
* @description
44+
* @public
45+
* @readonly
46+
* @type {boolean}
47+
*/
48+
public get mutable() {
49+
return this.isMutable();
50+
}
51+
52+
/**
53+
* @description
54+
* @public
55+
* @readonly
56+
* @type {boolean}
57+
*/
58+
public get sealed() {
59+
return this.isSealed();
60+
}
61+
2262
/**
2363
* @description Privately stored locked state as `true` if locked, otherwise `false`.
2464
* @type {boolean}

src/lib/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ export { DataCore } from './data-core.abstract';
33
export { Immutability } from './immutability.abstract';
44
// Class.
55
export { Data } from './data.class';
6+
// `WeakData`.
7+
export { IndexedWeakData } from './indexed-weak-data.class';
68
export { NamedWeakData } from './named-weak-data.class';
9+
export { WeakData } from './weak-data.class';
10+
// `Value`.
711
export { Value } from './value.class';
8-
export { WeakData } from './weak-data.class';

0 commit comments

Comments
 (0)