mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Ensure shallow clones of typed arrays don't clone the buffer.
This commit is contained in:
@@ -1426,7 +1426,8 @@
|
|||||||
if (Ctor instanceof Ctor) {
|
if (Ctor instanceof Ctor) {
|
||||||
Ctor = ctorByClass[className];
|
Ctor = ctorByClass[className];
|
||||||
}
|
}
|
||||||
return new Ctor(cloneBuffer(value.buffer), value.byteOffset, value.length);
|
var buffer = value.buffer;
|
||||||
|
return new Ctor(isDeep ? cloneBuffer(buffer) : buffer, value.byteOffset, value.length);
|
||||||
|
|
||||||
case numberClass:
|
case numberClass:
|
||||||
case stringClass:
|
case stringClass:
|
||||||
|
|||||||
16
test/test.js
16
test/test.js
@@ -616,7 +616,7 @@
|
|||||||
if (root.Uint8Array) {
|
if (root.Uint8Array) {
|
||||||
try {
|
try {
|
||||||
var array = new Uint8Array(new ArrayBuffer(8));
|
var array = new Uint8Array(new ArrayBuffer(8));
|
||||||
actual = lodashBizarro.clone(array);
|
actual = lodashBizarro.cloneDeep(array);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
actual = null;
|
actual = null;
|
||||||
}
|
}
|
||||||
@@ -1475,8 +1475,7 @@
|
|||||||
|
|
||||||
_.each(['clone', 'cloneDeep'], function(methodName) {
|
_.each(['clone', 'cloneDeep'], function(methodName) {
|
||||||
var func = _[methodName],
|
var func = _[methodName],
|
||||||
isDeep = methodName == 'cloneDeep',
|
isDeep = methodName == 'cloneDeep';
|
||||||
klass = new Klass;
|
|
||||||
|
|
||||||
_.forOwn(objects, function(object, key) {
|
_.forOwn(objects, function(object, key) {
|
||||||
test('`_.' + methodName + '` should clone ' + key, 2, function() {
|
test('`_.' + methodName + '` should clone ' + key, 2, function() {
|
||||||
@@ -1520,14 +1519,14 @@
|
|||||||
test('`_.' + methodName + '` should clone ' + type + ' arrays', 10, function() {
|
test('`_.' + methodName + '` should clone ' + type + ' arrays', 10, function() {
|
||||||
var Ctor = root[type];
|
var Ctor = root[type];
|
||||||
if (Ctor) {
|
if (Ctor) {
|
||||||
var array = new Ctor(new ArrayBuffer(24));
|
|
||||||
|
|
||||||
_.times(2, function(index) {
|
_.times(2, function(index) {
|
||||||
var actual = index ? func(array, 8, 1) : func(array);
|
var buffer = new ArrayBuffer(24),
|
||||||
|
array = index ? new Ctor(buffer, 8, 1) : new Ctor(buffer),
|
||||||
|
actual = func(array);
|
||||||
|
|
||||||
deepEqual(actual, array);
|
deepEqual(actual, array);
|
||||||
notStrictEqual(actual, array);
|
notStrictEqual(actual, array);
|
||||||
notStrictEqual(actual.buffer, array.buffer);
|
strictEqual(actual.buffer === array.buffer, !isDeep);
|
||||||
strictEqual(actual.byteOffset, array.byteOffset);
|
strictEqual(actual.byteOffset, array.byteOffset);
|
||||||
strictEqual(actual.length, array.length);
|
strictEqual(actual.length, array.length);
|
||||||
});
|
});
|
||||||
@@ -1558,7 +1557,8 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should provide the correct `callback` arguments', 1, function() {
|
test('`_.' + methodName + '` should provide the correct `callback` arguments', 1, function() {
|
||||||
var argsList = [];
|
var argsList = [],
|
||||||
|
klass = new Klass;
|
||||||
|
|
||||||
func(klass, function() {
|
func(klass, function() {
|
||||||
argsList.push(slice.call(arguments));
|
argsList.push(slice.call(arguments));
|
||||||
|
|||||||
Reference in New Issue
Block a user