mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
Treat arguments objects like plain objects.
This commit is contained in:
committed by
jdalton
parent
edffe2a6fc
commit
bdbafb7a2a
@@ -1826,10 +1826,7 @@
|
||||
var tag = objToString.call(value),
|
||||
isFunc = tag == funcTag;
|
||||
|
||||
if (!lodash.support.argsTag && isArguments(value)) {
|
||||
tag = argsTag;
|
||||
}
|
||||
if (tag == objectTag || (isFunc && !object)) {
|
||||
if (tag == argsTag || tag == objectTag || (isFunc && !object)) {
|
||||
if (isHostObject(value)) {
|
||||
return object ? value : {};
|
||||
}
|
||||
@@ -2289,24 +2286,18 @@
|
||||
|
||||
if (!objIsArr) {
|
||||
objTag = objToString.call(object);
|
||||
if (isLength(object.length)) {
|
||||
if (isArguments(object)) {
|
||||
object = arrayToObject(object);
|
||||
objTag = objectTag;
|
||||
} else if (objTag != objectTag) {
|
||||
objIsArr = isTypedArray(object);
|
||||
}
|
||||
if (objTag == argsTag) {
|
||||
objTag = objectTag;
|
||||
} else if (objTag != objectTag) {
|
||||
objIsArr = isTypedArray(object);
|
||||
}
|
||||
}
|
||||
if (!othIsArr) {
|
||||
othTag = objToString.call(other);
|
||||
if (isLength(other.length)) {
|
||||
if (isArguments(other)) {
|
||||
other = arrayToObject(other);
|
||||
othTag = objectTag;
|
||||
} else if (othTag != objectTag) {
|
||||
othIsArr = isTypedArray(other);
|
||||
}
|
||||
if (othTag == argsTag) {
|
||||
othTag = objectTag;
|
||||
} else if (othTag != objectTag) {
|
||||
othIsArr = isTypedArray(other);
|
||||
}
|
||||
}
|
||||
var objIsObj = objTag == objectTag && !isHostObject(object),
|
||||
@@ -2528,7 +2519,7 @@
|
||||
}
|
||||
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
||||
result = isArguments(value)
|
||||
? arrayToObject(value)
|
||||
? toPlainObject(value)
|
||||
: (isPlainObject(value) ? value : {});
|
||||
}
|
||||
}
|
||||
@@ -3666,11 +3657,6 @@
|
||||
function initCloneByTag(object, tag, isDeep) {
|
||||
var Ctor = object.constructor;
|
||||
switch (tag) {
|
||||
case argsTag:
|
||||
var result = new Ctor;
|
||||
result.length = object.length;
|
||||
return arrayCopy(object, result);
|
||||
|
||||
case arrayBufferTag:
|
||||
return bufferClone(object);
|
||||
|
||||
@@ -3693,7 +3679,7 @@
|
||||
return new Ctor(object);
|
||||
|
||||
case regexpTag:
|
||||
result = new Ctor(object.source, reFlags.exec(object));
|
||||
var result = new Ctor(object.source, reFlags.exec(object));
|
||||
result.lastIndex = object.lastIndex;
|
||||
}
|
||||
return result;
|
||||
@@ -4031,17 +4017,6 @@
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts `array` to a plain array-like object.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} array The array to convert.
|
||||
* @returns {Object} Returns the converted object.
|
||||
*/
|
||||
function arrayToObject(array) {
|
||||
return arrayCopy(array, { 'length': array.length });
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts `value` to an array-like object if it is not one.
|
||||
*
|
||||
@@ -8366,9 +8341,7 @@
|
||||
* // => { 'a': 1, 'b': 2, 'c': 3 }
|
||||
*/
|
||||
function toPlainObject(value) {
|
||||
return (isArray(value) || isArguments(value) || isTypedArray(value))
|
||||
? arrayToObject(value)
|
||||
: baseCopy(value, keysIn(value));
|
||||
return baseCopy(value, keysIn(value));
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
35
test/test.js
35
test/test.js
@@ -1786,9 +1786,10 @@
|
||||
});
|
||||
|
||||
test('`_.' + methodName + '` should clone array buffers', 2, function() {
|
||||
var buffer = ArrayBuffer && new ArrayBuffer(10);
|
||||
if (buffer) {
|
||||
var actual = func(buffer);
|
||||
if (ArrayBuffer) {
|
||||
var buffer = new ArrayBuffer(10),
|
||||
actual = func(buffer);
|
||||
|
||||
strictEqual(actual.byteLength, buffer.byteLength);
|
||||
notStrictEqual(actual, buffer);
|
||||
}
|
||||
@@ -6585,7 +6586,7 @@
|
||||
|
||||
test('should treat `arguments` objects like `Object` objects', 4, function() {
|
||||
var args = (function() { return arguments; }(1, 2, 3)),
|
||||
object = { '0': 1, '1': 2, '2': 3, 'length': 3 };
|
||||
object = { '0': 1, '1': 2, '2': 3 };
|
||||
|
||||
function Foo() {}
|
||||
Foo.prototype = object;
|
||||
@@ -8927,7 +8928,7 @@
|
||||
test('should merge `arguments` objects', 3, function() {
|
||||
var object1 = { 'value': args },
|
||||
object2 = { 'value': { '3': 4 } },
|
||||
expected = { '0': 1, '1': 2, '2': 3, '3': 4, 'length': 3 },
|
||||
expected = { '0': 1, '1': 2, '2': 3, '3': 4 },
|
||||
actual = _.merge(object1, object2);
|
||||
|
||||
ok(!_.isArguments(actual.value));
|
||||
@@ -8947,9 +8948,9 @@
|
||||
var arrays = [array2, array1, array4, array3, array2, array4, array4, array3, array2],
|
||||
buffer = ArrayBuffer && new ArrayBuffer(8);
|
||||
|
||||
if (root.Float64Array) {
|
||||
// juggle for `Float64Array` shim
|
||||
arrays[1] = _.size(new Float64Array(buffer)) > 1 ? array4 : array1;
|
||||
// juggle for `Float64Array` shim
|
||||
if (root.Float64Array && (new Float64Array(buffer)).length == 8) {
|
||||
arrays[1] = array4;
|
||||
}
|
||||
var expected = _.map(typedArrays, function(type, index) {
|
||||
var array = arrays[index].slice();
|
||||
@@ -13213,30 +13214,30 @@
|
||||
|
||||
test('should convert `arguments` objects to plain objects', 1, function() {
|
||||
var actual = _.toPlainObject(args),
|
||||
expected = { '0': 1, '1': 2, '2': 3, 'length': 3 };
|
||||
expected = { '0': 1, '1': 2, '2': 3 };
|
||||
|
||||
deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
test('should convert arrays to plain objects', 1, function() {
|
||||
var actual = _.toPlainObject(['a', 'b', 'c']),
|
||||
expected = { '0': 'a', '1': 'b', '2': 'c', 'length': 3 };
|
||||
expected = { '0': 'a', '1': 'b', '2': 'c' };
|
||||
|
||||
deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
test('should convert typed arrays to plain objects', 1, function() {
|
||||
var object1 = { '0': 0, 'length': 1 },
|
||||
object2 = { '0': 0, '1': 0, 'length': 2 },
|
||||
object3 = { '0': 0, '1': 0, '2': 0, '3': 0, 'length': 4 },
|
||||
object4 = { '0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, 'length': 8 };
|
||||
var object1 = { '0': 0 },
|
||||
object2 = { '0': 0, '1': 0 },
|
||||
object3 = { '0': 0, '1': 0, '2': 0, '3': 0 },
|
||||
object4 = { '0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0 };
|
||||
|
||||
var objects = [object2, object1, object4, object3, object2, object4, object4, object3, object2],
|
||||
buffer = ArrayBuffer && new ArrayBuffer(8);
|
||||
|
||||
if (root.Float64Array) {
|
||||
// juggle for `Float64Array` shim
|
||||
objects[1] = _.size(new Float64Array(buffer)) > 1 ? object4 : object1;
|
||||
// juggle for `Float64Array` shim
|
||||
if (root.Float64Array && (new Float64Array(buffer)).length == 8) {
|
||||
objects[1] = object4;
|
||||
}
|
||||
var expected = _.map(typedArrays, function(type, index) {
|
||||
return root[type] ? objects[index] : false;
|
||||
|
||||
Reference in New Issue
Block a user