pick-by-alias
Advanced tools
Comparing version 1.1.2 to 1.2.0
46
index.js
'use strict' | ||
module.exports = function pick (src, props) { | ||
var result = {} | ||
module.exports = function pick (src, props, keepRest) { | ||
var result = {}, prop, i | ||
if (typeof props === 'string') props = toList(props) | ||
if (Array.isArray(props)) { | ||
let res = {} | ||
for (var i = 0; i < props.length; i++) { | ||
var res = {} | ||
for (i = 0; i < props.length; i++) { | ||
res[props[i]] = true | ||
@@ -16,13 +16,30 @@ } | ||
for (var prop in props) { | ||
// convert strings to lists | ||
for (prop in props) { | ||
props[prop] = toList(props[prop]) | ||
} | ||
// keep-rest strategy requires unmatched props to be preserved | ||
var occupied = {} | ||
for (prop in props) { | ||
var aliases = props[prop] | ||
aliases = toList(aliases) | ||
if (Array.isArray(aliases)) { | ||
for (var i = 0; i < aliases.length; i++) { | ||
for (i = 0; i < aliases.length; i++) { | ||
var alias = aliases[i] | ||
if (keepRest) { | ||
occupied[alias] = true | ||
} | ||
if (alias in src) { | ||
result[prop] = src[alias] | ||
if (keepRest) { | ||
for (var j = i; j < aliases.length; j++) { | ||
occupied[aliases[j]] = true | ||
} | ||
} | ||
break | ||
@@ -33,2 +50,15 @@ } | ||
else if (prop in src) { | ||
if (props[prop]) { | ||
result[prop] = src[prop] | ||
} | ||
if (keepRest) { | ||
occupied[prop] = true | ||
} | ||
} | ||
} | ||
if (keepRest) { | ||
for (prop in src) { | ||
if (occupied[prop]) continue | ||
result[prop] = src[prop] | ||
@@ -35,0 +65,0 @@ } |
{ | ||
"name": "pick-by-alias", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Pick properties by aliases", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -22,3 +22,3 @@ # pick-by-alias [](http://github.com/badges/stability-badges) | ||
### `pick(object, properties)` | ||
### `pick(object, properties, keepRest=false)` | ||
@@ -38,5 +38,8 @@ Return an object with properties picked by the list or dict with aliases. Aliases are matched in priority order, ie. first matching name is picked. Each alias can be an array or a comma/space-separated string. | ||
`keepRest` defines if not listed object properties should be transfered to resulting object or discarded. | ||
## Related | ||
* [defined](https://www.npmjs.com/package/defined) − get first non-undefined out of a list of values | ||
* [update-diff](https://github.com/dfcreative/update-diff) − update object state by mapping diff. | ||
@@ -43,0 +46,0 @@ ## License |
25
test.js
@@ -53,5 +53,28 @@ 'use strict' | ||
t('avoid picking absent props', t => { | ||
t.deepEqual(pick({a: 1, b:2}, 'a b c'), {a: 1, b: 2}) | ||
t.deepEqual(pick({a: 1, b: 2}, 'a b c'), {a: 1, b: 2}) | ||
t.end() | ||
}) | ||
t('keep rest strategy', t => { | ||
t.deepEqual(pick({a: 1, B: 2, Be: 2.5, c: 3}, { | ||
a: true, | ||
b: 'B Be' | ||
}, true), | ||
{ | ||
a: 1, | ||
b: 2, | ||
c: 3 | ||
}) | ||
t.deepEqual(pick({a: 1, B: 2, Be: 2.5, c: 3}, { | ||
a: true, | ||
b: 'B Be' | ||
}), | ||
{ | ||
a: 1, | ||
b: 2 | ||
}) | ||
t.end() | ||
}) |
5745
169
47