Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yeikos committed Sep 7, 2014
1 parent b071d83 commit 6fc27c2
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 85 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ 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
Expand All @@ -40,6 +44,10 @@ console.log(original.x.y, 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 } }
</script>
```

Expand Down
22 changes: 22 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "merge",
"version": "1.2.0",
"homepage": "https://github.com/yeikos/js.merge",
"authors": [
"yeikos <[email protected]>"
],
"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",
"keywords": [
"merge",
"recursive",
"extend",
"clone",
"object",
"browser"
],
"license": "MIT",
"ignore": [
"tests"
]
}
138 changes: 116 additions & 22 deletions merge.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* @name JavaScript/NodeJS Merge v1.1.3
* @name JavaScript/NodeJS Merge v1.2.0
* @author yeikos
* @repository https://github.com/yeikos/js.merge
Expand All @@ -9,31 +9,39 @@

;(function(isNode) {

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 = {};

for (index=0;index<size;++index)
}, publicName = 'merge';

if (typeOf(item = items[index]) === 'object')
/**
* Merge two or more objects recursively
* @param bool? clone
* @param mixed,... arguments
* @return object
*/

for (key in item)
Public.recursive = function(clone) {

result[key] = deep ? clone(item[key]) : item[key];
return merge(clone === true, true, arguments);

return result;
};

}
/**
* Clone the input removing any reference
* @param mixed input
* @return mixed
*/

function clone(input) {
Public.clone = function(input) {

var output = input,
type = typeOf(input),
Expand All @@ -46,35 +54,121 @@

for (index=0;index<size;++index)

output[index] = clone(input[index]);
output[index] = Public.clone(input[index]);

} else if (type === 'object') {

output = {};

for (index in input)

output[index] = clone(input[index]);
output[index] = Public.clone(input[index]);

}

return output;

};

/**
* 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();

}

if (isNode) {

module.exports = merge;
module.exports = Public;

} else {

window.merge = merge;
window[publicName] = Public;

}

Expand Down
4 changes: 2 additions & 2 deletions merge.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "merge",
"version": "1.1.3",
"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",
Expand All @@ -12,6 +12,7 @@
},
"keywords": [
"merge",
"recursive",
"extend",
"clone",
"object",
Expand Down
8 changes: 4 additions & 4 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JavaScript/NodeJS Merge v1.1.3</title>
<title>JavaScript/NodeJS Merge v1.2.0</title>

<link rel="stylesheet" href="qunit/qunit.css" />

<script src="qunit/qunit.js" data-qunit></script>
<script src="../merge.js" data-qunit></script>
<script src="tests.js" data-qunit></script>
<script src="qunit/qunit.js"></script>
<script src="../merge.js"></script>
<script src="tests.js"></script>

</head>

Expand Down
Loading

0 comments on commit 6fc27c2

Please sign in to comment.