pax_global_header 0000666 0000000 0000000 00000000064 12646546622 0014527 g ustar 00root root 0000000 0000000 52 comment=08e39356bba769744c669eb219a31fee07decd19
node-deep-extend-0.4.1/ 0000775 0000000 0000000 00000000000 12646546622 0014656 5 ustar 00root root 0000000 0000000 node-deep-extend-0.4.1/.editorconfig 0000664 0000000 0000000 00000000246 12646546622 0017335 0 ustar 00root root 0000000 0000000 root = true
[*]
trim_trailing_whitespace = true
indent_style = tab
end_of_line = lf
insert_final_newline = true
[package.json]
indent_style = space
indent_size = 2
node-deep-extend-0.4.1/.gitignore 0000664 0000000 0000000 00000000015 12646546622 0016642 0 ustar 00root root 0000000 0000000 node_modules
node-deep-extend-0.4.1/CHANGELOG.md 0000664 0000000 0000000 00000000754 12646546622 0016475 0 ustar 00root root 0000000 0000000 Changelog
=========
v0.4.1
------
- Removed test code from npm package
([see pull request #21](https://github.com/unclechu/node-deep-extend/pull/21));
- Increased minimal version of Node from 0.4.0 to 0.12.0
(because can't run tests on lesser version anyway).
v0.4.0
------
Broken backward compatibility with v0.3.x
- Fixed bug with extending arrays instead of cloning;
- Deep cloning for arrays;
- Check for own property;
- Fixed some documentation issues;
- Strict JS mode.
node-deep-extend-0.4.1/LICENSE 0000664 0000000 0000000 00000002105 12646546622 0015661 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2013-2015, Viacheslav Lotsmanov
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
node-deep-extend-0.4.1/README.md 0000664 0000000 0000000 00000002337 12646546622 0016142 0 ustar 00root root 0000000 0000000 Deep Extend
===========
Recursive object extending.
[](https://nodei.co/npm/deep-extend/)
[](https://nodei.co/npm/deep-extend/)
Install
-------
```bash
$ npm install deep-extend
```
Usage
-----
```javascript
var deepExtend = require('deep-extend');
var obj1 = {
a: 1,
b: 2,
d: {
a: 1,
b: [],
c: { test1: 123, test2: 321 }
},
f: 5,
g: 123,
i: 321,
j: [1, 2]
};
var obj2 = {
b: 3,
c: 5,
d: {
b: { first: 'one', second: 'two' },
c: { test2: 222 }
},
e: { one: 1, two: 2 },
f: [],
g: (void 0),
h: /abc/g,
i: null,
j: [3, 4]
};
deepExtend(obj1, obj2);
console.log(obj1);
/*
{ a: 1,
b: 3,
d:
{ a: 1,
b: { first: 'one', second: 'two' },
c: { test1: 123, test2: 222 } },
f: null,
g: undefined,
c: 5,
e: { one: 1, two: 2 },
h: /abc/g,
i: null,
j: [3, 4] }
*/
```
Unit testing
------------
```bash
$ npm test
```
Changelog
---------
[CHANGELOG.md](./CHANGELOG.md)
Any issues?
-----------
Please, report about issues
[here](https://github.com/unclechu/node-deep-extend/issues).
License
-------
[MIT](./LICENSE)
node-deep-extend-0.4.1/index.js 0000664 0000000 0000000 00000000057 12646546622 0016325 0 ustar 00root root 0000000 0000000 module.exports = require('./lib/deep-extend');
node-deep-extend-0.4.1/lib/ 0000775 0000000 0000000 00000000000 12646546622 0015424 5 ustar 00root root 0000000 0000000 node-deep-extend-0.4.1/lib/deep-extend.js 0000664 0000000 0000000 00000007744 12646546622 0020200 0 ustar 00root root 0000000 0000000 /*!
* @description Recursive object extending
* @author Viacheslav Lotsmanov
* @license MIT
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2015 Viacheslav Lotsmanov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
'use strict';
function isSpecificValue(val) {
return (
val instanceof Buffer
|| val instanceof Date
|| val instanceof RegExp
) ? true : false;
}
function cloneSpecificValue(val) {
if (val instanceof Buffer) {
var x = new Buffer(val.length);
val.copy(x);
return x;
} else if (val instanceof Date) {
return new Date(val.getTime());
} else if (val instanceof RegExp) {
return new RegExp(val);
} else {
throw new Error('Unexpected situation');
}
}
/**
* Recursive cloning array.
*/
function deepCloneArray(arr) {
var clone = [];
arr.forEach(function (item, index) {
if (typeof item === 'object' && item !== null) {
if (Array.isArray(item)) {
clone[index] = deepCloneArray(item);
} else if (isSpecificValue(item)) {
clone[index] = cloneSpecificValue(item);
} else {
clone[index] = deepExtend({}, item);
}
} else {
clone[index] = item;
}
});
return clone;
}
/**
* Extening object that entered in first argument.
*
* Returns extended object or false if have no target object or incorrect type.
*
* If you wish to clone source object (without modify it), just use empty new
* object as first argument, like this:
* deepExtend({}, yourObj_1, [yourObj_N]);
*/
var deepExtend = module.exports = function (/*obj_1, [obj_2], [obj_N]*/) {
if (arguments.length < 1 || typeof arguments[0] !== 'object') {
return false;
}
if (arguments.length < 2) {
return arguments[0];
}
var target = arguments[0];
// convert arguments to array and cut off target object
var args = Array.prototype.slice.call(arguments, 1);
var val, src, clone;
args.forEach(function (obj) {
// skip argument if it is array or isn't object
if (typeof obj !== 'object' || Array.isArray(obj)) {
return;
}
Object.keys(obj).forEach(function (key) {
src = target[key]; // source value
val = obj[key]; // new value
// recursion prevention
if (val === target) {
return;
/**
* if new value isn't object then just overwrite by new value
* instead of extending.
*/
} else if (typeof val !== 'object' || val === null) {
target[key] = val;
return;
// just clone arrays (and recursive clone objects inside)
} else if (Array.isArray(val)) {
target[key] = deepCloneArray(val);
return;
// custom cloning and overwrite for specific objects
} else if (isSpecificValue(val)) {
target[key] = cloneSpecificValue(val);
return;
// overwrite by new value if source isn't object or array
} else if (typeof src !== 'object' || src === null || Array.isArray(src)) {
target[key] = deepExtend({}, val);
return;
// source value and new value is objects both, extending...
} else {
target[key] = deepExtend(src, val);
return;
}
});
});
return target;
}
node-deep-extend-0.4.1/package.json 0000664 0000000 0000000 00000002326 12646546622 0017147 0 ustar 00root root 0000000 0000000 {
"name": "deep-extend",
"description": "Recursive object extending",
"license": "MIT",
"version": "0.4.1",
"homepage": "https://github.com/unclechu/node-deep-extend",
"keywords": [
"deep-extend",
"extend",
"deep",
"recursive",
"xtend",
"clone",
"merge",
"json"
],
"licenses": [
{
"type": "MIT",
"url": "https://raw.githubusercontent.com/unclechu/node-deep-extend/master/LICENSE"
}
],
"repository": {
"type": "git",
"url": "git://github.com/unclechu/node-deep-extend.git"
},
"author": "Viacheslav Lotsmanov ",
"bugs": "https://github.com/unclechu/node-deep-extend/issues",
"contributors": [
{
"name": "Romain Prieto",
"url": "https://github.com/rprieto"
},
{
"name": "Max Maximov",
"url": "https://github.com/maxmaximov"
}
],
"main": "lib/deep-extend.js",
"engines": {
"node": ">=0.12.0",
"iojs": ">=1.0.0"
},
"scripts": {
"test": "./node_modules/.bin/mocha"
},
"devDependencies": {
"mocha": "^2.2.1",
"should": "^5.2.0"
},
"directories": {
"lib": "./lib/",
"test": "./test/"
},
"files": [
"lib/deep-extend.js",
"index.js"
]
}
node-deep-extend-0.4.1/test/ 0000775 0000000 0000000 00000000000 12646546622 0015635 5 ustar 00root root 0000000 0000000 node-deep-extend-0.4.1/test/index.spec.js 0000664 0000000 0000000 00000011262 12646546622 0020235 0 ustar 00root root 0000000 0000000 'use strict';
var should = require('should');
var extend = require('../index'); // it must be ./lib/deep-extend.js
describe('deep-extend', function () {
it('can extend on 1 level', function () {
var a = { hello: 1 };
var b = { world: 2 };
extend(a, b);
a.should.eql({
hello: 1,
world: 2
});
});
it('can extend on 2 levels', function () {
var a = { person: { name: 'John' } };
var b = { person: { age: 30 } };
extend(a, b);
a.should.eql({
person: { name: 'John', age: 30 }
});
});
it('can extend with Buffer values', function () {
var a = { hello: 1 };
var b = { value: new Buffer('world') };
extend(a, b);
a.should.eql({
hello: 1,
value: new Buffer('world')
});
});
it('Buffer is cloned', function () {
var a = { };
var b = { value: new Buffer('foo') };
extend(a, b);
a.value.write('bar');
a.value.toString().should.eql('bar');
b.value.toString().should.eql('foo');
});
it('Date objects', function () {
var a = { d: new Date() };
var b = extend({}, a);
b.d.should.instanceOf(Date);
});
it('Date object is cloned', function () {
var a = { d: new Date() };
var b = extend({}, a);
b.d.setTime( (new Date()).getTime() + 100000 );
b.d.getTime().should.not.eql( a.d.getTime() );
});
it('RegExp objects', function () {
var a = { d: new RegExp() };
var b = extend({}, a);
b.d.should.instanceOf(RegExp);
});
it('RegExp object is cloned', function () {
var a = { d: new RegExp('b', 'g') };
var b = extend({}, a);
b.d.test('abc');
b.d.lastIndex.should.not.eql( a.d.lastIndex );
});
it('doesn\'t change sources', function () {
var a = {a: [1]};
var b = {a: [2]};
var c = {c: 3};
var d = extend({}, a, b, c);
a.should.eql({a: [1]});
b.should.eql({a: [2]});
c.should.eql({c: 3});
});
it('example from README.md', function () {
var obj1 = {
a: 1,
b: 2,
d: {
a: 1,
b: [],
c: { test1: 123, test2: 321 }
},
f: 5,
g: 123,
i: 321,
j: [1, 2]
};
var obj2 = {
b: 3,
c: 5,
d: {
b: { first: 'one', second: 'two' },
c: { test2: 222 }
},
e: { one: 1, two: 2 },
f: [],
g: (void 0),
h: /abc/g,
i: null,
j: [3, 4]
};
extend(obj1, obj2);
obj1.should.eql({
a: 1,
b: 3,
d: {
a: 1,
b: { first: 'one', second: 'two' },
c: { test1: 123, test2: 222 }
},
f: [],
g: undefined,
c: 5,
e: { one: 1, two: 2 },
h: /abc/g,
i: null,
j: [3, 4]
});
('g' in obj1).should.eql(true);
('x' in obj1).should.eql(false);
});
it('clone arrays instead of extend', function () {
extend({a: [1, 2, 3]}, {a: [2, 3]}).should.eql({a: [2, 3]});
});
it('recursive clone objects and special objects in cloned arrays', function () {
var obj1 = {
x: 1,
y: new Buffer('foo')
};
var b = new Buffer('bar');
var obj2 = {
x: 1,
y: [2, 4, obj1, b],
z: new Buffer('test')
};
var foo = {
a: [obj2, obj2]
};
var bar = extend({}, foo);
bar.a[0].x = 2;
bar.a[0].z.write('text', 'utf-8');
bar.a[1].x = 3;
bar.a[1].z.write('lel', 'utf-8');
bar.a[0].y[0] = 3;
bar.a[0].y[2].x = 5;
bar.a[0].y[2].y.write('heh', 'utf-8');
bar.a[0].y[3].write('ho', 'utf-8');
bar.a[1].y[1] = 3;
bar.a[1].y[2].y.write('nah', 'utf-8');
bar.a[1].y[3].write('he', 'utf-8');
obj2.x.should.eql(1);
obj2.z.toString().should.eql('test');
bar.a[0].x.should.eql(2);
bar.a[0].z.toString().should.eql('text');
bar.a[1].x.should.eql(3);
bar.a[1].z.toString().should.eql('lelt');
obj1.x.should.eql(1);
obj1.y.toString().should.eql('foo');
b.toString().should.eql('bar');
bar.a[0].y[0].should.eql(3);
bar.a[0].y[1].should.eql(4);
bar.a[0].y[2].x.should.eql(5);
bar.a[0].y[2].y.toString().should.eql('heh');
bar.a[0].y[3].toString().should.eql('hor');
bar.a[1].y[0].should.eql(2);
bar.a[1].y[1].should.eql(3);
bar.a[1].y[2].x.should.eql(1);
bar.a[1].y[2].y.toString().should.eql('nah');
bar.a[1].y[3].toString().should.eql('her');
foo.a.length.should.eql(2);
bar.a.length.should.eql(2);
Object.keys(obj2).should.eql(['x', 'y', 'z']);
Object.keys(bar.a[0]).should.eql(['x', 'y', 'z']);
Object.keys(bar.a[1]).should.eql(['x', 'y', 'z']);
obj2.y.length.should.eql(4);
bar.a[0].y.length.should.eql(4);
bar.a[1].y.length.should.eql(4);
Object.keys(obj2.y[2]).should.eql(['x', 'y']);
Object.keys(bar.a[0].y[2]).should.eql(['x', 'y']);
Object.keys(bar.a[1].y[2]).should.eql(['x', 'y']);
});
it('checking keys for hasOwnPrototype', function () {
var A = function () {
this.x = 1;
this.y = 2;
};
A.prototype.z = 3;
var foo = new A();
extend({x: 123}, foo).should.eql({
x: 1,
y: 2
});
foo.z = 5;
extend({x: 123}, foo, {y: 22}).should.eql({
x: 1,
y: 22,
z: 5
});
});
});
node-deep-extend-0.4.1/test/mocha.opts 0000664 0000000 0000000 00000000020 12646546622 0017623 0 ustar 00root root 0000000 0000000 --reporter spec