@teamawesome/multi-dict
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -173,2 +173,37 @@ "use strict"; | ||
/** | ||
* Create a new dictionary from the given level. | ||
* | ||
* @param {...*} keys | ||
* @returns {MultiDict} | ||
*/ | ||
}, { | ||
key: "level", | ||
value: function level() { | ||
for (var _len5 = arguments.length, keys = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
keys[_key5] = arguments[_key5]; | ||
} | ||
if (this.tree.get(keys, false) === undefined) { | ||
throw new Error('Cannot create dictionary from non-existing level.'); | ||
} | ||
var _this$tree$options = this.tree.options, | ||
defaultType = _this$tree$options.defaultType, | ||
types = _this$tree$options.types; | ||
var dict = new MultiDict([], { | ||
defaultType: defaultType, | ||
types: types.slice(keys.length) | ||
}); | ||
var indexedItems = new Map((0, _toConsumableArray2["default"])(this.items).map(function (item, index) { | ||
return [item, index]; | ||
})); | ||
this.tree.level(keys).sort(function (a, b) { | ||
return indexedItems.get(a) - indexedItems.get(b); | ||
}).forEach(function (item) { | ||
dict.set.apply(dict, (0, _toConsumableArray2["default"])(item.keys.slice(keys.length)).concat([item.value])); | ||
}); | ||
return dict; | ||
} | ||
/** | ||
* Get an iterator for each of the entries. | ||
@@ -175,0 +210,0 @@ * |
@@ -41,6 +41,13 @@ "use strict"; | ||
} | ||
/** | ||
* @param {Array} keys | ||
* @param {boolean} value - True to return the value at this level, false for the node. | ||
* @return {*|undefined} | ||
*/ | ||
(0, _createClass2["default"])(Tree, [{ | ||
key: "get", | ||
value: function get(keys) { | ||
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
var node = this.root; | ||
@@ -66,3 +73,3 @@ | ||
return _access["default"].get(node, LEAF); | ||
return value ? _access["default"].get(node, LEAF) : node; | ||
} | ||
@@ -100,20 +107,6 @@ }, { | ||
value: function has(keys) { | ||
var node = this.root; | ||
var node = this.get(keys, false); | ||
var _iterator3 = _createForOfIteratorHelper(keys), | ||
_step3; | ||
try { | ||
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { | ||
var key = _step3.value; | ||
node = _access["default"].get(node, key); | ||
if (node === undefined) { | ||
return false; | ||
} | ||
} | ||
} catch (err) { | ||
_iterator3.e(err); | ||
} finally { | ||
_iterator3.f(); | ||
if (node === undefined) { | ||
return false; | ||
} | ||
@@ -126,20 +119,6 @@ | ||
value: function _delete(keys) { | ||
var node = this.root; | ||
var node = this.get(keys, false); | ||
var _iterator4 = _createForOfIteratorHelper(keys), | ||
_step4; | ||
try { | ||
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { | ||
var key = _step4.value; | ||
node = _access["default"].get(node, key); | ||
if (node === undefined) { | ||
return false; | ||
} | ||
} | ||
} catch (err) { | ||
_iterator4.e(err); | ||
} finally { | ||
_iterator4.f(); | ||
if (node === undefined) { | ||
return false; | ||
} | ||
@@ -154,2 +133,34 @@ | ||
} | ||
}, { | ||
key: "level", | ||
value: function level(keys) { | ||
var node = this.get(keys, false); | ||
var entries = []; | ||
var walker = function walker(level) { | ||
var _iterator3 = _createForOfIteratorHelper(_access["default"].entries(level)), | ||
_step3; | ||
try { | ||
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { | ||
var _step3$value = (0, _slicedToArray2["default"])(_step3.value, 2), | ||
key = _step3$value[0], | ||
value = _step3$value[1]; | ||
if (key === LEAF) { | ||
entries.push(value); | ||
} else { | ||
walker(_access["default"].get(level, key)); | ||
} | ||
} | ||
} catch (err) { | ||
_iterator3.e(err); | ||
} finally { | ||
_iterator3.f(); | ||
} | ||
}; | ||
walker(node, []); | ||
return entries; | ||
} | ||
/** | ||
@@ -156,0 +167,0 @@ * Create a tree node. |
{ | ||
"name": "@teamawesome/multi-dict", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
# Installation | ||
``` | ||
```shell script | ||
npm install @teamawesome/multi-dict | ||
``` | ||
# Usage | ||
The aim of multi-dict is to behave just like a set of nested maps, but with an easy to use api. | ||
``` | ||
import MultiDict from '@teamawesome/multi-dict' | ||
The aim of multi-dict is to behave just like a set of nested maps, but with an easy to use api. It supports | ||
all the regular methods of `Map`, including the iterators. | ||
```js | ||
import Dictionary from '@teamawesome/multi-dict' | ||
const dict = new MultiDict(); | ||
const dict = new Dictionary(); | ||
``` | ||
@@ -17,15 +18,15 @@ | ||
* `options` Optional. A hash of options as specified in the options paragraph. | ||
```js | ||
new Dictionary([ /* Entries */ ], { /* Options */ }); | ||
new Dictionary([ /* Entries */ ]); | ||
``` | ||
new Dict([ /* Entries */ ], { /* Options */ }); | ||
new Dict([ /* Entries */ ]); | ||
``` | ||
## Options | ||
By default, for each level of the dict a Map is used. However, you can specify a type for each level and/or a | ||
By default, each level of the dict is a `Map`. However, you can specify a type for each level and/or a | ||
default type. | ||
* `defaultType` The type for each level that is not specified in `types`. Default is Map. | ||
* `types` Array of types, for each depth that should be specified. | ||
``` | ||
* `defaultType` The constructor for each level not specified in `types`. Default is `Map`. | ||
* `types` Array of constructors, for each depth that should be specified. | ||
```js | ||
// Each level of the dict will be an array | ||
new MultiDict({ | ||
defaultType: Array | ||
defaultType: Array | ||
}); | ||
@@ -36,3 +37,3 @@ | ||
new MultiDict({ | ||
types: [Object, Array] | ||
types: [Object, Array] | ||
}); | ||
@@ -47,8 +48,9 @@ ``` | ||
* `clear()` Remove all entries in the dict | ||
* `level(...keys)` Get a new Dictionary from the given level | ||
* `forEach(callback, thisArg)` Run a callback for each entry of the dict | ||
``` | ||
```js | ||
dict.set(key1, key2, value); | ||
dict.get(key1, key2); // => value | ||
``` | ||
``` | ||
```js | ||
dict.set(key1, key2, key3, value); | ||
@@ -62,3 +64,13 @@ dict.get(key1, key2, key3); // => value | ||
``` | ||
```js | ||
dict = new Dictionary([ | ||
['a', 'b', 'value',] | ||
['a', 'b', 'c1', 'value c1'] | ||
['a', 'b', 'c2', 'value c2'] | ||
]); | ||
dict2 = dict.level('a', 'b'); | ||
[...dict2.entries()] // => [[[], 'value'], [['c1'], 'value c1'], [['c2'], 'value c2']] | ||
``` | ||
## Iterable methods: | ||
@@ -69,3 +81,3 @@ * `keys` Get an iterable yielding all the paths of the dict | ||
* `Symbol.iterator` Get an iterable yielding all the paths and values of the dict | ||
``` | ||
```js | ||
for (const keys of dict.keys()) | ||
@@ -72,0 +84,0 @@ for (const value of dict.values()) |
@@ -120,2 +120,31 @@ import Item from './item'; | ||
/** | ||
* Create a new dictionary from the given level. | ||
* | ||
* @param {...*} keys | ||
* @returns {MultiDict} | ||
*/ | ||
level(...keys) { | ||
if (this.tree.get(keys, false) === undefined) { | ||
throw new Error('Cannot create dictionary from non-existing level.'); | ||
} | ||
const { defaultType, types } = this.tree.options; | ||
const dict = new MultiDict([], { | ||
defaultType, | ||
types: types.slice(keys.length), | ||
}); | ||
const indexedItems = new Map( | ||
[...this.items].map((item, index) => [item, index]), | ||
); | ||
this.tree.level(keys) | ||
.sort((a, b) => indexedItems.get(a) - indexedItems.get(b)) | ||
.forEach((item) => { | ||
dict.set(...item.keys.slice(keys.length), item.value); | ||
}); | ||
return dict; | ||
} | ||
/** | ||
* Get an iterator for each of the entries. | ||
@@ -122,0 +151,0 @@ * |
@@ -15,3 +15,8 @@ import access from '@teamawesome/access'; | ||
get(keys) { | ||
/** | ||
* @param {Array} keys | ||
* @param {boolean} value - True to return the value at this level, false for the node. | ||
* @return {*|undefined} | ||
*/ | ||
get(keys, value = true) { | ||
let node = this.root; | ||
@@ -27,3 +32,3 @@ | ||
return access.get(node, LEAF); | ||
return value ? access.get(node, LEAF) : node; | ||
} | ||
@@ -46,10 +51,6 @@ | ||
has(keys) { | ||
let node = this.root; | ||
const node = this.get(keys, false); | ||
for (const key of keys) { | ||
node = access.get(node, key); | ||
if (node === undefined) { | ||
return false; | ||
} | ||
if (node === undefined) { | ||
return false; | ||
} | ||
@@ -61,10 +62,6 @@ | ||
delete(keys) { | ||
let node = this.root; | ||
const node = this.get(keys, false); | ||
for (const key of keys) { | ||
node = access.get(node, key); | ||
if (node === undefined) { | ||
return false; | ||
} | ||
if (node === undefined) { | ||
return false; | ||
} | ||
@@ -79,2 +76,20 @@ | ||
level(keys) { | ||
const node = this.get(keys, false); | ||
const entries = []; | ||
const walker = (level) => { | ||
for (const [key, value] of access.entries(level)) { | ||
if (key === LEAF) { | ||
entries.push(value); | ||
} else { | ||
walker(access.get(level, key)); | ||
} | ||
} | ||
}; | ||
walker(node, []); | ||
return entries; | ||
} | ||
/** | ||
@@ -81,0 +96,0 @@ * Create a tree node. |
@@ -44,2 +44,10 @@ const should = require('should'); | ||
it('get set no key', () => { | ||
const dict = new Dict(); | ||
dict.set('value').should.equal(dict); | ||
dict.size.should.be.equal(1); | ||
dict.get().should.equal('value'); | ||
dict.has().should.be.true(); | ||
}); | ||
it('overwrite', () => { | ||
@@ -211,1 +219,26 @@ const dict = new Dict(); | ||
}); | ||
it('level', () => { | ||
const dict1 = new Dict([ | ||
['a', 'b1', 'c', '3'], | ||
['a', 'b1', '1'], | ||
['a', 'b2', '2'], | ||
]); | ||
(() => dict1.level('nope')).should.throw(); | ||
const dict2 = dict1.level('a'); | ||
dict2.should.be.instanceof(Dict); | ||
dict2.size.should.equal(3); | ||
[...dict2].should.eql([ | ||
[['b1', 'c'], '3'], | ||
[['b1'], '1'], | ||
[['b2'], '2'], | ||
]); | ||
const dict3 = dict1.level('a', 'b1'); | ||
dict3.size.should.equal(2); | ||
dict3.get().should.equal('1'); | ||
dict3.get('c').should.equal('3'); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
39585
1074
87
0