deep-extend
Advanced tools
Comparing version 0.3.3 to 0.4.0
92
index.js
@@ -1,91 +0,1 @@ | ||
/*! | ||
* Node.JS module "Deep Extend" | ||
* @description Recursive object extending. | ||
* @author Viacheslav Lotsmanov (unclechu) <lotsmanov89@gmail.com> | ||
* @license MIT | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2013 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. | ||
*/ | ||
/** | ||
* 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 object, simply use that: | ||
* deepExtend({}, yourObj_1, [yourObj_N]) - first arg is new empty object | ||
*/ | ||
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 key, val, src, clone, tmpBuf; | ||
args.forEach(function (obj) { | ||
if (typeof obj !== 'object') return; | ||
for (key in obj) { | ||
src = target[key]; | ||
val = obj[key]; | ||
if (val === target) continue; | ||
if (typeof val !== 'object' || val === null) { | ||
target[key] = val; | ||
continue; | ||
} else if (val instanceof Buffer) { | ||
tmpBuf = new Buffer(val.length); | ||
val.copy(tmpBuf); | ||
target[key] = tmpBuf; | ||
continue; | ||
} else if (val instanceof Date) { | ||
target[key] = new Date(val.getTime()); | ||
continue; | ||
} else if (val instanceof RegExp) { | ||
target[key] = new RegExp(val); | ||
continue; | ||
} | ||
if (typeof src !== 'object' || src === null) { | ||
clone = (Array.isArray(val)) ? [] : {}; | ||
target[key] = deepExtend(clone, val); | ||
continue; | ||
} | ||
if (Array.isArray(val)) { | ||
clone = (Array.isArray(src)) ? src : []; | ||
} else { | ||
clone = (!Array.isArray(src)) ? src : {}; | ||
} | ||
target[key] = deepExtend(clone, val); | ||
} | ||
}); | ||
return target; | ||
} | ||
module.exports = require('./lib/deep-extend'); |
{ | ||
"name": "deep-extend", | ||
"description": "Recursive object extending.", | ||
"description": "Recursive object extending", | ||
"license": "MIT", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/unclechu/node-deep-extend", | ||
@@ -39,6 +39,6 @@ "keywords": [ | ||
], | ||
"main": "index", | ||
"main": "lib/deep-extend.js", | ||
"engines": { | ||
"node": ">=0.4", | ||
"iojs": ">=1" | ||
"node": ">=0.4.0", | ||
"iojs": ">=1.0.0" | ||
}, | ||
@@ -53,5 +53,7 @@ "scripts": { | ||
"directories": { | ||
"lib": "./lib/", | ||
"test": "./test/" | ||
}, | ||
"files": [ | ||
"lib/deep-extend.js", | ||
"test/index.spec.js", | ||
@@ -58,0 +60,0 @@ "test/mocha.opts", |
@@ -1,3 +0,3 @@ | ||
Node.JS module “Deep Extend” | ||
============================ | ||
Deep Extend | ||
=========== | ||
@@ -10,5 +10,7 @@ Recursive object extending. | ||
Install | ||
----- | ||
------- | ||
npm install deep-extend | ||
```bash | ||
$ npm install deep-extend | ||
``` | ||
@@ -18,2 +20,3 @@ Usage | ||
```javascript | ||
var deepExtend = require('deep-extend'); | ||
@@ -29,3 +32,5 @@ var obj1 = { | ||
f: 5, | ||
g: 123 | ||
g: 123, | ||
i: 321, | ||
j: [1, 2] | ||
}; | ||
@@ -43,3 +48,4 @@ var obj2 = { | ||
h: /abc/g, | ||
f: null | ||
i: null, | ||
j: [3, 4] | ||
}; | ||
@@ -61,3 +67,28 @@ | ||
e: { one: 1, two: 2 }, | ||
h: /abc/g } | ||
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) |
@@ -0,3 +1,5 @@ | ||
'use strict'; | ||
var should = require('should'); | ||
var extend = require('../index'); | ||
var extend = require('../index'); // it must be ./lib/deep-extend.js | ||
@@ -91,3 +93,5 @@ describe('deep-extend', function () { | ||
f: 5, | ||
g: 123 | ||
g: 123, | ||
i: 321, | ||
j: [1, 2] | ||
}; | ||
@@ -105,3 +109,4 @@ var obj2 = { | ||
h: /abc/g, | ||
f: null | ||
i: null, | ||
j: [3, 4] | ||
}; | ||
@@ -119,7 +124,9 @@ | ||
}, | ||
f: null, | ||
f: [], | ||
g: undefined, | ||
c: 5, | ||
e: { one: 1, two: 2 }, | ||
h: /abc/g | ||
h: /abc/g, | ||
i: null, | ||
j: [3, 4] | ||
}); | ||
@@ -131,2 +138,87 @@ | ||
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 | ||
}); | ||
}); | ||
}); |
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
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
12833
8
323
90
1