Giter Club home page Giter Club logo

multidict's Introduction

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

multidict's People

Contributors

vicary avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.