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

multidict

v1.0.9

Published

MultiDict is an extension of Map that supports bidirectioal, many to many relations among multiple keys and values.

Downloads

20,158

Readme

MultiDict

MultiDict is an extension of Map that supports bidirectioal, many to many relations amongst multiple keys and values.

Usage

import { MultiDict } from "multidict";

const myMap = new MultiDict<string, number>();
myMap.set("foo", 1);
myMap.set("foo", 2);
myMap.set(3, "bar"); // In a two-way reference model, key and value means the same thing.

console.log(myMap.get("foo")); // -> Set { 1, 2 }
console.log(myMap.get(1)); // -> Set { "foo" }

console.log(myMap.get("bar")); // -> Set { 3 }
console.log(myMap.get(3)); // -> Set { "bar" }

You may also use it like an iterator:

for (const [key, values] of myMap) {
  for (const value of values) {
    console.log(key, value);
  }
}

MultiDict supports all of the standard Map methods, but always return a Set of values.

You can easily deletes all values associated with a key with the delete() method, or only deletes one specific key-value pair by specifying the value as well:

myMap.set("foo", 1);
myMap.set(2, "foo");
myMap.set("foo", 3);

myMap.delete("foo", 1);
console.log(myMap.get("foo")); // -> Set { 2, 3 }

myMap.delete("foo");
console.log(myMap.get("foo")); // -> undefined
console.log(myMap.get(2)); // -> undefined

API

  1. clear(): Clears all key-value pairs from the store.
  2. delete(key: K, value?: V): boolean: Deletes a key-value pair from the store. If a value parameter is provided, only the specified key-value pair is deleted. If no value parameter is provided, all key-value pairs associated with the specified key are deleted. Returns true if the key-value pair(s) were deleted, false otherwise.
  3. entries(): Returns an iterator of all key-value pairs in the store.
  4. forEach(callbackfn: (value: Set<K | V>, key: K | V, map: Map<K | V, Set<K | V>>) => void, thisArg?: any): Calls a provided function once for each key-value pair in the store, in insertion order.
  5. get(key: K): Set<V> | undefined: Returns a Set of all values associated with the specified key, or undefined if the key is not present in the store.
  6. has(key: K | V): boolean: Returns true if the specified key or value is present in the store, false otherwise.
  7. keys(): Returns an iterator of all keys in the store.
  8. set(key: K, value: V): this: Associates a key with a value in the store. Returns the same instance to allow for method chaining. If the key or value already exists in the store, the existing values associated with the key or value are preserved, and the new value is added to the existing set of values.
  9. size: Returns the number of key-value pairs in the store.
  10. values(): Returns an iterator of all values in the store.

Contributing

If you find a bug or would like to suggest a new feature, please open an issue or submit a pull request on GitHub.

License

MultiDict is licensed under the MIT License. See the LICENSE file for more information.

Funding

If you find this project useful, please consider supporting it by donating to the author.

Donate