Add arrayConcat and arrayPush helpers.

This commit is contained in:
jdalton
2015-06-01 00:14:19 -07:00
parent 88270a7b66
commit 642d77bb91

View File

@@ -736,7 +736,6 @@
clearTimeout = context.clearTimeout, clearTimeout = context.clearTimeout,
floor = Math.floor, floor = Math.floor,
parseFloat = context.parseFloat, parseFloat = context.parseFloat,
push = arrayProto.push,
propertyIsEnumerable = objectProto.propertyIsEnumerable, propertyIsEnumerable = objectProto.propertyIsEnumerable,
Set = getNative(context, 'Set'), Set = getNative(context, 'Set'),
setTimeout = context.setTimeout, setTimeout = context.setTimeout,
@@ -1335,6 +1334,30 @@
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/**
* Creates a new array joining `array` with `values`.
*
* @private
* @param {Array} array The array to join with `values`.
* @param {Array} values The values to join with `array`.
* @returns {Array} Returns the new concatenated array.
*/
function arrayConcat(array, values) {
var index = -1,
length = array.length,
valsIndex = -1,
valsLength = values.length,
result = Array(length + valsLength);
while (++index < length) {
result[index] = array[index];
}
while (++valsIndex < valsLength) {
result[index++] = values[valsIndex];
}
return result;
}
/** /**
* Copies the values of `source` to `array`. * Copies the values of `source` to `array`.
* *
@@ -1490,6 +1513,25 @@
return result; return result;
} }
/**
* Appends the elements of `values` to `array`.
*
* @private
* @param {Array} array The array to modify.
* @param {Array} values The values to append.
* @returns {Array} Returns `array`.
*/
function arrayPush(array, values) {
var index = -1,
length = values.length,
offset = array.length;
while (++index < length) {
array[offset + index] = values[index];
}
return array;
}
/** /**
* A specialized version of `_.reduce` for arrays without support for callback * A specialized version of `_.reduce` for arrays without support for callback
* shorthands and `this` binding. * shorthands and `this` binding.
@@ -2039,8 +2081,7 @@
result || (result = []); result || (result = []);
var index = -1, var index = -1,
length = array.length, length = array.length;
resIndex = result.length - 1;
while (++index < length) { while (++index < length) {
var value = array[index]; var value = array[index];
@@ -2049,17 +2090,11 @@
if (isDeep) { if (isDeep) {
// Recursively flatten arrays (susceptible to call stack limits). // Recursively flatten arrays (susceptible to call stack limits).
baseFlatten(value, isDeep, isStrict, result); baseFlatten(value, isDeep, isStrict, result);
resIndex = result.length - 1;
} else { } else {
var valIndex = -1, arrayPush(result, value);
valLength = value.length;
while (++valIndex < valLength) {
result[++resIndex] = value[valIndex];
}
} }
} else if (!isStrict) { } else if (!isStrict) {
result[++resIndex] = value; result[result.length] = value;
} }
} }
return result; return result;
@@ -2837,11 +2872,8 @@
length = actions.length; length = actions.length;
while (++index < length) { while (++index < length) {
var args = [result], var action = actions[index];
action = actions[index]; result = action.func.apply(action.thisArg, arrayPush([result], action.args));
push.apply(args, action.args);
result = action.func.apply(action.thisArg, args);
} }
return result; return result;
} }
@@ -2992,7 +3024,7 @@
argsLength = nativeMax(args.length - holdersLength, 0), argsLength = nativeMax(args.length - holdersLength, 0),
leftIndex = -1, leftIndex = -1,
leftLength = partials.length, leftLength = partials.length,
result = Array(argsLength + leftLength); result = Array(leftLength + argsLength);
while (++leftIndex < leftLength) { while (++leftIndex < leftLength) {
result[leftIndex] = partials[leftIndex]; result[leftIndex] = partials[leftIndex];
@@ -3641,7 +3673,7 @@
argsLength = arguments.length, argsLength = arguments.length,
leftIndex = -1, leftIndex = -1,
leftLength = partials.length, leftLength = partials.length,
args = Array(argsLength + leftLength); args = Array(leftLength + argsLength);
while (++leftIndex < leftLength) { while (++leftIndex < leftLength) {
args[leftIndex] = partials[leftIndex]; args[leftIndex] = partials[leftIndex];
@@ -4577,7 +4609,7 @@
* // => [1, 3] * // => [1, 3]
*/ */
var difference = restParam(function(array, values) { var difference = restParam(function(array, values) {
return isArrayLike(array) return (isObjectLike(array) && isArrayLike(array))
? baseDifference(array, baseFlatten(values, false, true)) ? baseDifference(array, baseFlatten(values, false, true))
: []; : [];
}); });
@@ -5792,7 +5824,7 @@
var array = arguments[index]; var array = arguments[index];
if (isArrayLike(array)) { if (isArrayLike(array)) {
var result = result var result = result
? baseDifference(result, array).concat(baseDifference(array, result)) ? arrayPush(baseDifference(result, array), baseDifference(array, result))
: array; : array;
} }
} }
@@ -6033,6 +6065,33 @@
return new LodashWrapper(this.value(), this.__chain__); return new LodashWrapper(this.value(), this.__chain__);
} }
/**
* Creates a new array joining a wrapped array with any additional arrays
* and/or values.
*
* @name concat
* @memberOf _
* @category Chain
* @param {...*} [values] The arrays and/or values to concatenate.
* @returns {Array} Returns the new concatenated array.
* @example
*
* var array = [1, 2];
* var wrapper = _(array).concat([3, 4]);
*
* console.log(wrapper.value());
* // => [1, 2, 3, 4]
*
* console.log(array);
* // => [1, 2]
*/
var wrapperConcat = restParam(function(values) {
values = baseFlatten(values);
return this.thru(function(array) {
return arrayConcat(isArray(array) ? array : [toObject(array)], values);
});
});
/** /**
* Creates a clone of the chained sequence planting `value` as the wrapped value. * Creates a clone of the chained sequence planting `value` as the wrapped value.
* *
@@ -11371,9 +11430,7 @@
result.__chain__ = chainAll; result.__chain__ = chainAll;
return result; return result;
} }
var args = [this.value()]; return func.apply(object, arrayPush([this.value()], arguments));
push.apply(args, arguments);
return func.apply(object, args);
}; };
}(func)); }(func));
} }
@@ -12195,9 +12252,7 @@
: lodashFunc.call(lodash, this.value()); : lodashFunc.call(lodash, this.value());
} }
var interceptor = function(value) { var interceptor = function(value) {
var otherArgs = [value]; return lodashFunc.apply(lodash, arrayPush([value], args));
push.apply(otherArgs, args);
return lodashFunc.apply(lodash, otherArgs);
}; };
if (useLazy) { if (useLazy) {
var wrapper = onlyLazy ? value : new LazyWrapper(this), var wrapper = onlyLazy ? value : new LazyWrapper(this),
@@ -12214,7 +12269,7 @@
}); });
// Add `Array` and `String` methods to `lodash.prototype`. // Add `Array` and `String` methods to `lodash.prototype`.
arrayEach(['concat', 'join', 'pop', 'push', 'replace', 'shift', 'sort', 'splice', 'split', 'unshift'], function(methodName) { arrayEach(['join', 'pop', 'push', 'replace', 'shift', 'sort', 'splice', 'split', 'unshift'], function(methodName) {
var protoFunc = (/^(?:replace|split)$/.test(methodName) ? stringProto : arrayProto)[methodName], var protoFunc = (/^(?:replace|split)$/.test(methodName) ? stringProto : arrayProto)[methodName],
chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru', chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
fixObjects = !support.spliceObjects && /^(?:pop|shift|splice)$/.test(methodName), fixObjects = !support.spliceObjects && /^(?:pop|shift|splice)$/.test(methodName),
@@ -12262,6 +12317,7 @@
// Add chaining functions to the `lodash` wrapper. // Add chaining functions to the `lodash` wrapper.
lodash.prototype.chain = wrapperChain; lodash.prototype.chain = wrapperChain;
lodash.prototype.commit = wrapperCommit; lodash.prototype.commit = wrapperCommit;
lodash.prototype.concat = wrapperConcat;
lodash.prototype.plant = wrapperPlant; lodash.prototype.plant = wrapperPlant;
lodash.prototype.reverse = wrapperReverse; lodash.prototype.reverse = wrapperReverse;
lodash.prototype.toString = wrapperToString; lodash.prototype.toString = wrapperToString;