Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6328bde
feat: add `IndexedWeakData` and `IndexedNamedWeakData` for objects.
sciborrudnicki Mar 25, 2025
fc3f920
test: update.
sciborrudnicki Mar 25, 2025
b5aa806
chore(api): update,
sciborrudnicki Mar 25, 2025
b301329
chore(package): add keywords.
sciborrudnicki Mar 25, 2025
37f3e37
feat(map, set): add the `DataMap`, `WeakDataMap` and `DataSet`.
sciborrudnicki Mar 27, 2025
ea28ebe
feat: add the` map`, `object` and `set` type of `WeakData`.
sciborrudnicki Mar 27, 2025
a983646
feat(WeakDataSet): add the `Set` that uses `WeakData` store mechanism.
sciborrudnicki Mar 27, 2025
f5e69e7
refactor(DataCore): add the `abstract` clear method.
sciborrudnicki Mar 27, 2025
3827dc7
feat(ArrayWeakData): add the `WeakData` of type `Array`.
sciborrudnicki Mar 27, 2025
a51b064
refactor(Data): add `clear()` method to set the value to `null`.
sciborrudnicki Mar 27, 2025
e6939e1
refactor(WeakData): add methods `with()` `update()` `has()` `delete()…
sciborrudnicki Mar 27, 2025
483ae34
refactor(`NamedWeakData`): update the `delete()` and `clear()` methods.
sciborrudnicki Mar 27, 2025
deb91ea
refactor(`IndexedWeakData`): throw an error in the constructor if ind…
sciborrudnicki Mar 27, 2025
a423cd5
refactor(`IndexedNamedWeakData`): add `update()` method.
sciborrudnicki Mar 27, 2025
11859f6
chore(api): update.
sciborrudnicki Mar 27, 2025
1aadab4
docs(README.md): update.
sciborrudnicki Mar 27, 2025
3120424
docs(README.md): update.
sciborrudnicki Mar 27, 2025
7afd59e
docs(README.md): update.
sciborrudnicki Mar 27, 2025
52135e2
refactor: remove the `array` `map` `object` and `set` types of `WeakM…
sciborrudnicki Mar 27, 2025
b5ebe54
chore(map): move to `map` directory.
sciborrudnicki Mar 27, 2025
8ad0bc1
chore(set): move to `set` directory.,
sciborrudnicki Mar 27, 2025
51ccbbd
chore(weak): move to `weak` directory.
sciborrudnicki Mar 27, 2025
a3018a5
chore(api): update.
sciborrudnicki Mar 27, 2025
76f953d
feat(array): add.
sciborrudnicki Mar 28, 2025
aa8911c
chore(api): update.
sciborrudnicki Mar 28, 2025
c5ec6be
docs(README.md): update.
sciborrudnicki Mar 28, 2025
a963f77
refactor(IndexedNamedWeakData): removed in favor of storage.
sciborrudnicki Apr 5, 2025
0394325
refactor(IndexedNamedWeakData): deprecated in favor of storage.
sciborrudnicki Apr 5, 2025
92a3ca3
refactor(WeakData): add `has()`,
sciborrudnicki Apr 5, 2025
6b774e4
chore(weak): update.
sciborrudnicki Apr 5, 2025
2371df0
chore: remove for the next version.
sciborrudnicki Apr 6, 2025
6395fa2
test(IndexedNamedWeakData): remove.
sciborrudnicki Apr 6, 2025
f1f9674
refactor(Immutability): add `frozen` `locked` `mutable` `sealed` get…
sciborrudnicki Apr 6, 2025
71bbd8b
chore: bring back weak data classes.
sciborrudnicki Apr 6, 2025
0e05df2
docs(Value): set as `public`.
sciborrudnicki Apr 6, 2025
132ad42
chore(api): update.
sciborrudnicki Apr 6, 2025
f76570b
test(Data): update.
sciborrudnicki Apr 6, 2025
36f6d9d
test: update.
sciborrudnicki Apr 6, 2025
a40022a
chore(package): update.
sciborrudnicki Apr 6, 2025
2dc18ff
docs(README.md): update.
sciborrudnicki Apr 6, 2025
c0aaa8b
2.0.0
sciborrudnicki Apr 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 52 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ A lightweight **TypeScript** library for basic data management.

- [Installation](#installation)
- [Api](#api)
- [`Data`](#data)
- [`DataCore`](#datacore)
- [`Immutability`](#immutability)
- [`NamedWeakData`](#namedweakdata)
- [`Value`](#value)
- [`WeakData`](#weakdata)
- `abstract`
- [`Immutability`](#immutability)
- [`DataCore`](#datacore)
- **Base**
- [`Data`](#data)
- [`Value`](#value)
- **`WeakData`**
- [`IndexedWeakData`](#indexedweakdata)
- [`WeakData`](#weakdata)
- [Immutability](#immutability)
- [Sealed](#sealed)
- [Frozen](#frozen)
Expand Down Expand Up @@ -52,13 +55,23 @@ import {
Immutability,

// Class.
// Base.
Data,
Value,

// `WeakData`.
NamedWeakData,
// Indexed.
IndexedWeakData,
// Basic.
WeakData,
Value
} from '@typescript-package/data';
```

### `DataCore`

The base abstraction with immutability for handling data-related classes.

### `Data`

The `Data` class is a concrete class that wraps a value and provides methods for setting, retrieving, and destroying the value.
Expand Down Expand Up @@ -87,46 +100,45 @@ data.destroy();
console.log(data.value); // Throws error or undefined (based on how it's handled)
```

### `DataCore`
### `Value`

The base abstraction with immutability for handling data-related classes.
The class to manage the value of generic type variable `Type`.

```typescript
import { Value } from '@typescript-package/data';
```

### `Immutability`

Manages the immutability states of `this` current instance.

### `NamedWeakData`

```typescript
import { NamedWeakData } from '@typescript-package/data';

// Define a class that extends NamedWeakData
export class ProfileData extends NamedWeakData<number, 'age' | 'score'> {}

// Create two instances with different names
const ageData = new ProfileData(25, 'age');
const scoreData = new ProfileData(90, 'score');
import { Immutability } from '@typescript-package/data';
```

// Access the values stored in each instance using their respective names
console.log(ageData.value); // Outputs: 25
console.log(scoreData.value); // Outputs: 90
### WeakData

// You can also retrieve the data from another instance using the static method `getFrom`
console.log(NamedWeakData.getFrom(ageData, 'age')); // Outputs: 25
console.log(NamedWeakData.getFrom(scoreData, 'score')); // Outputs: 90
### `IndexedWeakData`

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

// Destroy an instance and clear its stored data
ageData.destroy();
console.log(NamedWeakData.getFrom(ageData, 'age')); // Outputs: undefined
// Create an interface.
export interface Profile {
id: number,
age: number;
score: number;
}

// Clear all stored values from the map
scoreData.clear();
console.log(NamedWeakData.getFrom(scoreData, 'score')); // Outputs: undefined
// Initialize multiple instances of `IndexedWeakData`.
export const profileData1 = new IndexedWeakData({ id: 1, age: 27, score: 1100 } as Profile, 'id');
export const profileData2 = new IndexedWeakData({ id: 2, age: 127, score: 1200 } as Profile, 'id');
export const profileData3 = new IndexedWeakData({ id: 3, age: 227, score: 1300 } as Profile, 'id');
export const profileData4 = new IndexedWeakData({ id: 4, age: 327, score: 1400 } as Profile, 'id');

// Get the value by using index.
console.log(`profileData1: `, profileData1.getByIndex(1)); // Output: {id: 1, age: 27, score: 1100}
console.log(`profileData3: `, profileData3.getByIndex(3)); // Output: {id: 3, age: 227, score: 1300}
```

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

// Example subclass of WeakData
class StringWeakData extends WeakData<string> {
export class StringWeakData extends WeakData<string> {
constructor(value: string) {
super(value);
}
}

// Create a new instance of StringWeakData
const data1 = new StringWeakData("Hello, world!");
export const data = new StringWeakData("Hello, world!");

// Access the current value
console.log(data1.value); // Output: Hello, world!
console.log(data.value); // Output: Hello, world!

// Update the value
data1.set("New value");
console.log(data1.value); // Output: New value
data.set("New value");
console.log(data.value); // Output: New value

// Destroy the value
data1.destroy();
data.destroy();
```

### `Value`

The class to manage the value of generic type variable `Type`.

## Immutability

### Sealed
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typescript-package/data",
"version": "1.0.0",
"version": "2.0.0",
"author": "wwwdev.io <dev@wwwdev.io>",
"description": "A lightweight TypeScript library for basic data management.",
"license": "MIT",
Expand All @@ -27,7 +27,7 @@
"@typescript-package/data",
"Data",
"Immutability",
"NamedWeakData",
"IndexedWeakData",
"Value",
"WeakData"
],
Expand Down
14 changes: 11 additions & 3 deletions src/lib/data-core.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ export abstract class DataCore<Type> extends Immutability {
*/
public abstract get value(): Type;

/**
* @description Clears the value by setting to `undefined` or `null`.
* @public
* @abstract
* @returns {this} Returns `this` current instance.
*/
public abstract clear(): this;

/**
* @description Abstract method to clear or remove the stored data value.
* @public
* @abstract
* @returns {this} Returns current instance.
* @returns {this} Returns `this` current instance.
*/
public abstract destroy(): this;

Expand All @@ -43,7 +51,7 @@ export abstract class DataCore<Type> extends Immutability {
*/
public override lock(): this {
Immutability.deepFreeze(this.value);
super.lock();
super.lock();
return this;
}

Expand All @@ -52,7 +60,7 @@ export abstract class DataCore<Type> extends Immutability {
* @public
* @abstract
* @param {Type} value The data of `Type` to set.
* @returns {this} Returns current instance.
* @returns {this} Returns `this` current instance.
*/
public abstract set(value: Type): this;
}
16 changes: 13 additions & 3 deletions src/lib/data.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Data<Type> extends DataCore<Type> {
#value;

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

/**
* @description Clears the value to `null`.
* @public
* @returns {this} Returns `this` current instance.
*/
public clear(): this {
this.#value.set(null as unknown as Type);
return this;
}

/**
* @description Destroys the `Value` object by setting it to `null`.
* @public
* @returns {this} Returns the current instance.
* @returns {this} Returns `this` current instance.
*/
public destroy(): this {
this.#value = null as any;
Expand All @@ -60,7 +70,7 @@ export class Data<Type> extends DataCore<Type> {
* @description Sets the data value.
* @public
* @param {Type} value The data of `Type` to set.
* @returns {this} Returns the current instance.
* @returns {this} Returns `this` current instance.
*/
public set(value: Type) {
super.validate();
Expand Down
40 changes: 40 additions & 0 deletions src/lib/immutability.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,46 @@ export abstract class Immutability {
return object;
}

/**
* @description
* @public
* @readonly
* @type {boolean}
*/
public get frozen() {
return this.isFrozen();
}

/**
* @description
* @public
* @readonly
* @type {boolean}
*/
public get locked() {
return this.#locked;
}

/**
* @description
* @public
* @readonly
* @type {boolean}
*/
public get mutable() {
return this.isMutable();
}

/**
* @description
* @public
* @readonly
* @type {boolean}
*/
public get sealed() {
return this.isSealed();
}

/**
* @description Privately stored locked state as `true` if locked, otherwise `false`.
* @type {boolean}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export { DataCore } from './data-core.abstract';
export { Immutability } from './immutability.abstract';
// Class.
export { Data } from './data.class';
// `WeakData`.
export { IndexedWeakData } from './indexed-weak-data.class';
export { NamedWeakData } from './named-weak-data.class';
export { WeakData } from './weak-data.class';
// `Value`.
export { Value } from './value.class';
export { WeakData } from './weak-data.class';
Loading