[Pkg-javascript-commits] [node-deep-extend] 01/02: Imported Upstream version 0.4.1

Thorsten Alteholz alteholz at moszumanska.debian.org
Sat Feb 27 14:03:33 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-deep-extend.

commit db3e51c516017d89a6b9a1febaddd7fd98affabb
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Sat Feb 27 15:03:25 2016 +0100

    Imported Upstream version 0.4.1
---
 .editorconfig      |  11 +++
 .gitignore         |   1 +
 CHANGELOG.md       |  21 +++++
 LICENSE            |  20 +++++
 README.md          |  90 ++++++++++++++++++++++
 index.js           |   1 +
 lib/deep-extend.js | 144 +++++++++++++++++++++++++++++++++++
 package.json       |  59 ++++++++++++++
 test/index.spec.js | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/mocha.opts    |   1 +
 10 files changed, 568 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..dda3939
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,11 @@
+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
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..f3efe0b
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,21 @@
+Changelog
+=========
+
+v0.4.1
+------
+
+- Removed test code from <b>npm</b> 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.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..acc4662
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,20 @@
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..cc17c9c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,90 @@
+Deep Extend
+===========
+
+Recursive object extending.
+
+[![NPM](https://nodei.co/npm/deep-extend.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/deep-extend/)
+[![NPM](https://nodei.co/npm-dl/deep-extend.png?height=3)](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)
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..762d81e
--- /dev/null
+++ b/index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/deep-extend');
diff --git a/lib/deep-extend.js b/lib/deep-extend.js
new file mode 100644
index 0000000..522461d
--- /dev/null
+++ b/lib/deep-extend.js
@@ -0,0 +1,144 @@
+/*!
+ * @description Recursive object extending
+ * @author Viacheslav Lotsmanov <lotsmanov89 at gmail.com>
+ * @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;
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..bc11816
--- /dev/null
+++ b/package.json
@@ -0,0 +1,59 @@
+{
+  "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 <lotsmanov89 at gmail.com>",
+  "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"
+  ]
+}
diff --git a/test/index.spec.js b/test/index.spec.js
new file mode 100644
index 0000000..9746043
--- /dev/null
+++ b/test/index.spec.js
@@ -0,0 +1,220 @@
+'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
+		});
+	});
+
+});
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 0000000..5ada47b
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1 @@
+--reporter spec

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-deep-extend.git



More information about the Pkg-javascript-commits mailing list