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

arraymap-js

v1.0.3

Published

A Map-like data structure for Identifiable values that provides many array-like operations as well

Downloads

4

Readme

What

A thin wrapper around Array and some Maps that provides relatively efficient array-like and map-like methods and functions. Index-based operations and key-based operations are both usually constant time.

Why

My JavaScript and TypeScript work is all frontend work, specifically single page apps with React. When working on such applications, almost all the data I work with has an "id" field of some kind. This is usually a string, number, or uuid, which I just model as a string in TS.

I find myself needing to update or remove elements by ID, but also needing array-like operations such as map and filter (plus some other operations that I miss from other languages!). Sometimes I use a Map, but then I can't access elements by index or push something to the end, so most often I'm just using Array, which of course does not know that my elements all have unique IDs.

Enter ArrayMap, which does both. In addition to the usual suspects of put, get, push, map, and filter, it provides some additional operations such as collect, groupBy, sortByKey, and more.

I find this to be a highly convenient solution that makes state management much easier when building my applications.

Performance

ArrayMap does its best to be efficient, but it is primarily about convenience and correctness, not performance. Since ArrayMap has to maintain additional information about the position of each key within the primary array, some of the operations are a bit more expensive, notably 'remove'. However, it should be more than fast enough for any reasonable frontend use case. Don't build a game-engine on top of it.

The Basics

ArrayMap works with any type T, but it needs to know how to get the ID of that type. It does this with the provided identify function which takes a T and returns its ID. For example, if we had a user named Alex and a User model type:

interface User {
  id: number;
  name: string;
  email: string;
}
const alex = { id: 1, name: "Alex", email: "[email protected]" };

We could add Alex to our users collection like so:

import ArrayMap from "arraymap-js"; 
const users = ArrayMap.empty(user => user.id);
users.push(alex);
//or
const users = ArrayMap.fromArray([alex], user => user.id);

Usage

IDE dot completion is your friend here, as the names, docstrings, and types of every method should explain its usage. For constructors, try ArrayMap.. For operations on an ArrayMap x, try x.