82 lines
1.4 KiB
JavaScript
82 lines
1.4 KiB
JavaScript
/*!
|
|
* @name JavaScript/NodeJS Merge v1.1.0
|
|
* @author yeikos
|
|
* @repository https://github.com/yeikos/js.merge
|
|
|
|
* Copyright 2013
|
|
* GNU General Public License
|
|
* http://www.gnu.org/licenses/gpl-3.0.txt
|
|
*/
|
|
|
|
;(function(isNode) {
|
|
|
|
function merge() {
|
|
|
|
var items = Array.prototype.slice.call(arguments),
|
|
result = items.shift(),
|
|
deep = (result === true),
|
|
size = items.length,
|
|
item, index, key;
|
|
|
|
if (deep || typeOf(result) !== 'object')
|
|
|
|
result = {};
|
|
|
|
for (index=0;index<size;++index)
|
|
|
|
if (typeOf(item = items[index]) === 'object')
|
|
|
|
for (key in item)
|
|
|
|
result[key] = deep ? clone(item[key]) : item[key];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function clone(input) {
|
|
|
|
var output = input,
|
|
type = typeOf(input),
|
|
index, size;
|
|
|
|
if (type === 'array') {
|
|
|
|
output = [];
|
|
size = input.length;
|
|
|
|
for (index=0;index<size;++index)
|
|
|
|
output[index] = clone(input[index]);
|
|
|
|
} else if (type === 'object') {
|
|
|
|
output = {};
|
|
|
|
for (index in input)
|
|
|
|
output[index] = clone(input[index]);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
function typeOf(input) {
|
|
|
|
return ({}).toString.call(input).match(/\s([\w]+)/)[1].toLowerCase();
|
|
|
|
}
|
|
|
|
if (isNode) {
|
|
|
|
module.exports = merge;
|
|
|
|
} else {
|
|
|
|
window.merge = merge;
|
|
|
|
}
|
|
|
|
})(typeof module === 'object' && module && typeof module.exports === 'object' && module.exports); |