Skip to content

JarvisPrestidge/n-index-map

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

46 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Version Issues Pulls Codecov MIT License


header-dark

header-light

A multi index map implementation in TypeScript

Report Bug Β· Request Feature


  • ⚑ Performant
  • πŸ”₯ Zero dependencies
  • πŸŽ‰ First class typescript support
  • πŸ“– Simple fluent API similar to native map

Table of Contents
  1. Installation
  2. Usage
  3. API
  4. Contributing
  5. License
  6. Contact

πŸ’Ύ Installation

$ yarn add n-index-map
$ npm install n-index-map

(back to top)

πŸ”¨ Usage

Import the default NIndexMap export and instantiate a new instance.

Constructor

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);

Typing

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);
...

(back to top)

πŸ“– API

new

constructor(indexKeys: IndexedProp[] = [], initialData: DataType[] = []): NIndexMap

Creates a new instance of NIndexMap.

has

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

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

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

getDefault(): DataType | undefined

Return default value if specified

set

set(...dataItems: DataType[]): this

Adds one or more elements to the NIndexMap store.

setDefault

setDefault(dataItems: DataType): this

Adds one or more elements to the NIndexMap store.

delete

delete(key: IndexedProp, value: DataType[IndexedProp]): this

Removes the element from the NIndexMap store that matches the specified key and value provided.

addIndex

addIndex(key: IndexedProp): this

Adds new index to maintain for all currently and future added elements.

removeIndex

removeIndex(key: IndexedProp): this

Removes an exising index.

size

size(): number

Returns the size of the NIndexMap.

indexes

indexes(): number

Returns the size of the indexes maintained by the NIndexMap.

values

values(): IterableIterator<DataType>

Returns a new iterator object that contains the values for each element in the NIndexMap in insertion order.

indexes

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.

indexes

clear(): void

Clears all elements and indexes from the NIndexMap.

(back to top)

❀️ Contributing

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!

Tooling

  • Changeset for changes to documentation, changelog generation, and release management.

Making a Pull Request

  1. Fork the project and clone your fork:
gh repo fork --clone
  1. Create your feature branch:
git checkout -b feature/AmazingFeature
  1. Commit your changes:
git commit -m 'Add some AmazingFeature'
  1. 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.

  1. Push branch and open a Pull Request
gh pr create

(back to top)

πŸ“œ License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

πŸ“« Contact

Jarvis Prestidge - [email protected]

Project Link: https://github.com/jarvisprestidge/n-index-map

(back to top)