[Pkg-javascript-commits] [node-extend] 01/02: Imported Upstream version 1.2.1
Leo Iannacone
l3on-guest at moszumanska.debian.org
Mon Jun 2 13:38:03 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-extend.
commit 5ea724d7ae01abb478a377364fd544638a9d3730
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Mon Jun 2 15:30:44 2014 +0200
Imported Upstream version 1.2.1
---
.npmignore | 1 +
.travis.yml | 5 +
README.md | 59 +++++++
index.js | 78 +++++++++
package.json | 31 ++++
test/index.js | 537 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 711 insertions(+)
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..30d74d2
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1 @@
+test
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..c99d400
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - "0.10"
+ - 0.8
+ - 0.6
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ddaee06
--- /dev/null
+++ b/README.md
@@ -0,0 +1,59 @@
+[![Build Status][1]][2] [![dependency status][9]][10] [![dev dependency status][11]][12]
+
+# extend() for Node.js <sup>[![Version Badge][8]][3]</sup>
+
+`node-extend` is a port of the classic extend() method from jQuery. It behaves as you expect. It is simple, tried and true.
+
+## Installation
+
+This package is available on [npm][3] as: `extend`
+
+``` sh
+npm install extend
+```
+
+## Usage
+
+**Syntax:** extend **(** [`deep`], `target`, `object1`, [`objectN`] **)**
+
+*Extend one object with one or more others, returning the modified object.*
+
+Keep in mind that the target object will be modified, and will be returned from extend().
+
+If a boolean true is specified as the first argument, extend performs a deep copy, recursively copying any objects it finds. Otherwise, the copy will share structure with the original object(s).
+Undefined properties are not copied. However, properties inherited from the object's prototype will be copied over.
+
+### Arguments
+
+* `deep` *Boolean* (optional)
+If set, the merge becomes recursive (i.e. deep copy).
+* `target` *Object*
+The object to extend.
+* `object1` *Object*
+The object that will be merged into the first.
+* `objectN` *Object* (Optional)
+More objects to merge into the first.
+
+## License
+
+`node-extend` is licensed under the [MIT License][4].
+
+## Acknowledgements
+
+All credit to the jQuery authors for perfecting this amazing utility.
+
+Ported to Node.js by [Stefan Thomas][5] with contributions by [Jonathan Buchanan][6] and [Jordan Harband][7].
+
+[1]: https://travis-ci.org/justmoon/node-extend.png
+[2]: https://travis-ci.org/justmoon/node-extend
+[3]: https://npmjs.org/package/extend
+[4]: http://opensource.org/licenses/MIT
+[5]: https://github.com/justmoon
+[6]: https://github.com/insin
+[7]: https://github.com/ljharb
+[8]: http://vb.teelaun.ch/justmoon/node-extend.svg
+[9]: https://david-dm.org/justmoon/node-extend.png
+[10]: https://david-dm.org/justmoon/node-extend
+[11]: https://david-dm.org/justmoon/node-extend/dev-status.png
+[12]: https://david-dm.org/justmoon/node-extend#info=devDependencies
+
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..be7300b
--- /dev/null
+++ b/index.js
@@ -0,0 +1,78 @@
+var hasOwn = Object.prototype.hasOwnProperty;
+var toString = Object.prototype.toString;
+
+function isPlainObject(obj) {
+ if (!obj || toString.call(obj) !== '[object Object]' || obj.nodeType || obj.setInterval)
+ return false;
+
+ var has_own_constructor = hasOwn.call(obj, 'constructor');
+ var has_is_property_of_method = hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
+ // Not own constructor property must be Object
+ if (obj.constructor && !has_own_constructor && !has_is_property_of_method)
+ return false;
+
+ // Own properties are enumerated firstly, so to speed up,
+ // if last one is own, then all properties are own.
+ var key;
+ for ( key in obj ) {}
+
+ return key === undefined || hasOwn.call( obj, key );
+};
+
+module.exports = function extend() {
+ var options, name, src, copy, copyIsArray, clone,
+ target = arguments[0] || {},
+ i = 1,
+ length = arguments.length,
+ deep = false;
+
+ // Handle a deep copy situation
+ if ( typeof target === "boolean" ) {
+ deep = target;
+ target = arguments[1] || {};
+ // skip the boolean and the target
+ i = 2;
+ }
+
+ // Handle case when target is a string or something (possible in deep copy)
+ if ( typeof target !== "object" && typeof target !== "function") {
+ target = {};
+ }
+
+ for ( ; i < length; i++ ) {
+ // Only deal with non-null/undefined values
+ if ( (options = arguments[ i ]) != null ) {
+ // Extend the base object
+ for ( name in options ) {
+ src = target[ name ];
+ copy = options[ name ];
+
+ // Prevent never-ending loop
+ if ( target === copy ) {
+ continue;
+ }
+
+ // Recurse if we're merging plain objects or arrays
+ if ( deep && copy && ( isPlainObject(copy) || (copyIsArray = Array.isArray(copy)) ) ) {
+ if ( copyIsArray ) {
+ copyIsArray = false;
+ clone = src && Array.isArray(src) ? src : [];
+
+ } else {
+ clone = src && isPlainObject(src) ? src : {};
+ }
+
+ // Never move original objects, clone them
+ target[ name ] = extend( deep, clone, copy );
+
+ // Don't bring in undefined values
+ } else if ( copy !== undefined ) {
+ target[ name ] = copy;
+ }
+ }
+ }
+ }
+
+ // Return the modified object
+ return target;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..38cb640
--- /dev/null
+++ b/package.json
@@ -0,0 +1,31 @@
+{
+ "name": "extend",
+ "author": "Stefan Thomas <justmoon at members.fsf.org> (http://www.justmoon.net)",
+ "version": "1.2.1",
+ "description": "Port of jQuery.extend for Node.js",
+ "main": "index",
+ "scripts": {
+ "test": "node test/index.js"
+ },
+ "contributors": [
+ {
+ "name": "Jordan Harband",
+ "url": "https://github.com/ljharb"
+ }
+ ],
+ "keywords": [
+ "extend",
+ "clone",
+ "merge"
+ ],
+ "repository" : {
+ "type": "git",
+ "url": "https://github.com/justmoon/node-extend.git"
+ },
+ "dependencies": {
+ },
+ "devDependencies": {
+ "tape" : "~1.1.0"
+ }
+}
+
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..1957694
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,537 @@
+var extend = require('../index');
+var test = require('tape');
+
+var str = 'me a test';
+var int = 10;
+var arr = [1, 'what', new Date(81, 8, 4)];
+var date = new Date(81, 4, 13);
+
+var obj = {
+ str: str,
+ int: int,
+ arr: arr,
+ date: date
+};
+
+var deep = {
+ ori: obj,
+ layer: {
+ int: 10,
+ str: 'str',
+ date: new Date(84, 5, 12),
+ arr: [101, 'dude', new Date(82, 10, 4)],
+ deep: {
+ str: obj.str,
+ int: int,
+ arr: obj.arr,
+ date: new Date(81, 7, 4)
+ }
+ }
+};
+
+
+test('merge string with string', function (t) {
+ var ori = 'what u gonna say';
+ var target = extend(ori, str);
+ var expectedTarget = {
+ '0': 'm',
+ '1': 'e',
+ '2': ' ',
+ '3': 'a',
+ '4': ' ',
+ '5': 't',
+ '6': 'e',
+ '7': 's',
+ '8': 't'
+ };
+
+ t.equal(ori, 'what u gonna say', 'original string 1 is unchanged');
+ t.equal(str, 'me a test', 'original string 2 is unchanged');
+ t.deepEqual(target, expectedTarget, 'string + string is merged object form of string');
+ t.end();
+});
+
+test('merge string with number', function (t) {
+ var ori = 'what u gonna say';
+ var target = extend(ori, 10);
+
+ t.equal(ori, 'what u gonna say', 'original string is unchanged');
+ t.deepEqual(target, {}, 'string + number is empty object');
+
+ t.end();
+});
+
+test('merge string with array', function (t) {
+ var ori = 'what u gonna say';
+ var target = extend(ori, arr);
+
+ t.equal(ori, 'what u gonna say', 'original string is unchanged');
+ t.deepEqual(arr, [1, 'what', new Date(81, 8, 4)], 'array is unchanged');
+ t.deepEqual(target, {
+ '0' : 1,
+ '1' : 'what',
+ '2' : new Date(81, 8, 4)
+ }, 'string + array is array');
+ t.end();
+});
+
+test('merge string with date', function (t) {
+ var ori = 'what u gonna say';
+ var target = extend(ori, date);
+
+ var testDate = new Date(81, 4, 13);
+ t.equal(ori, 'what u gonna say', 'original string is unchanged');
+ t.deepEqual(date, testDate, 'date is unchanged');
+ t.deepEqual(target, testDate, 'string + date is date');
+ t.end();
+});
+
+test('merge string with obj', function (t) {
+ var ori = 'what u gonna say';
+ var target = extend(ori, obj);
+
+ t.equal(ori, 'what u gonna say', 'original string is unchanged');
+ var testObj = {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13)
+ };
+ t.deepEqual(obj, testObj, 'original obj is unchanged');
+ t.deepEqual(target, testObj, 'string + obj is obj');
+ t.end();
+});
+
+test('merge number with string', function (t) {
+ var ori = 20;
+ var target = extend(ori, str);
+
+ t.equal(ori, 20, 'number is unchanged');
+ t.equal(str, 'me a test', 'string is unchanged');
+ t.deepEqual(target, {
+ '0': 'm',
+ '1': 'e',
+ '2': ' ',
+ '3': 'a',
+ '4': ' ',
+ '5': 't',
+ '6': 'e',
+ '7': 's',
+ '8': 't'
+ }, 'number + string is object form of string');
+ t.end();
+});
+
+test('merge number with number', function (t) {
+ t.deepEqual(extend(20, 10), {}, 'number + number is empty object');
+ t.end();
+});
+
+test('merge number with array', function (t) {
+ var target = extend(20, arr);
+
+ t.deepEqual(arr, [1, 'what', new Date(81, 8, 4)], 'array is unchanged');
+ t.deepEqual(target, {
+ '0': 1,
+ '1': 'what',
+ '2': new Date(81, 8, 4)
+ }, 'number + arr is object with array contents');
+ t.end();
+});
+
+test('merge number with date', function (t) {
+ var target = extend(20, date);
+ var testDate = new Date(81, 4, 13);
+
+ t.deepEqual(date, testDate, 'original date is unchanged');
+ t.deepEqual(target, testDate, 'number + date is date');
+ t.end();
+});
+
+test('merge number with object', function (t) {
+ var target = extend(20, obj);
+ var testObj = {
+ str: 'me a test',
+ int: 10,
+ arr: [ 1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13)
+ };
+
+ t.deepEqual(obj, testObj, 'obj is unchanged');
+ t.deepEqual(target, testObj, 'number + obj is obj');
+ t.end();
+});
+
+test('merge array with string', function (t) {
+ var ori = [1, 2, 3, 4, 5, 6];
+ var target = extend(ori, str);
+
+ t.deepEqual(ori, str.split(''), 'array is changed to be an array of string chars');
+ t.equal(str, 'me a test', 'string is unchanged');
+ t.deepEqual(target, {
+ '0': 'm',
+ '1': 'e',
+ '2': ' ',
+ '3': 'a',
+ '4': ' ',
+ '5': 't',
+ '6': 'e',
+ '7': 's',
+ '8': 't'
+ }, 'array + string is object form of string');
+ t.end();
+});
+
+test('merge array with number', function (t) {
+ var ori = [1, 2, 3, 4, 5, 6];
+ var target = extend(ori, 10);
+
+ t.deepEqual(ori, [1, 2, 3, 4, 5, 6], 'array is unchanged');
+ t.deepEqual(target, ori, 'array + number is array');
+ t.end();
+});
+
+test('merge array with array', function (t) {
+ var ori = [1, 2, 3, 4, 5, 6];
+ var target = extend(ori, arr);
+ var testDate = new Date(81, 8, 4);
+ var expectedTarget = [1, 'what', testDate, 4, 5, 6];
+
+ t.deepEqual(ori, expectedTarget, 'array + array merges arrays; changes first array');
+ t.deepEqual(arr, [1, 'what', testDate], 'second array is unchanged');
+ t.deepEqual(target, expectedTarget, 'array + array is merged array');
+ t.end();
+});
+
+test('merge array with date', function (t) {
+ var ori = [1, 2, 3, 4, 5, 6];
+ var target = extend(ori, date);
+ var testDate = new Date(81, 4, 13);
+ var testArray = [1, 2, 3, 4, 5, 6];
+
+ t.deepEqual(ori, testArray, 'array is unchanged');
+ t.deepEqual(date, testDate, 'date is unchanged');
+ t.deepEqual(target, testArray, 'array + date is array');
+ t.end();
+});
+
+test('merge array with object', function (t) {
+ var ori = [1, 2, 3, 4, 5, 6];
+ var target = extend(ori, obj);
+ var testObject = {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13)
+ };
+
+ t.deepEqual(obj, testObject, 'obj is unchanged');
+ t.equal(ori.length, 6, 'array has proper length');
+ t.equal(ori.str, obj.str, 'array has obj.str property');
+ t.equal(ori.int, obj.int, 'array has obj.int property');
+ t.deepEqual(ori.arr, obj.arr, 'array has obj.arr property');
+ t.equal(ori.date, obj.date, 'array has obj.date property');
+
+ t.equal(target.length, 6, 'target has proper length');
+ t.equal(target.str, obj.str, 'target has obj.str property');
+ t.equal(target.int, obj.int, 'target has obj.int property');
+ t.deepEqual(target.arr, obj.arr, 'target has obj.arr property');
+ t.equal(target.date, obj.date, 'target has obj.date property');
+ t.end();
+});
+
+test('merge date with string', function (t) {
+ var ori = new Date(81, 9, 20);
+ var target = extend(ori, str);
+ var testObject = {
+ '0': 'm',
+ '1': 'e',
+ '2': ' ',
+ '3': 'a',
+ '4': ' ',
+ '5': 't',
+ '6': 'e',
+ '7': 's',
+ '8': 't'
+ };
+
+ t.deepEqual(ori, testObject, 'date is changed to object form of string');
+ t.equal(str, 'me a test', 'string is unchanged');
+ t.deepEqual(target, testObject, 'date + string is object form of string');
+ t.end();
+});
+
+test('merge date with number', function (t) {
+ var ori = new Date(81, 9, 20);
+ var target = extend(ori, 10);
+
+ t.deepEqual(ori, {}, 'date is changed to empty object');
+ t.deepEqual(target, {}, 'date + number is empty object');
+ t.end();
+});
+
+test('merge date with array', function (t) {
+ var ori = new Date(81, 9, 20);
+ var target = extend(ori, arr);
+ var testDate = new Date(81, 9, 20);
+ var testArray = [1, 'what', new Date(81, 8, 4)];
+
+ t.deepEqual(ori, testDate, 'date is unchanged');
+ t.deepEqual(arr, testArray, 'array is unchanged');
+ t.deepEqual(target, testDate, 'date + array is date');
+ t.end();
+});
+
+test('merge date with date', function (t) {
+ var ori = new Date(81, 9, 20);
+ var target = extend(ori, date);
+
+ t.deepEqual(ori, {}, 'date is empty object');
+ t.deepEqual(target, {}, 'date + date is empty object');
+ t.end();
+});
+
+test('merge date with object', function (t) {
+ var ori = new Date(81, 9, 20);
+ var target = extend(ori, obj);
+ var testDate = new Date(81, 8, 4);
+ var testObject = {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', testDate],
+ date: new Date(81, 4, 13)
+ };
+
+ t.deepEqual(obj, testObject, 'original object is unchanged');
+ t.deepEqual(ori, testObject, 'date becomes original object');
+ t.deepEqual(target, testObject, 'date + object is object');
+ t.end();
+});
+
+test('merge object with string', function (t) {
+ var testDate = new Date(81, 7, 26);
+ var ori = {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: testDate
+ };
+ var target = extend(ori, str);
+ var testObj = {
+ '0': 'm',
+ '1': 'e',
+ '2': ' ',
+ '3': 'a',
+ '4': ' ',
+ '5': 't',
+ '6': 'e',
+ '7': 's',
+ '8': 't',
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: testDate
+ };
+
+ t.deepEqual(ori, testObj, 'original object updated');
+ t.equal(str, 'me a test', 'string is unchanged');
+ t.deepEqual(target, testObj, 'object + string is object + object form of string');
+ t.end();
+});
+
+test('merge object with number', function (t) {
+ var ori = {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26)
+ };
+ var testObject = {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26)
+ };
+ var target = extend(ori, 10);
+ t.deepEqual(ori, testObject, 'object is unchanged');
+ t.deepEqual(target, testObject, 'object + number is object');
+ t.end();
+});
+
+test('merge object with array', function (t) {
+ var ori = {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26)
+ };
+ var target = extend(ori, arr);
+ var testObject = {
+ '0': 1,
+ '1': 'what',
+ '2': new Date(81, 8, 4),
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26)
+ };
+
+ t.deepEqual(ori, testObject, 'original object is merged');
+ t.deepEqual(arr, [1, 'what', testObject[2]], 'array is unchanged');
+ t.deepEqual(target, testObject, 'object + array is merged object');
+ t.end();
+});
+
+test('merge object with date', function (t) {
+ var ori = {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26)
+ };
+ var target = extend(ori, date);
+ var testObject = {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26)
+ };
+
+ t.deepEqual(ori, testObject, 'original object is unchanged');
+ t.deepEqual(date, new Date(81, 4, 13), 'date is unchanged');
+ t.deepEqual(target, testObject, 'object + date is object');
+ t.end();
+});
+
+test('merge object with object', function (t) {
+ var ori = {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26),
+ foo: 'bar'
+ };
+ var target = extend(ori, obj);
+ var expectedObj = {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13)
+ };
+ var expectedTarget = {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13),
+ foo: 'bar'
+ };
+
+ t.deepEqual(obj, expectedObj, 'obj is unchanged');
+ t.deepEqual(ori, expectedTarget, 'original has been merged');
+ t.deepEqual(target, expectedTarget, 'object + object is merged object');
+ t.end();
+});
+
+test('deep clone', function (t) {
+ var ori = {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26)
+ };
+ var target = extend(true, ori, deep);
+
+ t.deepEqual(ori, {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26),
+ ori: {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13)
+ },
+ layer: {
+ int: 10,
+ str: 'str',
+ date: new Date(84, 5, 12),
+ arr: [101, 'dude', new Date(82, 10, 4)],
+ deep: {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 7, 4)
+ }
+ }
+ }, 'original object is merged');
+ t.deepEqual(deep, {
+ ori: {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13)
+ },
+ layer: {
+ int: 10,
+ str: 'str',
+ date: new Date(84, 5, 12),
+ arr: [101, 'dude', new Date(82, 10, 4)],
+ deep: {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 7, 4)
+ }
+ }
+ }, 'deep is unchanged');
+ t.deepEqual(target, {
+ str: 'no shit',
+ int: 76,
+ arr: [1, 2, 3, 4],
+ date: new Date(81, 7, 26),
+ ori: {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13)
+ },
+ layer: {
+ int: 10,
+ str: 'str',
+ date: new Date(84, 5, 12),
+ arr: [101, 'dude', new Date(82, 10, 4)],
+ deep: {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 7, 4)
+ }
+ }
+ }, 'deep + object + object is deeply merged object');
+
+ target.layer.deep = 339;
+ t.deepEqual(deep, {
+ ori: {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 4, 13)
+ },
+ layer: {
+ int: 10,
+ str: 'str',
+ date: new Date(84, 5, 12),
+ arr: [101, 'dude', new Date(82, 10, 4)],
+ deep: {
+ str: 'me a test',
+ int: 10,
+ arr: [1, 'what', new Date(81, 8, 4)],
+ date: new Date(81, 7, 4)
+ }
+ }
+ }, 'deep is unchanged after setting target property');
+ //----- NEVER USE EXTEND WITH THE ABOVE SITUATION ------------------------------
+ t.end();
+});
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-extend.git
More information about the Pkg-javascript-commits
mailing list