converted underscore.mixin.deepExtend to proper AMD module.

pull/6/head
Keivan Beigi 12 years ago
parent 28a919e74a
commit e0aceb98d2

@ -3,11 +3,16 @@
* *
* Based on https://gist.github.com/echong/3861963 * Based on https://gist.github.com/echong/3861963
*/ */
(function() { define(
var arrays, basicObjects, deepClone, deepExtend, deepExtendCouple, isBasicObject, [
__slice = [].slice; 'underscore'
], function (_) {
deepClone = function(obj) { var arrays, basicObjects, deepClone, deepExtend, deepExtendCouple, isBasicObject, __slice =
[
].slice;
deepClone = function (obj) {
var func, isArr; var func, isArr;
if (!_.isObject(obj) || _.isFunction(obj)) { if (!_.isObject(obj) || _.isFunction(obj)) {
return obj; return obj;
@ -22,35 +27,40 @@
return new RegExp(obj.source, obj.toString().replace(/.*\//, '')); return new RegExp(obj.source, obj.toString().replace(/.*\//, ''));
} }
isArr = _.isArray(obj || _.isArguments(obj)); isArr = _.isArray(obj || _.isArguments(obj));
func = function(memo, value, key) { func = function (memo, value, key) {
if (isArr) { if (isArr) {
memo.push(deepClone(value)); memo.push(deepClone(value));
} else { }
else {
memo[key] = deepClone(value); memo[key] = deepClone(value);
} }
return memo; return memo;
}; };
return _.reduce(obj, func, isArr ? [] : {}); return _.reduce(obj, func, isArr ?
[
] :{});
}; };
isBasicObject = function(object) { isBasicObject = function (object) {
if (object == null) return false; if (object == null) {
return false;
}
return (object.prototype === {}.prototype || object.prototype === Object.prototype) && _.isObject(object) && !_.isArray(object) && !_.isFunction(object) && !_.isDate(object) && !_.isRegExp(object) && !_.isArguments(object); return (object.prototype === {}.prototype || object.prototype === Object.prototype) && _.isObject(object) && !_.isArray(object) && !_.isFunction(object) && !_.isDate(object) && !_.isRegExp(object) && !_.isArguments(object);
}; };
basicObjects = function(object) { basicObjects = function (object) {
return _.filter(_.keys(object), function(key) { return _.filter(_.keys(object), function (key) {
return isBasicObject(object[key]); return isBasicObject(object[key]);
}); });
}; };
arrays = function(object) { arrays = function (object) {
return _.filter(_.keys(object), function(key) { return _.filter(_.keys(object), function (key) {
return _.isArray(object[key]); return _.isArray(object[key]);
}); });
}; };
deepExtendCouple = function(destination, source, maxDepth) { deepExtendCouple = function (destination, source, maxDepth) {
var combine, recurse, sharedArrayKey, sharedArrayKeys, sharedObjectKey, sharedObjectKeys, _i, _j, _len, _len1; var combine, recurse, sharedArrayKey, sharedArrayKeys, sharedObjectKey, sharedObjectKeys, _i, _j, _len, _len1;
if (maxDepth == null) { if (maxDepth == null) {
maxDepth = 20; maxDepth = 20;
@ -60,7 +70,7 @@
return _.extend(destination, source); return _.extend(destination, source);
} }
sharedObjectKeys = _.intersection(basicObjects(destination), basicObjects(source)); sharedObjectKeys = _.intersection(basicObjects(destination), basicObjects(source));
recurse = function(key) { recurse = function (key) {
return source[key] = deepExtendCouple(destination[key], source[key], maxDepth - 1); return source[key] = deepExtendCouple(destination[key], source[key], maxDepth - 1);
}; };
for (_i = 0, _len = sharedObjectKeys.length; _i < _len; _i++) { for (_i = 0, _len = sharedObjectKeys.length; _i < _len; _i++) {
@ -68,7 +78,7 @@
recurse(sharedObjectKey); recurse(sharedObjectKey);
} }
sharedArrayKeys = _.intersection(arrays(destination), arrays(source)); sharedArrayKeys = _.intersection(arrays(destination), arrays(source));
combine = function(key) { combine = function (key) {
return source[key] = _.union(destination[key], source[key]); return source[key] = _.union(destination[key], source[key]);
}; };
for (_j = 0, _len1 = sharedArrayKeys.length; _j < _len1; _j++) { for (_j = 0, _len1 = sharedArrayKeys.length; _j < _len1; _j++) {
@ -78,9 +88,11 @@
return _.extend(destination, source); return _.extend(destination, source);
}; };
deepExtend = function() { deepExtend = function () {
var finalObj, maxDepth, objects, _i; var finalObj, maxDepth, objects, _i;
objects = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), maxDepth = arguments[_i++]; objects = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) :(_i = 0,
[
]), maxDepth = arguments[_i++];
if (!_.isNumber(maxDepth)) { if (!_.isNumber(maxDepth)) {
objects.push(maxDepth); objects.push(maxDepth);
maxDepth = 20; maxDepth = 20;
@ -98,7 +110,6 @@
return finalObj; return finalObj;
}; };
require(['underscore'], function (_) {
_.mixin({ _.mixin({
deepClone : deepClone, deepClone : deepClone,
@ -109,4 +120,3 @@
}); });
}); });
}).call(this);

Loading…
Cancel
Save