From 2586129dcd2fe8f017d4f755dbcf152171f221f8 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 9 Jul 2015 19:37:23 -0700 Subject: [PATCH] Replace `arrayCopy` and `baseCopy` with `copyArray`, `copyObject`, and `copyObjectWith`. --- lodash.src.js | 148 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 89 insertions(+), 59 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 794138cba..938c720df 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -1164,12 +1164,12 @@ */ function lazyClone() { var result = new LazyWrapper(this.__wrapped__); - result.__actions__ = arrayCopy(this.__actions__); + result.__actions__ = copyArray(this.__actions__); result.__dir__ = this.__dir__; result.__filtered__ = this.__filtered__; - result.__iteratees__ = arrayCopy(this.__iteratees__); + result.__iteratees__ = copyArray(this.__iteratees__); result.__takeCount__ = this.__takeCount__; - result.__views__ = arrayCopy(this.__views__); + result.__views__ = copyArray(this.__views__); return result; } @@ -1397,25 +1397,6 @@ return result; } - /** - * Copies the values of `source` to `array`. - * - * @private - * @param {Array} source The array to copy values from. - * @param {Array} [array=[]] The array to copy values to. - * @returns {Array} Returns `array`. - */ - function arrayCopy(source, array) { - var index = -1, - length = source.length; - - array || (array = Array(length)); - while (++index < length) { - array[index] = source[index]; - } - return array; - } - /** * A specialized version of `_.forEach` for arrays without support for callback * shorthands and `this` binding. @@ -1768,30 +1749,8 @@ } /** - * Copies properties of `source` to `object`. - * - * @private - * @param {Object} source The object to copy properties from. - * @param {Array} props The property names to copy. - * @param {Object} [object={}] The object to copy properties to. - * @returns {Object} Returns `object`. - */ - function baseCopy(source, props, object) { - object || (object = {}); - - var index = -1, - length = props.length; - - while (++index < length) { - var key = props[index]; - object[key] = source[key]; - } - return object; - } - - /** - * The base implementation of `_.clone` without support for argument juggling - * and `this` binding `customizer` functions. + * The base implementation of `_.clone` and `_.cloneDeep` which tracks + * traversed objects. * * @private * @param {*} value The value to clone. @@ -1818,7 +1777,7 @@ if (isArr) { result = initCloneArray(value); if (!isDeep) { - return arrayCopy(value, result); + return copyArray(value, result); } } else { var tag = objToString.call(value), @@ -2491,7 +2450,7 @@ if (isArrayLike(srcValue) && (isArray(srcValue) || isTypedArray(srcValue))) { result = isArray(value) ? value - : (isArrayLike(value) ? arrayCopy(value) : []); + : (isArrayLike(value) ? copyArray(value) : []); } else if (isPlainObject(srcValue) || isArguments(srcValue)) { result = isArguments(value) @@ -2985,6 +2944,77 @@ return result; } + /** + * Copies the values of `source` to `array`. + * + * @private + * @param {Array} source The array to copy values from. + * @param {Array} [array=[]] The array to copy values to. + * @returns {Array} Returns `array`. + */ + function copyArray(source, array) { + var index = -1, + length = source.length; + + array || (array = Array(length)); + while (++index < length) { + array[index] = source[index]; + } + return array; + } + + /** + * Copies properties of `source` to `object`. + * + * @private + * @param {Object} source The object to copy properties from. + * @param {Array} props The property names to copy. + * @param {Object} [object={}] The object to copy properties to. + * @returns {Object} Returns `object`. + */ + function copyObject(source, props, object) { + object || (object = {}); + + var index = -1, + length = props.length; + + while (++index < length) { + var key = props[index]; + object[key] = source[key]; + } + return object; + } + + /** + * This function is like `copyObject` except that it accepts a function to + * customize copied values. + * + * @private + * @param {Object} source The object to copy properties from. + * @param {Array} props The property names to copy. + * @param {Function} customizer The function to customize copied values. + * @param {Object} [object={}] The object to copy properties to. + * @returns {Object} Returns `object`. + */ + function copyObjectWith(source, props, customizer, object) { + object || (object = {}); + + var index = -1, + length = props.length; + + while (++index < length) { + var key = props[index], + value = object[key], + result = customizer(value, source[key], key, object, source); + + if ((result === result ? (result !== value) : (value === value)) || + (value === undefined && !(key in object))) { + object[key] = result; + } + } + return object; + } + /** * Creates a `_.countBy`, `_.groupBy`, `_.indexBy`, or `_.partition` function. * @@ -3300,7 +3330,7 @@ length -= argsHolders.length; if (length < arity) { - var newArgPos = argPos ? arrayCopy(argPos) : undefined, + var newArgPos = argPos ? copyArray(argPos) : undefined, newArity = nativeMax(arity - length, 0), newsHolders = isCurry ? argsHolders : undefined, newHoldersRight = isCurry ? undefined : argsHolders, @@ -3453,7 +3483,7 @@ bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG); partials = holders = undefined; } - length -= (holders ? holders.length : 0); + length -= holders ? holders.length : 0; if (bitmask & PARTIAL_RIGHT_FLAG) { var partialsRight = partials, holdersRight = holders; @@ -4011,20 +4041,20 @@ var value = source[3]; if (value) { var partials = data[3]; - data[3] = partials ? composeArgs(partials, value, source[4]) : arrayCopy(value); - data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : arrayCopy(source[4]); + data[3] = partials ? composeArgs(partials, value, source[4]) : copyArray(value); + data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : copyArray(source[4]); } // Compose partial right arguments. value = source[5]; if (value) { partials = data[5]; - data[5] = partials ? composeArgsRight(partials, value, source[6]) : arrayCopy(value); - data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : arrayCopy(source[6]); + data[5] = partials ? composeArgsRight(partials, value, source[6]) : copyArray(value); + data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : copyArray(source[6]); } // Use source `argPos` if available. value = source[7]; if (value) { - data[7] = arrayCopy(value); + data[7] = copyArray(value); } // Use source `ary` if it's smaller. if (srcBitmask & ARY_FLAG) { @@ -4110,7 +4140,7 @@ function reorder(array, indexes) { var arrLength = array.length, length = nativeMin(indexes.length, arrLength), - oldArray = arrayCopy(array); + oldArray = copyArray(array); while (length--) { var index = indexes[length]; @@ -4260,7 +4290,7 @@ function wrapperClone(wrapper) { return wrapper instanceof LazyWrapper ? wrapper.clone() - : new LodashWrapper(wrapper.__wrapped__, wrapper.__chain__, arrayCopy(wrapper.__actions__)); + : new LodashWrapper(wrapper.__wrapped__, wrapper.__chain__, copyArray(wrapper.__actions__)); } /*------------------------------------------------------------------------*/ @@ -8528,7 +8558,7 @@ } return (lodash.support.unindexedChars && isString(value)) ? value.split('') - : arrayCopy(value); + : copyArray(value); } /** @@ -8555,7 +8585,7 @@ * // => { 'a': 1, 'b': 2, 'c': 3 } */ function toPlainObject(value) { - return baseCopy(value, keysIn(value)); + return copyObject(value, keysIn(value)); } /*------------------------------------------------------------------------*/