Comparing version 1.1.3 to 1.2.0
138
merge.js
/*! | ||
* @name JavaScript/NodeJS Merge v1.1.3 | ||
* @name JavaScript/NodeJS Merge v1.2.0 | ||
* @author yeikos | ||
@@ -12,28 +12,36 @@ * @repository https://github.com/yeikos/js.merge | ||
function merge() { | ||
/** | ||
* Merge one or more objects | ||
* @param bool? clone | ||
* @param mixed,... arguments | ||
* @return object | ||
*/ | ||
var items = Array.prototype.slice.call(arguments), | ||
result = items.shift(), | ||
deep = (result === true), | ||
size = items.length, | ||
item, index, key; | ||
var Public = function(clone) { | ||
if (deep || typeOf(result) !== 'object') | ||
return merge(clone === true, false, arguments); | ||
result = {}; | ||
}, publicName = 'merge'; | ||
for (index=0;index<size;++index) | ||
/** | ||
* Merge two or more objects recursively | ||
* @param bool? clone | ||
* @param mixed,... arguments | ||
* @return object | ||
*/ | ||
if (typeOf(item = items[index]) === 'object') | ||
Public.recursive = function(clone) { | ||
for (key in item) | ||
return merge(clone === true, true, arguments); | ||
result[key] = deep ? clone(item[key]) : item[key]; | ||
}; | ||
return result; | ||
/** | ||
* Clone the input removing any reference | ||
* @param mixed input | ||
* @return mixed | ||
*/ | ||
} | ||
Public.clone = function(input) { | ||
function clone(input) { | ||
var output = input, | ||
@@ -50,3 +58,3 @@ type = typeOf(input), | ||
output[index] = clone(input[index]); | ||
output[index] = Public.clone(input[index]); | ||
@@ -59,3 +67,3 @@ } else if (type === 'object') { | ||
output[index] = clone(input[index]); | ||
output[index] = Public.clone(input[index]); | ||
@@ -66,7 +74,93 @@ } | ||
}; | ||
/** | ||
* Merge two objects recursively | ||
* @param mixed input | ||
* @param mixed extend | ||
* @return mixed | ||
*/ | ||
function merge_recursive(base, extend) { | ||
if (typeOf(base) !== 'object') | ||
return extend; | ||
for (var key in extend) { | ||
if (typeOf(base[key]) === 'object' && typeOf(extend[key]) === 'object') { | ||
base[key] = merge_recursive(base[key], extend[key]); | ||
} else { | ||
base[key] = extend[key]; | ||
} | ||
} | ||
return base; | ||
} | ||
/** | ||
* Merge two or more objects | ||
* @param bool clone | ||
* @param bool recursive | ||
* @param array argv | ||
* @return object | ||
*/ | ||
function merge(clone, recursive, argv) { | ||
var result = argv[0], | ||
size = argv.length; | ||
if (clone || typeOf(result) !== 'object') | ||
result = {}; | ||
for (var index=0;index<size;++index) { | ||
var item = argv[index], | ||
type = typeOf(item); | ||
if (type !== 'object') continue; | ||
for (var key in item) { | ||
var sitem = clone ? Public.clone(item[key]) : item[key]; | ||
if (recursive) { | ||
result[key] = merge_recursive(result[key], sitem); | ||
} else { | ||
result[key] = sitem; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
/** | ||
* Get type of variable | ||
* @param mixed input | ||
* @return string | ||
* | ||
* @see http://jsperf.com/typeofvar | ||
*/ | ||
function typeOf(input) { | ||
return ({}).toString.call(input).match(/\s([\w]+)/)[1].toLowerCase(); | ||
return ({}).toString.call(input).slice(8, -1).toLowerCase(); | ||
@@ -77,7 +171,7 @@ } | ||
module.exports = merge; | ||
module.exports = Public; | ||
} else { | ||
window.merge = merge; | ||
window[publicName] = Public; | ||
@@ -84,0 +178,0 @@ } |
@@ -1,3 +0,3 @@ | ||
/*! JavaScript/NodeJS Merge v1.1.3 | Copyright 2014 yeikos - MIT license | https://github.com/yeikos/js.merge */ | ||
/*! JavaScript/NodeJS Merge v1.2.0 | Copyright 2014 yeikos - MIT license | https://github.com/yeikos/js.merge */ | ||
;(function(e){function t(){var e=Array.prototype.slice.call(arguments),t=e.shift(),i=t===true,s=e.length,o,u,a;if(i||r(t)!=="object")t={};for(u=0;u<s;++u)if(r(o=e[u])==="object")for(a in o)t[a]=i?n(o[a]):o[a];return t}function n(e){var t=e,i=r(e),s,o;if(i==="array"){t=[];o=e.length;for(s=0;s<o;++s)t[s]=n(e[s])}else if(i==="object"){t={};for(s in e)t[s]=n(e[s])}return t}function r(e){return{}.toString.call(e).match(/\s([\w]+)/)[1].toLowerCase()}if(e){module.exports=t}else{window.merge=t}})(typeof module==="object"&&module&&typeof module.exports==="object"&&module.exports); | ||
;(function(e){function r(e,t){if(s(e)!=="object")return t;for(var n in t){if(s(e[n])==="object"&&s(t[n])==="object"){e[n]=r(e[n],t[n])}else{e[n]=t[n]}}return e}function i(e,n,i){var o=i[0],u=i.length;if(e||s(o)!=="object")o={};for(var a=0;a<u;++a){var f=i[a],l=s(f);if(l!=="object")continue;for(var c in f){var h=e?t.clone(f[c]):f[c];if(n){o[c]=r(o[c],h)}else{o[c]=h}}}return o}function s(e){return{}.toString.call(e).slice(8,-1).toLowerCase()}var t=function(e){return i(e===true,false,arguments)},n="merge";t.recursive=function(e){return i(e===true,true,arguments)};t.clone=function(e){var n=e,r=s(e),i,o;if(r==="array"){n=[];o=e.length;for(i=0;i<o;++i)n[i]=t.clone(e[i])}else if(r==="object"){n={};for(i in e)n[i]=t.clone(e[i])}return n};if(e){module.exports=t}else{window[n]=t}})(typeof module==="object"&&module&&typeof module.exports==="object"&&module.exports); |
{ | ||
"name": "merge", | ||
"version": "1.1.3", | ||
"author": "yeikos (http://www.yeikos.com)", | ||
"description": "JavaScript/NodeJS Merge is a tool to merge multiple objects into one object, with the possibility of create a new object cloned. His operation is very similar to the jQuery.extend function but more flexible.", | ||
"main": "merge.js", | ||
"license": "MIT", | ||
"homepage": "https://github.com/yeikos/js.merge", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/yeikos/js.merge.git" | ||
}, | ||
"keywords": [ | ||
"merge", | ||
"extend", | ||
"clone", | ||
"object", | ||
"browser" | ||
] | ||
"name": "merge", | ||
"version": "1.2.0", | ||
"author": "yeikos (http://www.yeikos.com)", | ||
"description": "Merge multiple objects into one, optionally creating a new cloned object. Similar to the jQuery.extend but more flexible. Works in Node.js and the browser.", | ||
"main": "merge.js", | ||
"license": "MIT", | ||
"homepage": "https://github.com/yeikos/js.merge", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/yeikos/js.merge.git" | ||
}, | ||
"keywords": [ | ||
"merge", | ||
"recursive", | ||
"extend", | ||
"clone", | ||
"object", | ||
"browser" | ||
], | ||
"scripts": { | ||
"test": "cd tests; node index.js" | ||
} | ||
} |
@@ -1,52 +0,58 @@ | ||
JavaScript/NodeJS Merge v1.1.3 | ||
================================================== | ||
# Merge | ||
What is it? | ||
-------------------------------------- | ||
Merge multiple objects into one, optionally creating a new cloned object. | ||
Similar to the jQuery.extend but more flexible. Works in Node.js and the | ||
browser. | ||
JavaScript/NodeJS Merge is a tool to merge multiple objects into one object, with the possibility of create a new object cloned. His operation is very similar to the [jQuery.extend](http://api.jquery.com/jQuery.extend/) function but more flexible. | ||
## Node.js Usage | ||
Example from NodeJS | ||
-------------- | ||
```sh | ||
npm install merge --save | ||
``` | ||
var merge = require('merge'), // npm install -g merge | ||
original, cloned; | ||
console.log( | ||
merge({ one: 'hello' }, { two: 'world' }) | ||
```js | ||
var merge = require('merge'), original, cloned; | ||
); // {"one": "hello", "two": "world"} | ||
console.log(merge({one:'hello'}, {two: 'world'})); | ||
// -> {"one": "hello", "two": "world"} | ||
original = { x: { y: 1 } }; | ||
cloned = merge(true, original); | ||
cloned.x.y++; | ||
console.log(original.x.y, cloned.x.y); | ||
// -> 1, 2 | ||
console.log(merge.recursive(true, original, { x: { z: 2 } })); | ||
// -> {"x": { "y": 1, "z": 2 } } | ||
``` | ||
## Browser Usage | ||
```html | ||
<script src="http://files.yeikos.com/merge.js"></script> | ||
<script> | ||
var original, cloned; | ||
console.log(merge({one:'hello'}, {two: 'world'})); | ||
// -> {"one": "hello", "two": "world"} | ||
original = { x: { y: 1 } }; | ||
cloned = merge(true, original); | ||
cloned.x.y++; | ||
console.log(original.x.y, cloned.x.y); // 1, 2 | ||
console.log(original.x.y, cloned.x.y); | ||
// -> 1, 2 | ||
Example from JavaScript browser | ||
-------------------------- | ||
console.log(merge.recursive(true, original, { x: { z: 2 } })); | ||
// -> {"x": { "y": 1, "z": 2 } } | ||
<script src="http://files.yeikos.com/merge.js"></script> | ||
<script> | ||
var original, cloned; | ||
console.log( | ||
merge({ one: 'hello' }, { two: 'world' }) | ||
); // {"one": "hello", "two": "world"} | ||
original = { x: { y: 1 } }; | ||
cloned = merge(true, original); | ||
cloned.x.y++; | ||
console.log(original.x.y, cloned.x.y); // 1, 2 | ||
</script> | ||
``` | ||
</script> | ||
## Tests | ||
```sh | ||
npm test | ||
``` |
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
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
7254
7
130
59
0