pax_global_header00006660000000000000000000000064133010561620014507gustar00rootroot0000000000000052 comment=f3f2b4f30fffe8abc9a99a7d6469fb354ca206e9 node-deep-extend-0.6.0/000077500000000000000000000000001330105616200146375ustar00rootroot00000000000000node-deep-extend-0.6.0/.editorconfig000066400000000000000000000003311330105616200173110ustar00rootroot00000000000000root = true [*] trim_trailing_whitespace = true indent_style = tab end_of_line = lf insert_final_newline = true max_line_length = 100 [package.json] indent_style = space indent_size = 2 [*.md] max_line_length = 80 node-deep-extend-0.6.0/.gitignore000066400000000000000000000000371330105616200166270ustar00rootroot00000000000000node_modules package-lock.json node-deep-extend-0.6.0/.travis.yml000066400000000000000000000004001330105616200167420ustar00rootroot00000000000000language: node_js cache: yarn node_js: - "node" - "10" - "9" - "8" - "7" - "6" - "5" - "4" - "4.0.0" # minimal supported version before_install: - npm config set strict-ssl false - npm config set registry="http://registry.npmjs.org/" node-deep-extend-0.6.0/CHANGELOG.md000066400000000000000000000022031330105616200164450ustar00rootroot00000000000000Changelog ========= v0.6.0 ------ - Updated "devDependencies" versions to fix vulnerability alerts - Dropped support of io.js and node.js v0.12.x and lower since new versions of "devDependencies" couldn't work with those old node.js versions (minimal supported version of node.js now is v4.0.0) v0.5.1 ------ - Fix prototype pollution vulnerability (thanks to @mwakerman for the PR) - Avoid using deprecated Buffer API (thanks to @ChALkeR for the PR) v0.5.0 ------ - Auto-testing provided by Travis CI; - Support older Node.JS versions (`v0.11.x` and `v0.10.x`); - Removed tests files from npm package. v0.4.2 ------ - Fix for `null` as an argument. 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 ------ - **WARNING!** 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.6.0/LICENSE000066400000000000000000000021051330105616200156420ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2013-2018, 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.6.0/README.md000066400000000000000000000024121330105616200161150ustar00rootroot00000000000000Deep Extend =========== Recursive object extending. [![Build Status](https://api.travis-ci.org/unclechu/node-deep-extend.svg?branch=master)](https://travis-ci.org/unclechu/node-deep-extend) [![NPM](https://nodei.co/npm/deep-extend.png?downloads=true&downloadRank=true&stars=true)](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: [], 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.6.0/index.js000066400000000000000000000000571330105616200163060ustar00rootroot00000000000000module.exports = require('./lib/deep-extend'); node-deep-extend-0.6.0/lib/000077500000000000000000000000001330105616200154055ustar00rootroot00000000000000node-deep-extend-0.6.0/lib/deep-extend.js000066400000000000000000000103051330105616200201440ustar00rootroot00000000000000/*! * @description Recursive object extending * @author Viacheslav Lotsmanov * @license MIT * * The MIT License (MIT) * * Copyright (c) 2013-2018 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 = Buffer.alloc ? Buffer.alloc(val.length) : 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; } function safeGetProperty(object, property) { return property === '__proto__' ? undefined : object[property]; } /** * 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 isn't an object, is null, or is an array if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) { return; } Object.keys(obj).forEach(function (key) { src = safeGetProperty(target, key); // source value val = safeGetProperty(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.6.0/package.json000066400000000000000000000024401330105616200171250ustar00rootroot00000000000000{ "name": "deep-extend", "description": "Recursive object extending", "license": "MIT", "version": "0.6.0", "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" }, { "name": "Marshall Bowers", "url": "https://github.com/maxdeviant" }, { "name": "Misha Wakerman", "url": "https://github.com/mwakerman" } ], "main": "lib/deep-extend.js", "engines": { "node": ">=4.0.0" }, "scripts": { "test": "./node_modules/.bin/mocha" }, "devDependencies": { "mocha": "5.2.0", "should": "13.2.1" }, "files": [ "index.js", "lib/" ] } node-deep-extend-0.6.0/test/000077500000000000000000000000001330105616200156165ustar00rootroot00000000000000node-deep-extend-0.6.0/test/index.spec.js000066400000000000000000000140751330105616200202230ustar00rootroot00000000000000'use strict'; var should = require('should'); var extend = require('../index'); // it must be ./lib/deep-extend.js describe('deep-extend', function () { it('should ignore undefined', function () { var a = { hello: 1 }; var b = undefined; extend(a, b); a.should.eql({ hello: 1 }); }); it('should ignore null', function () { var a = { hello: 1 }; var b = null; extend(a, b); a.should.eql({ hello: 1 }); }); 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 }); }); describe('issue #33', function () { it('correct usage (cloning)', function () { var sharedObject = {foo: 'zero'}; var objDef = {bar: sharedObject, baz: sharedObject}; var obj = {bar: {foo: 'one'}, baz: {foo: 'two'}}; obj = extend({}, {bar: objDef.bar, baz: objDef.baz}, obj); obj.should.eql({bar: {foo: 'one'}, baz: {foo: 'two'}}); }); it('incorrect usage (just extending)', function () { var sharedObject = {foo: 'serif'}; var objDef = {bar: sharedObject, baz: sharedObject}; var obj = {bar: {foo: 'one'}, baz: {foo: 'two'}}; obj = extend({bar: objDef.bar, baz: objDef.baz}, obj); obj.should.eql({bar: {foo: 'two'}, baz: {foo: 'two'}}); }); }); // Vulnerability reported via hacker1: https://hackerone.com/reports/311333 // See https://github.com/unclechu/node-deep-extend/issues/39 // See https://github.com/unclechu/node-deep-extend/pull/40 it('should not modify Object prototype (hacker1 #311333)', function () { var a = {}; extend({}, JSON.parse('{"__proto__":{"oops":"It works!"}}')) should.not.exist(a.oops); should.not.exist(Object.prototype.oops); }); }); node-deep-extend-0.6.0/test/mocha.opts000066400000000000000000000000201330105616200176040ustar00rootroot00000000000000--reporter spec