Add isCloneable and clonePassthru.

This commit is contained in:
John-David Dalton
2014-10-18 00:03:18 -07:00
parent 050b703fc0
commit f2780bcbc2
2 changed files with 83 additions and 34 deletions

View File

@@ -1344,9 +1344,7 @@
* @returns {*} Returns the value to assign to the destination object.
*/
function assignDefaults(objectValue, sourceValue) {
return typeof objectValue == 'undefined'
? sourceValue
: objectValue;
return typeof objectValue == 'undefined' ? sourceValue : objectValue;
}
/**
@@ -1368,6 +1366,18 @@
: objectValue;
}
/**
* Used by `_.matches` to clone `source` values, letting uncloneable values
* passthu instead of returning empty objects.
*
* @private
* @param {*} value The value to clone.
* @returns {*} Returns the cloned value.
*/
function clonePassthru(value) {
return isCloneable(value) ? undefined : value;
}
/**
* The base implementation of `_.assign` without support for argument juggling,
* multiple sources, and `this` binding.
@@ -2965,11 +2975,11 @@
* @returns {null|Object} Returns the initialized object clone.
*/
function initObjectClone(object, isDeep) {
var className = toString.call(object);
if (!cloneableClasses[className] || isHostObject(object)) {
if (!isCloneable(object)) {
return null;
}
var Ctor = object.constructor,
className = toString.call(object),
isArgs = className == argsClass || (!lodash.support.argsClass && isArguments(object)),
isObj = className == objectClass;
@@ -3024,6 +3034,17 @@
arrayLikeClasses[toString.call(value)]) || false;
}
/**
* Checks if `value` is cloneable.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is cloneable, else `false`.
*/
function isCloneable(value) {
return (value && cloneableClasses[toString.call(value)] && !isHostObject(value)) || false;
}
/**
* Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
*
@@ -6887,8 +6908,8 @@
* **Note:** This method is loosely based on the structured clone algorithm.
* The enumerable properties of `arguments` objects and objects created by
* constructors other than `Object` are cloned to plain `Object` objects. An
* empty object is returned for functions, DOM nodes, Maps, Sets, and WeakMaps.
* See the [HTML5 specification](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm)
* empty object is returned for uncloneable values such as functions, DOM nodes,
* Maps, Sets, and WeakMaps. See the [HTML5 specification](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm)
* for more details.
*
* @static
@@ -6944,8 +6965,8 @@
* **Note:** This method is loosely based on the structured clone algorithm.
* The enumerable properties of `arguments` objects and objects created by
* constructors other than `Object` are cloned to plain `Object` objects. An
* empty object is returned for functions, DOM nodes, Maps, Sets, and WeakMaps.
* See the [HTML5 specification](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm)
* empty object is returned for uncloneable values such as functions, DOM nodes,
* Maps, Sets, and WeakMaps. See the [HTML5 specification](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm)
* for more details.
*
* @static
@@ -9245,7 +9266,7 @@
value = source[props[index]];
var isStrict = isStrictComparable(value);
values[index] = isStrict ? value : baseClone(value, true, matchesCloneCallback);
values[index] = isStrict ? value : baseClone(value, true, clonePassthru);
strictCompareFlags[index] = isStrict;
}
return function(object) {