object-merge
Advanced tools
Comparing version 1.1.1 to 2.1.0
;(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
objectMerge = require('../src/object-merge.js'); | ||
},{"../src/object-merge.js":3}],2:[function(require,module,exports){ | ||
},{"../src/object-merge.js":4}],2:[function(require,module,exports){ | ||
/* | ||
@@ -30,2 +30,21 @@ License gpl-3.0 http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
},{}],3:[function(require,module,exports){ | ||
/** | ||
* Executes a function on each of an objects own enumerable properties. The | ||
* callback function will receive three arguments: the value of the current | ||
* property, the name of the property, and the object being processed. This is | ||
* roughly equivalent to the signature for callbacks to | ||
* Array.prototype.forEach. | ||
* @param {Object} obj The object to act on. | ||
* @param {Function} callback The function to execute. | ||
* @returns {Object} Returns the given object. | ||
*/ | ||
function objectForeach(obj, callback) { | ||
"use strict"; | ||
Object.keys(obj).forEach(function (prop) { | ||
callback(obj[prop], prop, obj); | ||
}); | ||
return obj; | ||
}; | ||
module.exports = objectForeach; | ||
},{}],4:[function(require,module,exports){ | ||
/* | ||
@@ -94,33 +113,61 @@ License gpl-3.0 http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
'use strict'; | ||
var objectForeach = require('object-foreach'); | ||
var cloneFunction = require('clone-function'); | ||
var out = Object.create(null); | ||
shadows = Array.prototype.slice.call(arguments, 0); | ||
shadows.forEach(function (shadow) { | ||
Object.keys(shadow).forEach(function (prop) { | ||
var tmp = Object.create(null); | ||
var pBase = out[prop] || tmp; | ||
var arr = []; | ||
if (shadow[prop] instanceof Array) { | ||
pBase = typeof out[prop] === 'object' ? pBase : tmp; | ||
tmp = objectMerge(pBase, shadow[prop]); | ||
Object.keys(tmp).forEach(function (key) { | ||
arr[key] = tmp[key]; | ||
}); | ||
tmp = arr; | ||
} else if (shadow[prop] instanceof Function) { | ||
tmp = cloneFunction(shadow[prop]); | ||
} else if (typeof shadow[prop] === 'object') { | ||
pBase = typeof out[prop] === 'object' ? pBase : tmp; | ||
tmp = objectMerge(pBase, shadow[prop]); | ||
// gets the sequential trailing objects from array. | ||
function getShadowObjects(shadows) { | ||
var out = shadows.reduce(function (collector, shadow) { | ||
if (shadow instanceof Object) { | ||
collector.push(shadow); | ||
} else { | ||
collector = []; | ||
} | ||
return collector; | ||
}, []); | ||
return out; | ||
} | ||
// gets either a new object of the proper type or the last primitive value | ||
function getOutputObject(shadows) { | ||
var out; | ||
var lastShadow = shadows[shadows.length - 1]; | ||
if (lastShadow instanceof Array) { | ||
out = []; | ||
} else if (lastShadow instanceof Function) { | ||
out = cloneFunction(lastShadow); | ||
} else if (lastShadow instanceof Object) { | ||
out = {}; | ||
} else { | ||
// lastShadow is a primitive value; | ||
out = lastShadow; | ||
} | ||
return out; | ||
} | ||
function main(shadows) { | ||
var out = getOutputObject(shadows); | ||
function shadowHandler(val, prop, shadow) { | ||
/*jslint unparam:true */ | ||
if (out[prop]) { | ||
out[prop] = objectMerge(out[prop], shadow[prop]); | ||
} else { | ||
tmp = shadow[prop]; | ||
out[prop] = objectMerge(shadow[prop]); | ||
} | ||
out[prop] = tmp; | ||
}); | ||
}); | ||
return out; | ||
} | ||
function shadowMerger(shadow) { | ||
objectForeach(shadow, shadowHandler); | ||
} | ||
// short circuits case where output would be a primitive value anyway. | ||
if (out instanceof Object) { | ||
// only merges trailing objects since primitives would wipe out previous | ||
// objects, as in merging {a:'a'}, 'a', and {b:'b'} would result in | ||
// {b:'b'} so the first two arguments can be ignored completely. | ||
var relevantShadows = getShadowObjects(shadows); | ||
relevantShadows.forEach(shadowMerger); | ||
} | ||
return out; | ||
} | ||
return main(shadows); | ||
} |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ygb3V0W3Byb3BdID09PSAnb2JqZWN0JyA/IHBCYXNlIDogdG1wO1xyXG4gICAgICAgICAgICAgICAgdG1wID0gb2JqZWN0TWVyZ2UocEJhc2UsIHNoYWRvd1twcm9wXSk7XHJcbiAgICAgICAgICAgICAgICBPYmplY3Qua2V5cyh0bXApLmZvckVhY2goZnVuY3Rpb24gKGtleSkge1xyXG4gICAgICAgICAgICAgICAgICAgIGFycltrZXldID0gdG1wW2tleV07XHJcbiAgICAgICAgICAgICAgICB9KTtcclxuICAgICAgICAgICAgICAgIHRtcCA9IGFycjtcclxuICAgICAgICAgICAgfSBlbHNlIGlmIChzaGFkb3dbcHJvcF0gaW5zdGFuY2VvZiBGdW5jdGlvbikge1xyXG4gICAgICAgICAgICAgICAgdG1wID0gY2xvbmVGdW5jdGlvbihzaGFkb3dbcHJvcF0pO1xyXG4gICAgICAgICAgICB9IGVsc2UgaWYgKHR5cGVvZiBzaGFkb3dbcHJvcF0gPT09ICdvYmplY3QnKSB7XHJcbiAgICAgICAgICAgICAgICBwQmFzZSA9IHR5cGVvZiBvdXRbcHJvcF0gPT09ICdvYmplY3QnID8gcEJhc2UgOiB0bXA7XHJcbiAgICAgICAgICAgICAgICB0bXAgPSBvYmplY3RNZXJnZShwQmFzZSwgc2hhZG93W3Byb3BdKTtcclxuICAgICAgICAgICAgfSBlbHNlIHtcclxuICAgICAgICAgICAgICAgIHRtcCA9IHNoYWRvd1twcm9wXTtcclxuICAgICAgICAgICAgfVxyXG4gICAgICAgICAgICBvdXRbcHJvcF0gPSB0bXA7XHJcbiAgICAgICAgfSk7XHJcbiAgICB9KTtcclxuICAgIHJldHVybiBvdXQ7XHJcbn1cclxubW9kdWxlLmV4cG9ydHMgPSBvYmplY3RNZXJnZTsiXX0= | ||
},{"clone-function":2,"object-foreach":3}]},{},[1]) | ||
//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJhdGVkLmpzIiwic291cmNlcyI6WyJ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gYXJndW1lbnQgZ2l2ZW4gd2lsbCBiZSB0cmVhdGVkIGFzIGFuIG9iamVjdCB0byBtZXJnZS4gRWFjaCBvYmplY3Qgb3ZlcndyaXRlc1xyXG4gKiAgdGhlIHByZXZpb3VzIG9iamVjdHMgZGVzY2VuZGFudCBwcm9wZXJ0aWVzIGlmIHRoZSBwcm9wZXJ0eSBuYW1lIG1hdGNoZXMuIElmXHJcbiAqICBvYmplY3RzIHByb3BlcnRpZXMgYXJlIG9iamVjdHMgdGhleSB3aWxsIGJlIG1lcmdlZCByZWN1cnNpdmVseSBhcyB3ZWxsLlxyXG4gKiBAcmV0dXJucyB7T2JqZWN0fSBSZXR1cm5zIGEgc2luZ2xlIG1lcmdlZCBvYmplY3QgY29tcG9zZWQgZnJvbSBjbG9uZXMgb2YgdGhlXHJcbiAqICBpbnB1dCBvYmplY3RzLlxyXG4gKiBAZXhhbXBsZVxyXG4gKiAgdmFyIG9iamVjdE1lcmdlID0gcmVxdWlyZSgnb2JqZWN0LW1lcmdlJyk7XHJcbiAqICB2YXIgeCA9IHtcclxuICogICAgICBhIDogJ2EnLFxyXG4gKiAgICAgIGIgOiAnYicsXHJcbiAqICAgICAgYyA6IHtcclxuICogICAgICAgICAgZCA6ICdkJyxcclxuICogICAgICAgICAgZSA6ICdlJyxcclxuICogICAgICAgICAgZiA6IHtcclxuICogICAgICAgICAgICAgIGcgOiAnZydcclxuICogICAgICAgICAgfVxyXG4gKiAgICAgIH1cclxuICogIH07XHJcbiAqICB2YXIgeSA9IHtcclxuICogICAgICBhIDogJ2BhJyxcclxuICogICAgICBiIDogJ2BiJyxcclxuICogICAgICBjIDoge1xyXG4gKiAgICAgICAgICBkIDogJ2BkJ1xyXG4gKiAgICAgIH1cclxuICogIH07XHJcbiAqICB2YXIgeiA9IHtcclxuICogICAgICBhIDoge1xyXG4gKiAgICAgICAgICBiIDogJ2BgYidcclxuICogICAgICB9LFxyXG4gKiAgICAgIGZ1biA6IGZ1bmN0aW9uIGZvbyAoKSB7XHJcbiAqICAgICAgICAgIHJldHVybiAnZm9vJztcclxuICogICAgICB9LFxyXG4gKiAgICAgIGFwcyA6IEFycmF5LnByb3RvdHlwZS5zbGljZVxyXG4gKiAgfTtcclxuICogIHZhciBvdXQgPSBvYmplY3RNZXJnZSh4LCB5LCB6KTtcclxuICogIC8vIG91dC5hIHdpbGwgYmUge1xyXG4gKiAgLy8gICAgICAgICBiIDogJ2BgYidcclxuICogIC8vICAgICB9XHJcbiAqICAvLyBvdXQuYiB3aWxsIGJlICdgYidcclxuICogIC8vIG91dC5jIHdpbGwgYmUge1xyXG4gKiAgLy8gICAgICAgICBkIDogJ2BkJyxcclxuICogIC8vICAgICAgICAgZSA6ICdlJyxcclxuICogIC8vICAgICAgICAgZiA6IHtcclxuICogIC8vICAgICAgICAgICAgIGcgOiAnZydcclxuICogIC8vICAgICAgICAgfVxyXG4gKiAgLy8gICAgIH1cclxuICogIC8vIG91dC5mdW4gd2lsbCBiZSBhIGNsb25lIG9mIHouZnVuXHJcbiAqICAvLyBvdXQuYXBzIHdpbGwgYmUgZXF1YWwgdG8gei5hcHNcclxuICovXHJcbmZ1bmN0aW9uIG9iamVjdE1lcmdlKHNoYWRvd3MpIHtcclxuICAgICd1c2Ugc3RyaWN0JztcclxuICAgIHZhciBvYmplY3RGb3JlYWNoID0gcmVxdWlyZSgnb2JqZWN0LWZvcmVhY2gnKTtcclxuICAgIHZhciBjbG9uZUZ1bmN0aW9uID0gcmVxdWlyZSgnY2xvbmUtZnVuY3Rpb24nKTtcclxuICAgIHNoYWRvd3MgPSBBcnJheS5wcm90b3R5cGUuc2xpY2UuY2FsbChhcmd1bWVudHMsIDApO1xyXG4gICAgLy8gZ2V0cyB0aGUgc2VxdWVudGlhbCB0cmFpbGluZyBvYmplY3RzIGZyb20gYXJyYXkuXHJcbiAgICBmdW5jdGlvbiBnZXRTaGFkb3dPYmplY3RzKHNoYWRvd3MpIHtcclxuICAgICAgICB2YXIgb3V0ID0gc2hhZG93cy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CB7YjonYid9IHdvdWxkIHJlc3VsdCBpblxyXG4gICAgICAgICAgICAvLyB7YjonYid9IHNvIHRoZSBmaXJzdCB0d28gYXJndW1lbnRzIGNhbiBiZSBpZ25vcmVkIGNvbXBsZXRlbHkuXHJcbiAgICAgICAgICAgIHZhciByZWxldmFudFNoYWRvd3MgPSBnZXRTaGFkb3dPYmplY3RzKHNoYWRvd3MpO1xyXG4gICAgICAgICAgICByZWxldmFudFNoYWRvd3MuZm9yRWFjaChzaGFkb3dNZXJnZXIpO1xyXG4gICAgICAgIH1cclxuICAgICAgICByZXR1cm4gb3V0O1xyXG4gICAgfVxyXG4gICAgcmV0dXJuIG1haW4oc2hhZG93cyk7XHJcbn1cclxubW9kdWxlLmV4cG9ydHMgPSBvYmplY3RNZXJnZTsiXX0= | ||
; |
@@ -44,2 +44,22 @@ /* | ||
}); | ||
it('clones function properties', function () { | ||
var func = function () { | ||
return null; | ||
}; | ||
var func2 = function () { | ||
return 'hello'; | ||
}; | ||
func.wohoo = 'wohoo'; | ||
func.obj = {a:'a'}; | ||
func2.wee = 'wee'; | ||
func2.obj2 = {a:'a'}; | ||
var out = objectMerge(func, func2); | ||
expect(out.wohoo).toEqual('wohoo'); | ||
expect(out.wee).toEqual('wee'); | ||
expect(out.obj.a).toEqual('a'); | ||
expect(out.obj2.a).toEqual('a'); | ||
expect(out.obj === func.obj).toEqual(false); | ||
expect(out.obj2 === func2.obj2).toEqual(false); | ||
expect(out() === 'hello').toEqual(true); | ||
}); | ||
it('references native functions', function () { | ||
@@ -50,2 +70,49 @@ var out = objectMerge(x, y, z); | ||
}); | ||
it('preserves array indexes', function () { | ||
var arr = []; | ||
arr[0] = '0'; | ||
arr[5] = '5'; | ||
var obj = { | ||
'0' : '1', | ||
'1' : '1' | ||
}; | ||
var out = objectMerge(obj, arr); | ||
expect(arr.length).toEqual(6); | ||
expect(out.length).toEqual(6); | ||
expect(arr[0]).toEqual(out[0]); | ||
expect(obj[1]).toEqual(out[1]); | ||
expect(out[5]).toEqual(arr[5]); | ||
expect(out instanceof Array).toEqual(true); | ||
}); | ||
it('clones array properties', function () { | ||
var arr = []; | ||
arr[0] = '0'; | ||
var obj = { | ||
'wee' : 'wee', | ||
'wohoo' : 'wohoo', | ||
'obj' : {} | ||
}; | ||
var out = objectMerge(obj, arr); | ||
expect(arr.length).toEqual(out.length); | ||
expect(arr[0]).toEqual(out[0]); | ||
expect(out.wee).toEqual(obj.wee); | ||
expect(arr.wee === undefined).toEqual(true); | ||
expect(out.wohoo).toEqual(obj.wohoo); | ||
expect(out instanceof Array).toEqual(true); | ||
expect(out.obj).toEqual({}); | ||
expect(out.obj === obj.obj).toEqual(false); | ||
}); | ||
it('merges arrays', function () { | ||
var arr1 = ['0','1','2']; | ||
var arr2 = []; | ||
arr2[2] = '`2'; | ||
arr2[3] = '`3'; | ||
var out = objectMerge(arr1, arr2); | ||
expect(arr1.length).toEqual(3); | ||
expect(arr2.length).toEqual(4); | ||
expect(out.length).toEqual(4); | ||
expect(arr1).toEqual(['0','1','2']); | ||
expect(arr2).toEqual([undefined, undefined, '`2','`3']); | ||
expect(out).toEqual(['0','1','`2','`3']); | ||
}); | ||
it('clones array contents', function () { | ||
@@ -52,0 +119,0 @@ var out = objectMerge(x, w); |
{ | ||
"name": "object-merge", | ||
"version": "1.1.1", | ||
"version": "2.1.0", | ||
"description": "Merges JavaScript objects recursively without altering the objects merged.", | ||
@@ -46,4 +46,5 @@ "main": "./src/object-merge.js", | ||
"dependencies": { | ||
"clone-function": ">=1.0.1" | ||
"clone-function": ">=1.0.1", | ||
"object-foreach": ">=0.1.2" | ||
} | ||
} |
@@ -64,30 +64,58 @@ /* | ||
'use strict'; | ||
var objectForeach = require('object-foreach'); | ||
var cloneFunction = require('clone-function'); | ||
var out = Object.create(null); | ||
shadows = Array.prototype.slice.call(arguments, 0); | ||
shadows.forEach(function (shadow) { | ||
Object.keys(shadow).forEach(function (prop) { | ||
var tmp = Object.create(null); | ||
var pBase = out[prop] || tmp; | ||
var arr = []; | ||
if (shadow[prop] instanceof Array) { | ||
pBase = typeof out[prop] === 'object' ? pBase : tmp; | ||
tmp = objectMerge(pBase, shadow[prop]); | ||
Object.keys(tmp).forEach(function (key) { | ||
arr[key] = tmp[key]; | ||
}); | ||
tmp = arr; | ||
} else if (shadow[prop] instanceof Function) { | ||
tmp = cloneFunction(shadow[prop]); | ||
} else if (typeof shadow[prop] === 'object') { | ||
pBase = typeof out[prop] === 'object' ? pBase : tmp; | ||
tmp = objectMerge(pBase, shadow[prop]); | ||
// gets the sequential trailing objects from array. | ||
function getShadowObjects(shadows) { | ||
var out = shadows.reduce(function (collector, shadow) { | ||
if (shadow instanceof Object) { | ||
collector.push(shadow); | ||
} else { | ||
collector = []; | ||
} | ||
return collector; | ||
}, []); | ||
return out; | ||
} | ||
// gets either a new object of the proper type or the last primitive value | ||
function getOutputObject(shadows) { | ||
var out; | ||
var lastShadow = shadows[shadows.length - 1]; | ||
if (lastShadow instanceof Array) { | ||
out = []; | ||
} else if (lastShadow instanceof Function) { | ||
out = cloneFunction(lastShadow); | ||
} else if (lastShadow instanceof Object) { | ||
out = {}; | ||
} else { | ||
// lastShadow is a primitive value; | ||
out = lastShadow; | ||
} | ||
return out; | ||
} | ||
function main(shadows) { | ||
var out = getOutputObject(shadows); | ||
function shadowHandler(val, prop, shadow) { | ||
/*jslint unparam:true */ | ||
if (out[prop]) { | ||
out[prop] = objectMerge(out[prop], shadow[prop]); | ||
} else { | ||
tmp = shadow[prop]; | ||
out[prop] = objectMerge(shadow[prop]); | ||
} | ||
out[prop] = tmp; | ||
}); | ||
}); | ||
return out; | ||
} | ||
function shadowMerger(shadow) { | ||
objectForeach(shadow, shadowHandler); | ||
} | ||
// short circuits case where output would be a primitive value anyway. | ||
if (out instanceof Object) { | ||
// only merges trailing objects since primitives would wipe out previous | ||
// objects, as in merging {a:'a'}, 'a', and {b:'b'} would result in | ||
// {b:'b'} so the first two arguments can be ignored completely. | ||
var relevantShadows = getShadowObjects(shadows); | ||
relevantShadows.forEach(shadowMerger); | ||
} | ||
return out; | ||
} | ||
return main(shadows); | ||
} | ||
module.exports = objectMerge; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
217814
3408
2
+ Addedobject-foreach@>=0.1.2
+ Addedobject-foreach@0.1.2(transitive)