├── .github └── workflows │ ├── nodejs.yml │ └── publish.yml ├── .gitignore ├── CODEOWNERS ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── SECURITY.md ├── package-lock.json ├── package.json ├── src └── index.ts ├── test ├── .eslintrc.json └── index.js └── tsconfig.json /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | on: [push] 3 | permissions: 4 | contents: read 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v4 10 | - name: Use Node.js 11 | uses: actions/setup-node@v4 12 | with: 13 | node-version: '22.x' 14 | - run: npm install 15 | - run: npm run build --if-present 16 | - run: npm test 17 | env: 18 | CI: true 19 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | permissions: 8 | contents: read 9 | id-token: write 10 | 11 | jobs: 12 | publish-npm: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | - uses: actions/setup-node@v4 17 | with: 18 | node-version: 22 19 | registry-url: https://registry.npmjs.org/ 20 | cache: npm 21 | - run: npm ci 22 | - run: npm test 23 | - run: npm version ${TAG_NAME} --git-tag-version=false 24 | env: 25 | TAG_NAME: ${{ github.event.release.tag_name }} 26 | - run: npm whoami; npm --ignore-scripts publish --provenance 27 | env: 28 | NODE_AUTH_TOKEN: ${{secrets.npm_token}} 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @github/web-systems-reviewers 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to make participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies within all project spaces, and it also applies when 49 | an individual is representing the project or its community in public spaces. 50 | Examples of representing a project or community include using an official 51 | project e-mail address, posting via an official social media account, or acting 52 | as an appointed representative at an online or offline event. Representation of 53 | a project may be further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at opensource@github.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | [fork]: https://github.com/github/multimap/fork 4 | [pr]: https://github.com/github/multimap/compare 5 | [code-of-conduct]: CODE_OF_CONDUCT.md 6 | 7 | Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great. 8 | 9 | Contributions to this project are [released](https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license) to the public under the [project's open source license](LICENSE). 10 | 11 | Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms. 12 | 13 | ## Submitting a pull request 14 | 15 | 0. [Fork][fork] and clone the repository 16 | 0. Configure and install the dependencies: `script/bootstrap` 17 | 0. Make sure the tests pass on your machine: `rake` 18 | 0. Create a new branch: `git checkout -b my-branch-name` 19 | 0. Make your change, add tests, and make sure the tests still pass 20 | 0. Push to your fork and [submit a pull request][pr] 21 | 0. Pat your self on the back and wait for your pull request to be reviewed and merged. 22 | 23 | Here are a few things you can do that will increase the likelihood of your pull request being accepted: 24 | 25 | - Write tests. 26 | - Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests. 27 | - Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html). 28 | 29 | ## Resources 30 | 31 | - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) 32 | - [Using Pull Requests](https://help.github.com/articles/about-pull-requests/) 33 | - [GitHub Help](https://help.github.com) 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 GitHub, Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MultiMap 2 | 3 | The [multimap][1] data structure is a map in which more than one value may be stored under each key. 4 | 5 | [1]: https://en.wikipedia.org/wiki/Multimap 6 | 7 | ## Installation 8 | 9 | ``` 10 | $ npm install @github/multimap 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import MultiMap from '@github/multimap' 17 | ``` 18 | 19 | ```ts 20 | const map = new MultiMap() 21 | map.set('a', 1) 22 | map.set('a', 2) 23 | map.get('a') // => Set([1, 2]) 24 | map.has('a') // => true 25 | map.size // => 1 26 | ``` 27 | 28 | ### Constructors 29 | 30 | - `MultiMap()` - Create an empty map. 31 | - `MultiMap(iterable)` - Create a map with values from an iterable yielding key value pairs: `new MultiMap([['k', 1], ['k', 2]])` 32 | 33 | ### Methods 34 | 35 | - `get(key)` - Retrieve the Set of values stored under a key or the empty Set if the key does not exist. 36 | - `set(key, value)` - Add a value to the key's set without removing previous values. Returns the map so `set` can be chained. 37 | - `has(key)` - Returns true if a value is stored under the key. 38 | - `delete(key)` - Remove key and all of key's values. Returns true if the key existed. 39 | - `delete(key, value)` - Remove a value from the key's set. Returns true if the key and value existed. 40 | - `drain(value)` - Remove a value from all keys that reference it. Returns an array of keys removed. 41 | - `clear()` - Remove all keys and values to empty the map. 42 | - `keys()` - An iterator of map keys. 43 | - `values()` - An iterator of Sets of values for all keys. 44 | - `entries()` - An iterator of [key, Set] pairs. 45 | 46 | ### Properties 47 | 48 | - `size` - The number of keys in the map. 49 | 50 | ## Development 51 | 52 | ``` 53 | npm install 54 | npm test 55 | ``` 56 | 57 | ## License 58 | 59 | Distributed under the MIT license. See LICENSE for details. 60 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | If you discover a security issue in this repository, please submit it through the [GitHub Security Bug Bounty](https://hackerone.com/github). 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@github/multimap", 3 | "version": "1.0.0", 4 | "description": "A map in which more than one value may be stored under each key.", 5 | "main": "dist/index.js", 6 | "type": "module", 7 | "module": "dist/index.js", 8 | "types": "dist/index.d.ts", 9 | "license": "MIT", 10 | "repository": "github/multimap", 11 | "files": [ 12 | "dist" 13 | ], 14 | "scripts": { 15 | "clean": "rm -rf dist", 16 | "lint": "eslint src/*.ts test/*.js", 17 | "prebuild": "npm run clean", 18 | "build": "tsc", 19 | "pretest": "npm run build && npm run lint", 20 | "test": "mocha", 21 | "prepublishOnly": "npm run build", 22 | "postpublish": "npm publish --ignore-scripts --@github:registry='https://npm.pkg.github.com'" 23 | }, 24 | "prettier": "@github/prettier-config", 25 | "devDependencies": { 26 | "@github/prettier-config": "0.0.6", 27 | "chai": "^4.3.8", 28 | "eslint": "^8.49.0", 29 | "eslint-plugin-github": "^4.10.0", 30 | "mocha": "^10.2.0", 31 | "typescript": "^5.2.2" 32 | }, 33 | "eslintConfig": { 34 | "extends": [ 35 | "plugin:github/browser", 36 | "plugin:github/recommended", 37 | "plugin:github/typescript" 38 | ] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export default class MultiMap { 2 | private map: Map> = new Map() 3 | 4 | constructor(iterable?: Iterable<[K, V]>) { 5 | if (iterable) { 6 | for (const [k, v] of iterable) { 7 | this.set(k, v) 8 | } 9 | } 10 | } 11 | 12 | get(key: K): Set { 13 | const values = this.map.get(key) 14 | return values ? values : new Set() 15 | } 16 | 17 | set(key: K, value: V): MultiMap { 18 | let values = this.map.get(key) 19 | if (!values) { 20 | values = new Set() 21 | this.map.set(key, values) 22 | } 23 | values.add(value) 24 | return this 25 | } 26 | 27 | has(key: K): boolean { 28 | return this.map.has(key) 29 | } 30 | 31 | delete(key: K, value?: V): boolean { 32 | const values = this.map.get(key) 33 | if (!values) return false 34 | if (!value) return this.map.delete(key) 35 | 36 | const deleted = values.delete(value) 37 | if (!values.size) this.map.delete(key) 38 | return deleted 39 | } 40 | 41 | drain(value: V): K[] { 42 | const empty = [] 43 | for (const key of this.keys()) { 44 | if (this.delete(key, value) && !this.has(key)) { 45 | empty.push(key) 46 | } 47 | } 48 | return empty 49 | } 50 | 51 | keys(): Iterable { 52 | return this.map.keys() 53 | } 54 | 55 | values(): Iterable> { 56 | return this.map.values() 57 | } 58 | 59 | entries(): Iterable<[K, Set]> { 60 | return this.map.entries() 61 | } 62 | 63 | [Symbol.iterator](): Iterable<[K, Set]> { 64 | return this.entries() 65 | } 66 | 67 | clear(): void { 68 | this.map.clear() 69 | } 70 | 71 | get size(): number { 72 | return this.map.size 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /test/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "espree", 3 | "parserOptions": { 4 | "ecmaVersion": 8 5 | }, 6 | "env": { 7 | "mocha": true 8 | }, 9 | "globals": { 10 | "assert": true 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | // eslint-disable-next-line import/extensions 2 | import MultiMap from '../dist/index.js' 3 | import {assert} from 'chai' 4 | 5 | describe('multimap', function () { 6 | it('initializes from an iterable', function () { 7 | const map = new MultiMap([ 8 | ['k1', 1], 9 | ['k1', 2], 10 | ['k2', 3], 11 | ]) 12 | assert.deepEqual([1, 2], [...map.get('k1')]) 13 | assert.deepEqual([3], [...map.get('k2')]) 14 | }) 15 | 16 | it('size measures keys not values', function () { 17 | const map = new MultiMap() 18 | assert.equal(0, map.size) 19 | 20 | map.set('k1', 1) 21 | assert.equal(1, map.size) 22 | 23 | map.set('k1', 2) 24 | assert.equal(1, map.size) 25 | 26 | map.set('k2', 1) 27 | assert.equal(2, map.size) 28 | 29 | map.delete('k2') 30 | assert.equal(1, map.size) 31 | 32 | map.clear() 33 | assert.equal(0, map.size) 34 | }) 35 | 36 | it('sets multiple values', function () { 37 | const map = new MultiMap() 38 | map.set('k', 1) 39 | assert.deepEqual([1], [...map.get('k')]) 40 | map.set('k', 2) 41 | assert.deepEqual([1, 2], [...map.get('k')]) 42 | }) 43 | 44 | it('sets only unique values', function () { 45 | const map = new MultiMap() 46 | map.set('k', 1) 47 | assert.deepEqual([1], [...map.get('k')]) 48 | map.set('k', 1) 49 | assert.deepEqual([1], [...map.get('k')]) 50 | }) 51 | 52 | it('set can be chained', function () { 53 | const map = new MultiMap() 54 | map.set('k1', 1).set('k1', 2).set('k2', 3) 55 | assert.deepEqual([1, 2], [...map.get('k1')]) 56 | assert.deepEqual([3], [...map.get('k2')]) 57 | }) 58 | 59 | it('gets empty set for missing key', function () { 60 | const map = new MultiMap() 61 | assert.deepEqual([], [...map.get('k')]) 62 | }) 63 | 64 | it('gets value set in insertion order', function () { 65 | const map = new MultiMap() 66 | map.set('k', 1) 67 | map.set('k', 2) 68 | assert.deepEqual([1, 2], [...map.get('k')]) 69 | }) 70 | 71 | it('deletes missing key', function () { 72 | const map = new MultiMap() 73 | assert.isFalse(map.delete('k')) 74 | }) 75 | 76 | it('deletes missing value for key', function () { 77 | const map = new MultiMap() 78 | map.set('k', 1) 79 | assert.isFalse(map.delete('k', 2)) 80 | assert.equal(1, map.size) 81 | assert.deepEqual([1], [...map.get('k')]) 82 | }) 83 | 84 | it('deletes all values for key', function () { 85 | const map = new MultiMap() 86 | map.set('k', 1) 87 | map.set('k', 2) 88 | assert.isTrue(map.delete('k')) 89 | assert.equal(0, map.size) 90 | }) 91 | 92 | it('deletes one value for key', function () { 93 | const map = new MultiMap() 94 | map.set('k', 1) 95 | map.set('k', 2) 96 | assert.isTrue(map.delete('k', 1)) 97 | assert.equal(1, map.size) 98 | assert.deepEqual([2], [...map.get('k')]) 99 | }) 100 | 101 | it('delete last value for key deletes the key', function () { 102 | const map = new MultiMap() 103 | map.set('k', 1) 104 | assert.isTrue(map.delete('k', 1)) 105 | assert.equal(0, map.size) 106 | assert.isFalse(map.has('k')) 107 | }) 108 | 109 | it('drains a value from all keys', function () { 110 | const map = new MultiMap() 111 | map.set('k1', 1).set('k1', 2) 112 | map.set('k2', 2).set('k2', 3) 113 | map.set('k3', 2) 114 | map.set('k4', 2) 115 | assert.deepEqual(['k3', 'k4'], map.drain(2)) 116 | assert.deepEqual([1], [...map.get('k1')]) 117 | assert.deepEqual([3], [...map.get('k2')]) 118 | assert.isTrue(map.has('k1')) 119 | assert.isTrue(map.has('k2')) 120 | assert.isFalse(map.has('k3')) 121 | assert.isFalse(map.has('k4')) 122 | }) 123 | 124 | it('contains a key', function () { 125 | const map = new MultiMap() 126 | assert.isFalse(map.has('k')) 127 | map.set('k', 1) 128 | assert.isTrue(map.has('k')) 129 | }) 130 | 131 | it('iterates over keys', function () { 132 | const map = new MultiMap() 133 | map.set('k1', 1) 134 | map.set('k2', 2) 135 | assert.deepEqual(['k1', 'k2'], [...map.keys()]) 136 | }) 137 | 138 | it('iterates over values', function () { 139 | const map = new MultiMap() 140 | map.set('k1', 1).set('k1', 2) 141 | map.set('k2', 3).set('k2', 4) 142 | assert.deepEqual( 143 | [ 144 | [1, 2], 145 | [3, 4], 146 | ], 147 | [...map.values()].map(set => [...set]), 148 | ) 149 | }) 150 | 151 | it('iterates over entries', function () { 152 | const map = new MultiMap() 153 | map.set('k1', 1).set('k1', 2) 154 | map.set('k2', 3).set('k2', 4) 155 | assert.deepEqual( 156 | [ 157 | ['k1', 1, 2], 158 | ['k2', 3, 4], 159 | ], 160 | [...map.entries()].map(([key, set]) => [key, ...set]), 161 | ) 162 | }) 163 | 164 | it('conforms to iterable protocol with entries iterator', function () { 165 | const map = new MultiMap() 166 | map.set('k1', 1).set('k1', 2) 167 | map.set('k2', 3).set('k2', 4) 168 | assert.deepEqual( 169 | [ 170 | ['k1', 1, 2], 171 | ['k2', 3, 4], 172 | ], 173 | [...map].map(([key, set]) => [key, ...set]), 174 | ) 175 | }) 176 | }) 177 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "es2022", 4 | "target": "es2017", 5 | "strict": true, 6 | "declaration": true, 7 | "outDir": "dist", 8 | "removeComments": true 9 | }, 10 | "files": [ 11 | "src/index.ts" 12 | ] 13 | } 14 | --------------------------------------------------------------------------------