feat(@vates): add MultiKeyMap (#5362)

This commit is contained in:
badrAZ
2020-11-09 12:30:35 +01:00
committed by GitHub
parent 9fa0ec440d
commit 97bcc7afb6
9 changed files with 103 additions and 4 deletions

View File

@@ -0,0 +1,48 @@
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
# @vates/multi-key-map
[![Package Version](https://badgen.net/npm/v/@vates/multi-key-map)](https://npmjs.org/package/@vates/multi-key-map) ![License](https://badgen.net/npm/license/@vates/multi-key-map) [![PackagePhobia](https://badgen.net/bundlephobia/minzip/@vates/multi-key-map)](https://bundlephobia.com/result?p=@vates/multi-key-map) [![Node compatibility](https://badgen.net/npm/node/@vates/multi-key-map)](https://npmjs.org/package/@vates/multi-key-map)
> Create map with values affected to multiple keys
## Install
Installation of the [npm package](https://npmjs.org/package/@vates/multi-key-map):
```
> npm install --save @vates/multi-key-map
```
## Usage
```js
import { MultiKeyMap } from '@vates/multi-key-map'
const map = new MultiKeyMap()
map.set([], 0)
map.set(['foo'], 1)
map.set(['foo', 'bar'], 2)
map.set(['bar', 'foo'], 3)
map.get([]) // 0
map.get(['foo']) // 1
map.get(['foo', 'bar']) // 2
map.get(['bar', 'foo']) // 3
```
## Contributions
Contributions are _very_ welcomed, either on the documentation or on
the code.
You may:
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
you've encountered;
- fork and create a pull request.
## License
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)

View File

@@ -0,0 +1,20 @@
```js
import { MultiKeyMap } from '@vates/multi-key-map'
const map = new MultiKeyMap()
const OBJ = {}
map.set([], 0)
map.set(['foo'], 1)
map.set(['foo', 'bar'], 2)
map.set(['bar', 'foo'], 3)
map.set([OBJ], 4)
map.set([{}], 5)
map.get([]) // 0
map.get(['foo']) // 1
map.get(['foo', 'bar']) // 2
map.get(['bar', 'foo']) // 3
map.get([OBJ]) // 4
map.get([{}]) // undefined
```

View File

@@ -67,7 +67,7 @@ function set(node, i, keys, value) {
return node
}
export default class MultiKeyMap {
exports.MultiKeyMap = class MultiKeyMap {
constructor() {
// each node is either a value or a Node if it contains children
this._root = undefined

View File

@@ -1,6 +1,6 @@
/* eslint-env jest */
import MultiKeyMap from './_MultiKeyMap'
const { MultiKeyMap } = require('./')
describe('MultiKeyMap', () => {
it('works', () => {

View File

@@ -0,0 +1,28 @@
{
"private": false,
"name": "@vates/multi-key-map",
"description": "Create map with values affected to multiple keys",
"keywords": [
"cache",
"map"
],
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/multi-key-map",
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"repository": {
"directory": "@vates/multi-key-map",
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"author": {
"name": "Vates SAS",
"url": "https://vates.fr"
},
"license": "ISC",
"version": "0.0.0",
"engines": {
"node": ">=8.10"
},
"scripts": {
"postversion": "npm publish --access public"
}
}

View File

@@ -33,6 +33,7 @@
>
> In case of conflict, the highest (lowest in previous list) `$version` wins.
- @vates/multi-key-map minor
- @xen-orchestra/fs patch
- xo-server minor
- xo-web minor

View File

@@ -44,6 +44,7 @@
"@xen-orchestra/self-signed": "^0.1.0",
"@xen-orchestra/template": "^0.1.0",
"@vates/decorate-with": "^0.0.1",
"@vates/multi-key-map": "^0.0.0",
"@vates/parse-duration": "0.1.0",
"ajv": "^6.1.1",
"app-conf": "^0.8.0",

View File

@@ -1,7 +1,7 @@
import assert from 'assert'
import { MultiKeyMap } from '@vates/multi-key-map'
import ensureArray from './_ensureArray'
import MultiKeyMap from './_MultiKeyMap'
function State() {
this.i = 0

View File

@@ -1,5 +1,6 @@
import { MultiKeyMap } from '@vates/multi-key-map'
import ensureArray from './_ensureArray'
import MultiKeyMap from './_MultiKeyMap'
function removeCacheEntry(cache, keys) {
cache.delete(keys)