A lightweight TypeScript library for enhanced map
management.
npm install @typescript-package/data --save-peer
npm install @typescript-package/map --save-peer
import {
// Abstract.
CoreMap,
MapOnHook,
// Class.
DataMap,
FactoryMap,
WeakDataMap,
} from '@typescript-package/map';
The abstract core class is designed for building Map
and DataCore
related classes.
import { CoreMap } from '@typescript-package/map';
The DataMap
is a concrete class that extends Map
and encapsulates its data within a DataCore
store, providing additional data management capabilities.
import { DataMap } from '@typescript-package/map';
// Define a `DataCore` implementation for holding a data in `DataMap`.
export class CustomMapData<Key, Value> extends Data<Map<Key, Value>> {
constructor(initialValue?: Map<Key, Value>) {
super(initialValue ?? new Map());
}
}
// Create a new `DataMap` instance with predefined entries and customized data holder.
export const dataMap = new DataMap
// <string, number, CustomMapData<string, number>> previous approach, now captured.
(
[
["one", 1],
["two", 2],
["three", 3],
],
// new CustomMapData() // previous approach
CustomMapData // new approach
); // const dataMap: DataMap<string, number, CustomMapData<string, number>>
// Check the `CustomMapData`.
console.log(`Data holder of \`CustomMapData\`:`, dataMap.data); // Output: CustomMapData {#locked: false, #value: Value}
// Get the `CustomMapData` value.
console.log(`Data holder of \`CustomMapData\` value:`, dataMap.data.value); // Output: Map(3) {'one' => 1, 'two' => 2, 'three' => 3}
// Log the size of the map
console.log("Size:", dataMap.size); // Output: Size: 3
// Get a value from the map
console.log("Value for 'two':", dataMap.get("two")); // Output: Value for 'two': 2
// Check if a key exists
console.log("Has 'three'?", dataMap.has("three")); // Output: Has 'three'? true
// Set a new key-value pair
dataMap.set("four", 4);
console.log("Size after set:", dataMap.size); // Output: Size after set: 4
// Iterate over entries
dataMap.forEach((value, key) => console.log(`${key}: ${value}`));
// Output:
// one: 1
// two: 2
// three: 3
// four: 4
// Delete an entry
dataMap.delete("one");
console.log("Size after delete:", dataMap.size); // Output: Size after delete: 3
// Clear the map
dataMap.clear();
console.log("Size after clear:", dataMap.size); // Output: Size after clear: 0
import { FactoryMap } from '@typescript-package/map';
// Define custom `Map`.
export class CustomMap<Key, Value> extends Map<Key, Value> {
public newMethod() {}
constructor(entries?: [Key, Value][]) {
super(entries);
}
}
// Define data storage to store custom map.
export class TestCustomMapData<Key, Value> extends Data<CustomMap<Key, Value>> {
constructor(initialValue?: CustomMap<Key, Value>) {
super(initialValue ?? new CustomMap());
}
}
// Initialize the factory map with custom map and data.
const factoryMap = new FactoryMap(
[['a', {x: 1}
8000
], ['b', {x: 2}]],
// Use custom `Map`
CustomMap,
// Use custom storage for custom map.
TestCustomMapData,
{
// Define function for the default value.
defaultValue: () => ({x: 0}),
// Define cloner by using the `structuredClone`.
cloner: (value) => structuredClone(value),
}
); // const factoryMap: FactoryMap<string, { x: number; }, CustomMap<string, { x: number }>, TestCustomMapData<string, { x: number; }>>
console.log(factoryMap.get('c')); // { x: 0 }
console.log(factoryMap.get('b')); // { x: 2 }
console.log(factoryMap.get('a')); // { x: 1 }
console.debug(factoryMap.sort()); // sort.
The WeakDataMap
class is a concrete class that stores data in a static WeakMap
.
import { WeakDataMap } from '@typescript-package/map';
// Create an instance of `WeakDataMap`.
const weakDataMap = new WeakDataMap([
['one', 1],
['two', 2],
['three', 3],
]);
// Get the value from `WeakData` static.
console.log(`data: `, WeakData.get(weakDataMap.data)); // Output: Map(3) {'one' => 1, 'two' => 2, 'three' => 3}
// Get a value by key
console.log(weakDataMap.get('two')); // Output: 2
// Add a new key-value pair
weakDataMap.set('four', 4);
// Check if a key exists
console.log(weakDataMap.has('four')); // Output: true
// Delete a key
weakDataMap.delete('one');
// Iterate over entries
for (const [key, value] of weakDataMap.entries()) {
console.log(key, value);
}
// Output:
// two 2
// three 3
// four 4
Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
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!
By participating in this project, you agree to follow Code of Conduct.
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.
MIT © typescript-package (license)
- @typescript-package/affix: A lightweight TypeScript library for the affix - prefix and suffix.
- @typescript-package/are: Type-safe
are
checkers for validating value types in TypeScript. - @typescript-package/data: A lightweight TypeScript library for basic data management.
- @typescript-package/descriptor: A lightweight TypeScript library for property descriptor.
- @typescript-package/guard: Type-safe guards for guarding the value types in TypeScript.c
- @typescript-package/history: A TypeScript package for tracking history of values.
- @typescript-package/is: Type-safe is checkers for validating value types in TypeScript.
- @typescript-package/name: A lightweight TypeScript library for the name with prefix and suffix.
- @typescript-package/property: A lightweight TypeScript package with features to handle object properties.
- @typescript-package/queue: A lightweight TypeScript library for managing various queue and stack structures.
- @typescript-package/range: A lightweight TypeScript library for managing various types of ranges.
- @typescript-package/regexp: A lightweight TypeScript library for RegExp.
- @typescript-package/set: A lightweight TypeScript library for enhanced
set
management. - @typescript-package/state: Simple state management for different types in TypeScript.
- @typescript-package/storage: The storage of data under allowed names.
- @typescript-package/type: Utility types to enhance and simplify TypeScript development.
- @typescript-package/wrapper: A lightweight TypeScript library to wrap the text with the opening and closing chars.