8000 GitHub - typescript-package/map: A lightweight TypeScript library for enhanced `map` management.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

typescript-package/map

Repository files navigation

typescript-package/map

npm version GitHub issues GitHub license

A lightweight TypeScript library for enhanced map management.

Table of contents

Installation

1. Install peer data dependencies

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

2. Install the map package

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

Api

import {
  // Abstract.
  CoreMap,
  MapOnHook,

  // Class.
  DataMap,
  FactoryMap,
  WeakDataMap,
} from '@typescript-package/map';

CoreMap

The abstract core class is designed for building Map and DataCore related classes.

import { CoreMap } from '@typescript-package/map';

DataMap

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

FactoryMap

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.

WeakDataMap

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

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

About

A lightweight TypeScript library for enhanced `map` management.

Resources

License

Stars

Watchers

Forks

Packages

No packages published
0