npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@blueworld/dictionarray

v1.0.0

Published

An indexed array for fast data access

Downloads

4

Readme

Installation

Simply install the module via NPM:

npm i @blueworld/dictionarray -S

Usage

Import the TypeScript module into your project files:

import DictionArray from "@blueworld/dictionarray";

Constructor

The constructor of the generic DictionArray class receives a type and an object of index functions that we want to build up.

Let's consider the following example:

interface Test {
	id: number;
	text: string;
}

const d = new DictionArray<Test>({
	identifier: (elem: Test) => {
		return elem.id.toString();
	}
});

We instantiate a new DictionArray of type Test. We also specify an index with the name identifier. The index function extracts the property id of the element of type Test. Everytime a new element is added or removed to and from the DictionArray, the index for identifier is updated by running the index function across all elements of the DictionArray.

Note: The index function only accepts strings as return values. If you want your numeric id property to be indexed, you have to cast it into a string first, as seen in the example above. This is due to the fact that the extracted property per element of object T will become a key in an ECMAScript object internally.

push(...item: T[]): number

The push method works analogous to the standard ECMAScript Array.push() method. It takes n-many objects of type T and returns the new length of the array.

Example:

d.push({
	id: 123,
	text: "Test"
});

Triggers reindex: ✅

lookup(index: string, qualifier: string): number[]

This is where the magic of the DictionArray happens. We can push objects onto the array-part of the DictionArray, but what if we want to find a single element in the data structure without looping the whole array? Glad you're asking! Since we declared an index of name identifier in the Constructor-section above, we can use this index to lookup elements O(1) runtime.

const pos: number[] = d.lookup("identifier", "123");

The contant pos will now contain the positions of the objects of type T with the identifier of value 123 within the dictionarray.

Triggers reindex: ❌

get(index: number): T

To read an object at a certain position within the DictionArray, use the get() method.

Example:

const obj: Test = d.get(0);

Will return the first object within the DictionArray.

Triggers reindex: ❌

count(): number

count() returns the length of the array within the DictionArray.

Example:

const length: number = d.count();

Triggers reindex: ❌

all(): T[]

The retrieve all elements of the DictionArray in array form, use the all() function:

const elements: Test[] = d.all();

Triggers reindex: ❌

clear(): void

To remove all elements from the DictionArray use the clear() method:

d.clear();

Triggers reindex: ❌ (well, the index is completely cleared)

map(callbackFn: (item: T, index: number) => any): any[]

Proxy function for the map() function of the underlaying array within the DictionArray.

Example:

const result = d.map((item) => {
	return item.text;
});

Triggers reindex: ❌

filter(callbackFn: (item: T, index?: number) => boolean): T[]

Proxy function for the filter() function of the underlaying array within the DictionArray.

Example:

const result: Test[] = d.filter((item) => {
	return item.text.length > 2;
});

Triggers reindex: ❌

splice(start: number, deleteCount: number, items?: T[]): T[]

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements.

Works analogous to the ECMAScript splice() function.

Example:

d.splice(0, 1);

This removes the first item from the array.

Triggers reindex: ✅