Skip to content

Commit

Permalink
Merge branch 'fix-2875' (close PR #2876)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Sep 23, 2020
2 parents f5a9774 + 00d64f4 commit d9741b3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
11 changes: 11 additions & 0 deletions modules/_toDataView.js
@@ -0,0 +1,11 @@
import getByteLength from './_getByteLength.js';

// Internal function to wrap or shallow-copy an ArrayBuffer,
// typed array or DataView to a new DataView, reusing the buffer.
export default function toDataView(bufferSource) {
return new DataView(
bufferSource.buffer || bufferSource,
bufferSource.byteOffset,
getByteLength(bufferSource)
);
}
10 changes: 5 additions & 5 deletions modules/isEqual.js
Expand Up @@ -5,6 +5,7 @@ import isTypedArray from './isTypedArray.js';
import isFunction from './isFunction.js';
import keys from './keys.js';
import has from './_has.js';
import toDataView from './_toDataView.js';

// Internal recursive comparison function for `_.isEqual`.
function eq(a, b, aStack, bStack) {
Expand Down Expand Up @@ -53,12 +54,11 @@ function deepEq(a, b, aStack, bStack) {
return SymbolProto.valueOf.call(a) === SymbolProto.valueOf.call(b);
case '[object ArrayBuffer]':
// Coerce to `DataView` so we can fall through to the next case.
return deepEq(new DataView(a), new DataView(b), aStack, bStack);
return deepEq(toDataView(a), toDataView(b), aStack, bStack);
case '[object DataView]':
var byteLength = getByteLength(a);
if (byteLength !== getByteLength(b)) {
return false;
}
if (byteLength !== getByteLength(b)) return false;
if (a.buffer === b.buffer && a.byteOffset === b.byteOffset) return true;
while (byteLength--) {
if (a.getUint8(byteLength) !== b.getUint8(byteLength)) {
return false;
Expand All @@ -69,7 +69,7 @@ function deepEq(a, b, aStack, bStack) {

if (isTypedArray(a)) {
// Coerce typed arrays to `DataView`.
return deepEq(new DataView(a.buffer), new DataView(b.buffer), aStack, bStack);
return deepEq(toDataView(a), toDataView(b), aStack, bStack);
}

var areArrays = className === '[object Array]';
Expand Down
11 changes: 6 additions & 5 deletions test/objects.js
Expand Up @@ -583,8 +583,6 @@
assert.strictEqual(_.isEqual(symbol, sameStringSymbol), false, 'Different symbols of same string are not equal');
}



// typed arrays
if (typeof ArrayBuffer !== 'undefined') {
var u8 = new Uint8Array([1, 2]);
Expand All @@ -608,10 +606,13 @@
assert.notOk(_.isEqual(new DataView(u8.buffer), new DataView(u16one.buffer)), 'Different DataViews with different byte data are not equal');
assert.notOk(_.isEqual(u8.buffer, u16.buffer), 'Different ArrayBuffers with different length are not equal');
assert.notOk(_.isEqual(u8.buffer, u16one.buffer), 'Different ArrayBuffers with different byte data are not equal');
}

//assert.ok(_.isEqual(new DataView(u8.buffer)), new DataView(u8b.buffer))
//assert.notOk(_.isEqual(new DataView((new Uint8Array([1,2])).buffer), new DataView((new Uint8Array([5,6,10])).buffer));
// Regression tests for #2875.
var shared = new Uint8Array([1, 2, 3, 4]);
var view1 = new Uint8Array(shared.buffer, 0, 2);
var view2 = new Uint8Array(shared.buffer, 2, 2);
assert.notOk(_.isEqual(view1, view2), 'same buffer with different offset is not equal');
}
});

QUnit.test('isEmpty', function(assert) {
Expand Down
19 changes: 14 additions & 5 deletions underscore-esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore-esm.js.map

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions underscore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore.js.map

Large diffs are not rendered by default.

0 comments on commit d9741b3

Please sign in to comment.