- β‘ Performant
- π₯ Zero dependencies
- π First class typescript support
- π Simple fluent API similar to native
map
Table of Contents
$ yarn add n-index-map
$ npm install n-index-map
Import the default NIndexMap
export and instantiate a new instance.
You can optionally pass an array of indexes and/or an array of initial data into the constructor, in which case NIndexMap will use those index keys to cache all inital and future data set on the instance. Otherwise, NIndexMap will behave similarly to native map, that is until you to decide to add an index.
import NIndexMap from "n-index-map";
interface TestInterface {
stringId: string;
numberId: number;
data: string;
}
const initialData: TestInterface[] = [
{ stringId: "1", numberId: 1, data: "foo" },
{ stringId: "2", numberId: 2, data: "bar" },
];
const exampleMap = new NIndexMap(["stringId", "numberId"], initialData);
const fooElement = exampleMap.get("numberId", 1);
const barElement = exampleMap.get("stringId", "2");
// prints: {stringId: '1', numberId: 1, data: 'foo'}
console.log(fooElement);
// prints: {stringId: '2', numberId: 2, data: 'bar'}
console.log(barElement);
You have the option to specify the types of the data you expect NIndexMap
to handle, along with the index keys when calling the constructor as generic type paramaters.
NOTE: This is automatically inferred from the params if provided, but of course, without constructor params this will need to be specified for full type support.
const exampleWithoutArgs = new NIndexMap<TestInterface, "stringId" | "numberId">();
// correct type support for parameters
const fooElement = exampleWithoutArgs.get("numberId", 1);
const barElement = exampleWithoutArgs.get("stringId", "2");
// prints: {stringId: '1', numberId: 1, data: 'foo'}
console.log(fooElement);
// prints: {stringId: '2', numberId: 2, data: 'bar'}
console.log(barElement);
...
constructor(indexKeys: IndexedProp[] = [], initialData: DataType[] = []): NIndexMap
Creates a new instance of NIndexMap.
has(key: IndexedProp, value: DataType[IndexedProp]): boolean
Returns a boolean indicating whether an element with the specified combination of indexed key and value exists or not.
get(key: IndexedProp, value: DataType[IndexedProp]): DataType | undefined
Returns a element with the specified key and value (indexed or not) if it exists in the store.
getOrDefault(key: IndexedProp, value: DataType[IndexedProp]): DataType | undefined
Returns a element with the specified key and value (indexed or not) if it exists in the store, otherwise returns the default value when specified.
getDefault(): DataType | undefined
Return default value if specified
set(...dataItems: DataType[]): this
Adds one or more elements to the NIndexMap store.
setDefault(dataItems: DataType): this
Adds one or more elements to the NIndexMap store.
delete(key: IndexedProp, value: DataType[IndexedProp]): this
Removes the element from the NIndexMap store that matches the specified key and value provided.
addIndex(key: IndexedProp): this
Adds new index to maintain for all currently and future added elements.
removeIndex(key: IndexedProp): this
Removes an exising index.
size(): number
Returns the size of the NIndexMap.
indexes(): number
Returns the size of the indexes maintained by the NIndexMap.
values(): IterableIterator<DataType>
Returns a new iterator object that contains the values for each element in the NIndexMap in insertion order.
entries(): IterableIterator<[number, DataType]>
Returns a new iterator object that contains the [key, value] pairs for each element in the NIndexMap in insertion order. In this particular case, this iterator object is also an iterable, so the for-of loop can be used.
clear(): void
Clears all elements and indexes from the NIndexMap.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. Don't forget to give the project a star! Thanks again!
- Changeset for changes to documentation, changelog generation, and release management.
- Fork the project and clone your fork:
gh repo fork --clone
- Create your feature branch:
git checkout -b feature/AmazingFeature
- Commit your changes:
git commit -m 'Add some AmazingFeature'
- Use the changeset cli to create a detailed description of your changes.
yarn changeset
This will be used to generate a changelog when we publish an update. Learn more about Changeset.
- Push branch and open a Pull Request
gh pr create
Distributed under the MIT License. See LICENSE
for more information.
Jarvis Prestidge - [email protected]
Project Link: https://github.com/jarvisprestidge/n-index-map