many-keys-map
Advanced tools
Comparing version 1.0.2 to 1.0.3
56
index.js
'use strict'; | ||
const getInternalKeys = Symbol('getInternalKeys'); | ||
const getPrivateKey = Symbol('getPrivateKey'); | ||
const publicKeys = Symbol('publicKeys'); | ||
const objectHashes = Symbol('objectHashes'); | ||
const symbolHashes = Symbol('symbolHashes'); | ||
const nullKey = Symbol('null'); // `objectHashes` key for null | ||
let keyCounter = 0; | ||
function checkKeys(keys) { | ||
if (!Array.isArray(keys)) { | ||
throw new TypeError('The keys parameter must be an array'); | ||
} | ||
} | ||
@@ -21,5 +11,5 @@ module.exports = class ManyKeysMap extends Map { | ||
this[objectHashes] = new WeakMap(); | ||
this[symbolHashes] = new Map(); // https://github.com/tc39/ecma262/issues/1194 | ||
this[publicKeys] = new Map(); | ||
this._objectHashes = new WeakMap(); | ||
this._symbolHashes = new Map(); // https://github.com/tc39/ecma262/issues/1194 | ||
this._publicKeys = new Map(); | ||
@@ -41,11 +31,15 @@ // eslint-disable-next-line prefer-rest-params | ||
[getInternalKeys](keys, create = false) { | ||
const privateKey = this[getPrivateKey](keys, create); | ||
_getPublicKeys(keys, create = false) { | ||
if (!Array.isArray(keys)) { | ||
throw new TypeError('The keys parameter must be an array'); | ||
} | ||
const privateKey = this._getPrivateKey(keys, create); | ||
let publicKey; | ||
if (privateKey && this[publicKeys].has(privateKey)) { | ||
publicKey = this[publicKeys].get(privateKey); | ||
if (privateKey && this._publicKeys.has(privateKey)) { | ||
publicKey = this._publicKeys.get(privateKey); | ||
} else if (create) { | ||
publicKey = [...keys]; // Regenerate keys array to avoid external interaction | ||
this[publicKeys].set(privateKey, publicKey); | ||
this._publicKeys.set(privateKey, publicKey); | ||
} | ||
@@ -56,3 +50,3 @@ | ||
[getPrivateKey](keys, create = false) { | ||
_getPrivateKey(keys, create = false) { | ||
const privateKeys = []; | ||
@@ -64,3 +58,3 @@ for (let key of keys) { | ||
const hashes = typeof key === 'object' || typeof key === 'function' ? objectHashes : typeof key === 'symbol' ? symbolHashes : false; | ||
const hashes = typeof key === 'object' || typeof key === 'function' ? '_objectHashes' : typeof key === 'symbol' ? '_symbolHashes' : false; | ||
@@ -84,4 +78,3 @@ if (!hashes) { | ||
set(keys, value) { | ||
checkKeys(keys); | ||
const {publicKey} = this[getInternalKeys](keys, true); | ||
const {publicKey} = this._getPublicKeys(keys, true); | ||
return super.set(publicKey, value); | ||
@@ -91,4 +84,3 @@ } | ||
get(keys) { | ||
checkKeys(keys); | ||
const {publicKey} = this[getInternalKeys](keys); | ||
const {publicKey} = this._getPublicKeys(keys); | ||
return super.get(publicKey); | ||
@@ -98,4 +90,3 @@ } | ||
has(keys) { | ||
checkKeys(keys); | ||
const {publicKey} = this[getInternalKeys](keys); | ||
const {publicKey} = this._getPublicKeys(keys); | ||
return super.has(publicKey); | ||
@@ -105,5 +96,4 @@ } | ||
delete(keys) { | ||
checkKeys(keys); | ||
const {publicKey, privateKey} = this[getInternalKeys](keys); | ||
return Boolean(publicKey && super.delete(publicKey) && this[publicKeys].delete(privateKey)); | ||
const {publicKey, privateKey} = this._getPublicKeys(keys); | ||
return Boolean(publicKey && super.delete(publicKey) && this._publicKeys.delete(privateKey)); | ||
} | ||
@@ -113,4 +103,4 @@ | ||
super.clear(); | ||
this[symbolHashes].clear(); | ||
this[publicKeys].clear(); | ||
this._symbolHashes.clear(); | ||
this._publicKeys.clear(); | ||
} | ||
@@ -126,5 +116,1 @@ | ||
}; | ||
if (process.env.NODE_ENV === 'test') { | ||
Object.assign(module.exports, {publicKeys, symbolHashes}); | ||
} |
{ | ||
"name": "many-keys-map", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A `Map` subclass with support for multiple keys for one entry.", | ||
"license": "MIT", | ||
"repository": "bfred-it/many-keys-map", | ||
"author": "Federico Brigante <github@bfred.it> (bfred.it)", | ||
"keywords": [ | ||
"multiple", | ||
"any", | ||
"key", | ||
"keys", | ||
"many", | ||
"map", | ||
"multi", | ||
"multimap", | ||
"multi", | ||
"key", | ||
"many", | ||
"any" | ||
"multiple" | ||
], | ||
"repository": "fregante/many-keys-map", | ||
"license": "MIT", | ||
"author": "Federico Brigante <opensource@bfred.it> (bfred.it)", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
@@ -24,6 +25,2 @@ ], | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
@@ -33,3 +30,7 @@ "rules": { | ||
} | ||
}, | ||
"devDependencies": { | ||
"ava": "^2", | ||
"xo": "^0.20" | ||
} | ||
} |
# many-keys-map [![(size)][badge-gzip]](#no-link) [![(status)][badge-travis]][link-travis] | ||
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/many-keys-map.svg?label=gzipped | ||
[badge-travis]: https://api.travis-ci.com/bfred-it/many-keys-map.svg?branch=master | ||
[link-travis]: https://travis-ci.org/bfred-it/many-keys-map | ||
[badge-travis]: https://api.travis-ci.com/fregante/many-keys-map.svg?branch=master | ||
[link-travis]: https://travis-ci.org/fregante/many-keys-map | ||
[link-npm]: https://www.npmjs.com/package/many-keys-map | ||
@@ -30,3 +30,3 @@ | ||
The number of keys allowed is unlimited and their order is relevant. | ||
The number of keys allowed is unlimited and their order matters. | ||
@@ -83,3 +83,3 @@ ## Install | ||
3. `ManyKeysMap` supports any number of keys, any of these are valid and different: `.get([a])` and `.get([a, b, c, d, e, f, g])` | ||
4. The order of keys is irrelevant, so `.get([a, b])` is different from `.get(b, a)` | ||
4. The order of keys matters, so `.get([a, b])` is different from `.get([b, a])` | ||
5. The keys can be anything supported by `Map`. | ||
@@ -90,2 +90,2 @@ | ||
- [many-keys-weakmap](https://github.com/bfred-it/many-keys-weakmap) - A `WeakMap` subclass with support for multiple keys for one entry. | ||
- [many-keys-weakmap](https://github.com/fregante/many-keys-weakmap) - A `WeakMap` subclass with support for multiple keys for one entry. |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
5
6927
85
1