Comparing version 0.1.2 to 0.2.0
121
index.js
@@ -13,2 +13,3 @@ /** | ||
var deepEqual = require('./lib/deepequal'); | ||
/** | ||
@@ -323,17 +324,39 @@ * deep comparison of `actual` and `expected` | ||
*/ | ||
function _splitKeys (keys, opts) { | ||
function _splitPath (keys) { | ||
var out; | ||
if (util.isString(keys)) { | ||
out = []; | ||
keys | ||
.split('.') | ||
.forEach(function(k){ | ||
k = k.trim() | ||
.replace(/^([^\[]+)\[(["']?)(.+)\2\]$/, function(m, m1, m2, m3) { | ||
if (m1 && m3) { | ||
out.push(m1, m3) | ||
} | ||
return ''; | ||
}); | ||
if (k) { | ||
out.push(k); | ||
} | ||
}); | ||
keys = out; | ||
} | ||
return keys; | ||
} | ||
/** | ||
* | ||
*/ | ||
function _splitProps (keys) { | ||
var test = {}; | ||
opts = extend({ char: ','}, opts); | ||
if (util.isString(keys)) { | ||
if (~keys.indexOf(opts.char)) { | ||
keys = keys.split(opts.char); | ||
} | ||
else { | ||
keys = [ keys ]; | ||
} | ||
keys = keys | ||
.split(',') | ||
.map(function(k){ | ||
return k.trim(); | ||
}); | ||
} | ||
if (opts.array) { | ||
return keys; | ||
} | ||
if (util.isArray(keys)) { | ||
@@ -343,4 +366,5 @@ keys.forEach(function(key){ | ||
}); | ||
return test; | ||
} | ||
return test; | ||
return {}; | ||
} | ||
@@ -364,15 +388,16 @@ | ||
* @param {Object} obj - object to pick properties from | ||
* @param {Array|String} keys - Array of properties or comma separated string of properties | ||
* @param {Array|String} props - Array of properties or comma separated string of properties | ||
* @return {Object} object with picked properties | ||
*/ | ||
function pick (obj, keys) { | ||
function pick (obj, props) { | ||
var key, | ||
val, | ||
out, | ||
test = _splitKeys(keys); | ||
test = _splitProps(props); | ||
if (util.isObject(obj)) { | ||
out = {}; | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key) && (key in test)) { | ||
out[key] = obj[key]; | ||
for (key in test) { | ||
if ((val = get(obj, key)) != undefined) { | ||
set(out, key, val); | ||
} | ||
@@ -404,12 +429,12 @@ } | ||
*/ | ||
function omit (obj, keys) { | ||
function omit (obj, props) { | ||
var key, | ||
out, | ||
test = _splitKeys(keys); | ||
test = _splitProps(props); | ||
if (util.isObject(obj)) { | ||
out = {}; | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key) && !(key in test)) { | ||
out[key] = obj[key]; | ||
out = clone(obj); | ||
for (key in test) { | ||
if ((get(obj, key))) { | ||
set(out, key, null); | ||
} | ||
@@ -423,3 +448,3 @@ } | ||
/** | ||
* select properties from `obj` | ||
* get properties from `obj` | ||
* | ||
@@ -444,3 +469,3 @@ * #### Example | ||
*/ | ||
function select(obj, keys) { | ||
function get(obj, keys, _default) { | ||
var i, | ||
@@ -450,6 +475,6 @@ key, | ||
keys = _splitKeys(keys, { char: '.', array: true }); | ||
keys = _splitPath(keys); | ||
if (!keys || keys.length === 0) { | ||
return; | ||
return _default; | ||
} | ||
@@ -463,3 +488,3 @@ | ||
else { | ||
return; | ||
return _default; | ||
} | ||
@@ -469,3 +494,39 @@ } | ||
} | ||
exports.select = select; | ||
exports.get = get; | ||
exports.select = get; | ||
/** | ||
* | ||
*/ | ||
function set(obj, keys, value, opts) { | ||
var i, | ||
key, | ||
last, | ||
tmp = obj || {}; | ||
opts = opts || {}; | ||
keys = _splitPath(keys); | ||
if (!keys || keys.length === 0) { | ||
return; | ||
} | ||
last = keys.pop(); | ||
for (i=0; i<keys.length; i++) { | ||
key = keys[i]; | ||
if (!tmp[key]) { | ||
tmp[key] = {}; | ||
} | ||
if (tmp.hasOwnProperty(key)) { | ||
tmp = tmp[key]; | ||
} | ||
} | ||
if (value === null) { | ||
delete(tmp[last]); | ||
} else { | ||
tmp[last] = value; | ||
} | ||
return obj; | ||
} | ||
exports.set = set; |
{ | ||
"name": "mergee", | ||
"description": "Utilities for objects", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -15,6 +15,3 @@ "engines": { | ||
"devDependencies": { | ||
"mocha": "latest", | ||
"jshint": "latest", | ||
"istanbul": "latest", | ||
"jsdoc": "latest" | ||
"mocha": "~2.2.5" | ||
}, | ||
@@ -21,0 +18,0 @@ "scripts": { |
@@ -454,2 +454,8 @@ 'use strict'; | ||
}); | ||
it('pick a and d using String with spaces', function(){ | ||
var o = { a:1, b:2, c:3, d:4 }; | ||
var e = { a:1, d:4 }; | ||
var r = M.pick(o, 'a, d '); | ||
assert.deepEqual(r, e); | ||
}); | ||
it('pick only d using String', function(){ | ||
@@ -461,2 +467,8 @@ var o = { a:1, b:2, c:3, d:4 }; | ||
}); | ||
it('pick a.b and c.d using String', function(){ | ||
var o = { a: { a:1, b:2 }, c: { c:3, d:4 }, e: 5 }; | ||
var e = { a: {b:2}, c: {d:4} }; | ||
var r = M.pick(o, 'a.b, c.d'); | ||
assert.deepEqual(r, e); | ||
}); | ||
it('pick from empty object', function(){ | ||
@@ -499,2 +511,8 @@ var o = {}; | ||
}); | ||
it('omit a.b and c.d using String', function(){ | ||
var o = { a: { a:1, b:2 }, c: { c:3, d:4 }, e: 5 }; | ||
var e = { a: { a:1 }, c: { c:3 }, e: 5 }; | ||
var r = M.omit(o, 'a.b, c.d'); | ||
assert.deepEqual(r, e); | ||
}); | ||
it('omit from empty object', function(){ | ||
@@ -501,0 +519,0 @@ var o = {}; |
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
41913
1
1235