Cleanup _.transform and make baseEach and baseEachRight use toObject.

This commit is contained in:
John-David Dalton
2014-09-16 01:00:36 -07:00
parent 532be6cd87
commit 3ab19e81ac

View File

@@ -1481,7 +1481,6 @@
? baseClone(valValue, isDeep, null, stackA, stackB) ? baseClone(valValue, isDeep, null, stackA, stackB)
: valClone; : valClone;
}); });
return result; return result;
} }
@@ -1589,7 +1588,7 @@
return baseForOwn(collection, iteratee); return baseForOwn(collection, iteratee);
} }
var index = -1, var index = -1,
iterable = toIterable(collection); iterable = toObject(collection);
while (++index < length) { while (++index < length) {
if (iteratee(iterable[index], index, iterable) === false) { if (iteratee(iterable[index], index, iterable) === false) {
@@ -1613,7 +1612,7 @@
if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) { if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) {
return baseForOwnRight(collection, iteratee); return baseForOwnRight(collection, iteratee);
} }
var iterable = toIterable(collection); var iterable = toObject(collection);
while (length--) { while (length--) {
if (iteratee(iterable[length], length, iterable) === false) { if (iteratee(iterable[length], length, iterable) === false) {
break; break;
@@ -2142,7 +2141,6 @@
} }
object[key] = result; object[key] = result;
}); });
return object; return object;
} }
@@ -2485,9 +2483,9 @@
*/ */
function createAggregator(setter, initializer) { function createAggregator(setter, initializer) {
return function(collection, iteratee, thisArg) { return function(collection, iteratee, thisArg) {
var result = initializer ? initializer() : {};
iteratee = getCallback(iteratee, thisArg, 3); iteratee = getCallback(iteratee, thisArg, 3);
var result = initializer ? initializer() : {};
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
length = collection.length; length = collection.length;
@@ -3146,14 +3144,10 @@
if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) { if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) {
return values(value); return values(value);
} }
value = toObject(value);
if (support.unindexedChars && isString(value)) { if (support.unindexedChars && isString(value)) {
var index = -1; return value.split('');
while (++index < length) {
value[index] = value.charAt(index);
}
} }
return value; return toObject(value);
} }
/** /**
@@ -3164,7 +3158,15 @@
* @returns {Object} Returns the object. * @returns {Object} Returns the object.
*/ */
function toObject(value) { function toObject(value) {
return isObject(value) ? value : Object(value); var result = isObject(value) ? value : Object(value);
if (support.unindexedChars && isString(value)) {
var index = -1;
while (++index < length) {
result[index] = value.charAt(index);
}
}
return result;
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
@@ -7620,7 +7622,7 @@
var Ctor = object.constructor, var Ctor = object.constructor,
length = object.length; length = object.length;
if ((Ctor && object === Ctor.prototype) || if ((Ctor && Ctor.prototype === object) ||
(typeof length == 'number' && length > 0) || (typeof length == 'number' && length > 0) ||
(support.enumPrototypes && typeof object == 'function')) { (support.enumPrototypes && typeof object == 'function')) {
return shimKeys(object); return shimKeys(object);
@@ -7662,7 +7664,7 @@
var keyIndex, var keyIndex,
Ctor = object.constructor, Ctor = object.constructor,
index = -1, index = -1,
isProto = Ctor && object === Ctor.prototype, isProto = Ctor && Ctor.prototype === object,
maxIndex = length - 1, maxIndex = length - 1,
result = Array(length), result = Array(length),
skipIndexes = length > 0, skipIndexes = length > 0,
@@ -7739,9 +7741,9 @@
* // => { 'fred': 40, 'pebbles': 1 } * // => { 'fred': 40, 'pebbles': 1 }
*/ */
function mapValues(object, iteratee, thisArg) { function mapValues(object, iteratee, thisArg) {
var result = {};
iteratee = getCallback(iteratee, thisArg, 3); iteratee = getCallback(iteratee, thisArg, 3);
var result = {}
baseForOwn(object, function(value, key, object) { baseForOwn(object, function(value, key, object) {
result[key] = iteratee(value, key, object); result[key] = iteratee(value, key, object);
}); });
@@ -7933,25 +7935,22 @@
* // => { 'a': 3, 'b': 6, 'c': 9 } * // => { 'a': 3, 'b': 6, 'c': 9 }
*/ */
function transform(object, iteratee, accumulator, thisArg) { function transform(object, iteratee, accumulator, thisArg) {
var isArr = isArrayLike(object); iteratee = getCallback(iteratee, thisArg, 4);
var isArr = isArrayLike(object);
if (accumulator == null) { if (accumulator == null) {
if (isArr) { if (isArr) {
accumulator = []; accumulator = [];
} else if (isObject(object)) {
var Ctor = object.constructor;
accumulator = baseCreate(typeof Ctor == 'function' && Ctor.prototype);
} else { } else {
if (isObject(object)) { accumulator = {};
var Ctor = object.constructor,
proto = Ctor && Ctor.prototype;
}
accumulator = baseCreate(proto);
} }
} }
if (iteratee) { (isArr ? arrayEach : baseForOwn)(object, function(value, index, object) {
iteratee = getCallback(iteratee, thisArg, 4); return iteratee(accumulator, value, index, object);
(isArr ? arrayEach : baseForOwn)(object, function(value, index, object) { });
return iteratee(accumulator, value, index, object);
});
}
return accumulator; return accumulator;
} }