Skip to content

A lightweight TypeScript library for basic data management.

License

Notifications You must be signed in to change notification settings

typescript-package/data

Repository files navigation

typescript-package/data

npm version GitHub issues GitHub license

A lightweight TypeScript library for basic data management.

Table of contents

Installation

npm install @typescript-package/data --save-peer

Api

import {
  // Abstract.
  DataCore,
  Immutability,

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

  // `WeakData`.
  NamedWeakData,
  // Indexed.
  IndexedWeakData,
  // Basic.
  WeakData,
} 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.

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

// Example subclass of Data
class StringData extends Data<string> {
  constructor(value: string) {
    super(value);
  }
}

const data = new StringData("Hello, world!");

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

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

// Destroy the value
data.destroy();
console.log(data.value); // Throws error or undefined (based on how it's handled)

Value

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

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

Immutability

Manages the immutability states of this current instance.

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

WeakData

IndexedWeakData

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

// Create an interface.
export interface Profile {
  id: number,
  age: number;
  score: number;
}

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

The WeakData class is a concrete class that stores data in a static WeakMap.

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

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

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

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

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

// Destroy the value
data.destroy();

Immutability

Sealed

Provides structural immutability, but not value immutability. The least strict form of immutability. Sealing an object prevents adding or removing properties, but it does not restrict modifying the values of existing properties.

Frozen

Provides structural and shallow immutability. Stricter than seal. Freezing an object prevents changes to the object’s properties at the top level. This means that you cannot modify, add, or delete properties, but nested objects can still be mutated.

Locked

Combines the features of freeze, but extends immutability to nested structures(deep immutability). Locking(deep) an object prevents changing any properties at any level, including properties within nested objects.

Contributing

Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.

Support

If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.

Support via:

Thanks for your support!

Code of Conduct

By participating in this project, you agree to follow Code of Conduct.

GIT

Commit

Versioning

Semantic Versioning 2.0.0

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards-compatible manner, and
  • PATCH version when you make backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

FAQ How should I deal with revisions in the 0.y.z initial development phase?

The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.

How do I know when to release 1.0.0?

If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.

License

MIT © typescript-package (license)

Packages