mergee
Advanced tools
Comparing version 0.2.0 to 0.2.1
127
index.js
@@ -14,2 +14,6 @@ /** | ||
var OPEN = /\[["']|^\s*["']/; | ||
var CLOSE = /["']\]|["']\s*$/; | ||
var QUOTES = /^(["'])(.*)\1$/; | ||
/** | ||
@@ -319,3 +323,35 @@ * deep comparison of `actual` and `expected` | ||
/** | ||
* split comma separated String or Array into a test hash | ||
* segment path or properties string | ||
* @api private | ||
* @param {String} char - separator char | ||
* @return {Function} | ||
*/ | ||
function _segment (char) { | ||
var tmp; | ||
char = char || '.'; | ||
return function(k){ | ||
if (tmp) { | ||
tmp += char + k; | ||
if (CLOSE.test(k)) { | ||
k = tmp; | ||
tmp = ''; | ||
} | ||
else { | ||
return; | ||
} | ||
} | ||
else if (OPEN.test(k)) { | ||
tmp = k; | ||
if (CLOSE.test(k)) { | ||
tmp = ''; | ||
} else { | ||
return; | ||
} | ||
} | ||
return k.trim().replace(QUOTES, '$2'); | ||
}; | ||
} | ||
/** | ||
* split dot separated String or Array into a property path | ||
* @private | ||
@@ -332,7 +368,8 @@ * @param {Array|String} keys | ||
.split('.') | ||
.map(_segment('.')) | ||
.forEach(function(k){ | ||
k = k.trim() | ||
k = (k||' ').trim() | ||
.replace(/^([^\[]+)\[(["']?)(.+)\2\]$/, function(m, m1, m2, m3) { | ||
if (m1 && m3) { | ||
out.push(m1, m3) | ||
out.push(m1, m3); | ||
} | ||
@@ -349,18 +386,23 @@ return ''; | ||
} | ||
exports._splitPath = _splitPath; | ||
/** | ||
* | ||
* split comma separated String or Array into a test hash | ||
* @private | ||
* @param {Array|String} keys | ||
* @return {Object} obj for comparison | ||
*/ | ||
function _splitProps (keys) { | ||
function _splitProps (props) { | ||
var test = {}; | ||
if (util.isString(keys)) { | ||
keys = keys | ||
if (util.isString(props)) { | ||
props = props | ||
.split(',') | ||
.map(function(k){ | ||
return k.trim(); | ||
.map(_segment(',')) | ||
.filter(function(k){ | ||
return k; | ||
}); | ||
} | ||
if (util.isArray(keys)) { | ||
keys.forEach(function(key){ | ||
if (util.isArray(props)) { | ||
props.forEach(function(key){ | ||
test[key] = 1; | ||
@@ -372,2 +414,3 @@ }); | ||
} | ||
exports._splitProps = _splitProps; | ||
@@ -382,7 +425,7 @@ /** | ||
* pick = require('mergee').pick, | ||
* obj = { a:1, b:2, c:3, d:4 }; | ||
* r = pick(o, ['a', 'd']); | ||
* // r = { a:1, d: 4 } | ||
* r = pick(o, 'a,d'); | ||
* // r = { a:1, d: 4 } | ||
* obj = { a: 1, b: [ 1, 2 ], c: { cc:3, 'c-d':4 }, '0d': { '0d0': 5 } }; | ||
* r = pick(obj, ['a', 'b[1]', 'c["c-d"]', '0d.0d0']); | ||
* //> r = { a: 1, b: { '1': 2 }, c: { 'c-d': 4 }, '0d': { '0d0': 5 } } | ||
* r = pick(obj, 'a, b[1], c["c-d"], 0d.0d0'); | ||
* //> r = { a: 1, b: { '1': 2 }, c: { 'c-d': 4 }, '0d': { '0d0': 5 } } | ||
* ``` | ||
@@ -403,3 +446,3 @@ * | ||
for (key in test) { | ||
if ((val = get(obj, key)) != undefined) { | ||
if ((val = get(obj, key)) != undefined) { // jshint ignore:line | ||
set(out, key, val); | ||
@@ -421,11 +464,11 @@ } | ||
* omit = require('mergee').omit, | ||
* obj = { a:1, b:2, c:3, d:4 }; | ||
* r = omit(o, ['a', 'd']); | ||
* // r = { b:2, c: 3 } | ||
* r = omit(o, 'a,d'); | ||
* // r = { b:2, c: 3 } | ||
* obj = { a: 1, b: [ 1, 2 ], c: { cc:3, 'c-d':4 }, '0d': { '0d0': 5 } }; | ||
* r = omit(obj, ['a', 'b[1]', 'c["c-d"]', '0d.0d0']); | ||
* // r = { b: [ 1, ], c: { cc: 3 }, '0d': {} } | ||
* r = omit(obj, 'a, b[1], c["c-d"], 0d.0d0'); | ||
* // r = { b: [ 1, ], c: { cc: 3 }, '0d': {} } | ||
* ``` | ||
* | ||
* @param {Object} obj - object | ||
* @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 omitted properties | ||
@@ -453,2 +496,3 @@ */ | ||
* | ||
* | ||
* #### Example | ||
@@ -458,9 +502,9 @@ * | ||
* var r, | ||
* select = require('mergee').select, | ||
* get = require('mergee').get, | ||
* obj = { a: { b: { c: 1 } } }; | ||
* r = select(obj, [a, b, c]); | ||
* r = get(obj, ['a', 'b', 'c']); | ||
* // r = 1 | ||
* r = select(obj, 'a.b.c'); | ||
* // r = 1 | ||
* r = select(obj, 'there.is.no.such.property'); // this will not throw! | ||
* r = get(obj, 'a.b'); | ||
* // r = { c: 1 } | ||
* r = get(obj, 'there.is.no.such.property'); // this will not throw! | ||
* // r = undefined | ||
@@ -470,3 +514,3 @@ * ``` | ||
* @param {Object} obj - object to select from | ||
* @param {Array|String} keys - Array of properties or dot separated string of properties | ||
* @param {Array|String} keys - Array of properties or dot separated string of properties; If using a String avoid using property names containing a `.` | ||
* @return {Object} selected object | ||
@@ -497,8 +541,30 @@ */ | ||
exports.get = get; | ||
/** | ||
* select properties from `obj` | ||
* same as `get` - maintained for compatibility reasons | ||
*/ | ||
exports.select = get; | ||
/** | ||
* set a property in `obj` | ||
* | ||
* #### Example | ||
* | ||
* ```js | ||
* var r, | ||
* set = require('mergee').set, | ||
* obj = {}; | ||
* r = set(obj, ['a', 'b'], { c:1 }); | ||
* //> r = { a: { b: { c: 1 } } } | ||
* r = set(obj, 'a.b.d', 2); | ||
* //> r = { a: { b: { c:1, d:2 } } } | ||
* ``` | ||
* | ||
* @param {Object} obj - object to select from | ||
* @param {Array|String} keys - Array of properties or dot separated string of properties; If using a String avoid using property names containing a `.` | ||
* @param {Any} value - The value to set | ||
* @return {Object} set object | ||
*/ | ||
function set(obj, keys, value, opts) { | ||
function set(obj, keys, value) { | ||
var i, | ||
@@ -509,3 +575,2 @@ key, | ||
opts = opts || {}; | ||
keys = _splitPath(keys); | ||
@@ -512,0 +577,0 @@ |
{ | ||
"name": "mergee", | ||
"description": "Utilities for objects", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "engines": { |
@@ -29,5 +29,6 @@ # mergee | ||
* [clone(obj)](#cloneobj) | ||
* [pick(obj, keys)](#pickobj-keys) | ||
* [omit(obj, keys)](#omitobj-keys) | ||
* [select(obj, keys)](#selectobj-keys) | ||
* [pick(obj, props)](#pickobj-props) | ||
* [omit(obj, props)](#omitobj-props) | ||
* [get(obj, keys)](#getobj-keys) | ||
* [set(obj, keys, value)](#setobj-keys-value) | ||
* [isCircular(obj)](#iscircularobj) | ||
@@ -154,3 +155,3 @@ * [deepEqual(actual, expected)](#deepequalactual-expected) | ||
### pick(obj, keys) | ||
### pick(obj, props) | ||
@@ -164,7 +165,7 @@ pick properties from `obj` into a new object | ||
pick = require('mergee').pick, | ||
obj = { a:1, b:2, c:3, d:4 }; | ||
r = pick(o, ['a', 'd']); | ||
// r = { a:1, d: 4 } | ||
r = pick(o, 'a,d'); | ||
// r = { a:1, d: 4 } | ||
obj = { a: 1, b: [ 1, 2 ], c: { cc:3, 'c-d':4 }, '0d': { '0d0': 5 } }; | ||
r = pick(obj, ['a', 'b[1]', 'c["c-d"]', '0d.0d0']); | ||
//> r = { a: 1, b: { '1': 2 }, c: { 'c-d': 4 }, '0d': { '0d0': 5 } } | ||
r = pick(obj, 'a, b[1], c["c-d"], 0d.0d0'); | ||
//> r = { a: 1, b: { '1': 2 }, c: { 'c-d': 4 }, '0d': { '0d0': 5 } } | ||
``` | ||
@@ -176,3 +177,3 @@ | ||
**keys**: `Array | String`, Array of properties or comma separated string of properties | ||
**props**: `Array | String`, Array of properties or comma separated string of properties | ||
@@ -182,3 +183,3 @@ **Returns**: `Object`, object with picked properties | ||
### omit(obj, keys) | ||
### omit(obj, props) | ||
@@ -192,7 +193,7 @@ omit properties from `obj` into a new object | ||
omit = require('mergee').omit, | ||
obj = { a:1, b:2, c:3, d:4 }; | ||
r = omit(o, ['a', 'd']); | ||
// r = { b:2, c: 3 } | ||
r = omit(o, 'a,d'); | ||
// r = { b:2, c: 3 } | ||
obj = { a: 1, b: [ 1, 2 ], c: { cc:3, 'c-d':4 }, '0d': { '0d0': 5 } }; | ||
r = omit(obj, ['a', 'b[1]', 'c["c-d"]', '0d.0d0']); | ||
// r = { b: [ 1, ], c: { cc: 3 }, '0d': {} } | ||
r = omit(obj, 'a, b[1], c["c-d"], 0d.0d0'); | ||
// r = { b: [ 1, ], c: { cc: 3 }, '0d': {} } | ||
``` | ||
@@ -204,3 +205,3 @@ | ||
**keys**: `Array | String`, Array of properties or comma separated string of properties | ||
**props**: `Array | String`, Array of properties or comma separated string of properties | ||
@@ -210,6 +211,7 @@ **Returns**: `Object`, object with omitted properties | ||
### select(obj, keys) | ||
### get(obj, keys) | ||
select properties from `obj` | ||
get properties from `obj` | ||
#### Example | ||
@@ -219,9 +221,9 @@ | ||
var r, | ||
select = require('mergee').select, | ||
get = require('mergee').get, | ||
obj = { a: { b: { c: 1 } } }; | ||
r = select(obj, [a, b, c]); | ||
r = get(obj, ['a', 'b', 'c']); | ||
// r = 1 | ||
r = select(obj, 'a.b.c'); | ||
// r = 1 | ||
r = select(obj, 'there.is.no.such.property'); // this will not throw! | ||
r = get(obj, 'a.b'); | ||
// r = { c: 1 } | ||
r = get(obj, 'there.is.no.such.property'); // this will not throw! | ||
// r = undefined | ||
@@ -234,3 +236,3 @@ ``` | ||
**keys**: `Array | String`, Array of properties or dot separated string of properties | ||
**keys**: `Array | String`, Array of properties or dot separated string of properties; If using a String avoid using property names containing a `.` | ||
@@ -240,2 +242,29 @@ **Returns**: `Object`, selected object | ||
### set(obj, keys, value) | ||
set a property in `obj` | ||
#### Example | ||
```js | ||
var r, | ||
set = require('mergee').set, | ||
obj = {}; | ||
r = set(obj, ['a', 'b'], { c:1 }); | ||
//> r = { a: { b: { c: 1 } } } | ||
r = set(obj, 'a.b.d', 2); | ||
//> r = { a: { b: { c:1, d:2 } } } | ||
``` | ||
**Parameters** | ||
**obj**: `Object`, object to select from | ||
**keys**: `Array | String`, Array of properties or dot separated string of properties; If using a String avoid using property names containing a `.` | ||
**value**: `Any`, The value to set | ||
**Returns**: `Object`, set object | ||
### isCircular(obj) | ||
@@ -242,0 +271,0 @@ |
@@ -12,2 +12,23 @@ 'use strict'; | ||
describe('#_splitProps', function(){ | ||
it('string ',function(){ | ||
var res = M._splitProps('a, "b,0", c["d.a,e.a,f.a"]'); | ||
var exp = { a: 1, 'b,0': 1, 'c["d.a,e.a,f.a"]': 1 }; | ||
assert.deepEqual(res, exp); | ||
}); | ||
}); | ||
describe('#_splitPath', function(){ | ||
it('simple string ',function(){ | ||
var res = M._splitPath('a."b".c["d"]'); | ||
var exp = [ 'a', 'b', 'c', 'd' ]; | ||
assert.deepEqual(res, exp); | ||
}); | ||
it('complex string ',function(){ | ||
var res = M._splitPath('a."b.0".c["d.e.f"]'); | ||
var exp = [ 'a', 'b.0', 'c', 'd.e.f' ]; | ||
assert.deepEqual(res, exp); | ||
}); | ||
}); | ||
describe('#isCircular', function(){ | ||
@@ -380,5 +401,5 @@ it('circular object', function(){ | ||
var cloned = M.clone(obj); | ||
assert.ok(cloned !== obj) | ||
assert.ok(cloned.a !== obj.a) | ||
assert.ok(cloned.a.b !== obj.a.b) | ||
assert.ok(cloned !== obj); | ||
assert.ok(cloned.a !== obj.a); | ||
assert.ok(cloned.a.b !== obj.a.b); | ||
}); | ||
@@ -535,6 +556,6 @@ | ||
describe('#select', function(){ | ||
describe('#get', function(){ | ||
it('from a null object', function(){ | ||
var obj = null; | ||
var res = M.select(obj, ["test", "test"] ); | ||
var res = M.get(obj, ["test", "test"] ); | ||
assert.deepEqual(res, undefined); | ||
@@ -546,3 +567,3 @@ }); | ||
}; | ||
var res = M.select(obj); | ||
var res = M.get(obj); | ||
assert.deepEqual(res, undefined); | ||
@@ -559,3 +580,3 @@ }); | ||
}; | ||
var res = M.select(obj, 'test.test' ); | ||
var res = M.get(obj, 'test.test' ); | ||
assert.deepEqual(res, {test: { a: 1 }}); | ||
@@ -570,3 +591,3 @@ }); | ||
}; | ||
var res = M.select(obj, ["test", "test"] ); | ||
var res = M.get(obj, ["test", "test"] ); | ||
@@ -582,3 +603,3 @@ assert.deepEqual(res, 0); | ||
}; | ||
var res = M.select(obj, 'there.is.no.such.prop' ); | ||
var res = M.get(obj, 'there.is.no.such.prop' ); | ||
@@ -589,2 +610,22 @@ assert.deepEqual(res, undefined); | ||
describe('#set', function(){ | ||
it('set properties', function(){ | ||
var res = M.set({}, 'a."b.0".c["d.e.f"]', 1); | ||
var exp = {"a":{"b.0":{"c":{"d.e.f":1}}}}; | ||
assert.deepEqual(res, exp); | ||
}); | ||
it('delete a property', function(){ | ||
var obj = { a: { b: {c:1} } }; | ||
var res = M.set(obj, 'a.b', null); | ||
var exp = { a: {} }; | ||
assert.deepEqual(res, exp); | ||
}); | ||
it('append', function(){ | ||
var obj = { a: { b: {c:1} } }; | ||
var res = M.set(obj, 'a.b.d', 2); | ||
var exp = { a: { b: {c:1, d:2} } }; | ||
assert.deepEqual(res, exp); | ||
}); | ||
}); | ||
describe('#util', function(){ | ||
@@ -595,1 +636,2 @@ it('isObject', function(){ | ||
}); | ||
45825
1335
317