Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},

"globals": {
"DataView": false,
"Int8Array": false,
"Uint8Array": false,
},
Expand Down
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

var assign = require('object.assign');
var callBound = require('call-bound');
var dataViewBuffer = require('data-view-buffer');
var dataViewByteLength = require('data-view-byte-length');
var dataViewByteOffset = require('data-view-byte-offset');
var flags = require('regexp.prototype.flags');
var GetIntrinsic = require('get-intrinsic');
var getIterator = require('es-get-iterator');
Expand All @@ -10,6 +13,7 @@ var is = require('object-is');
var isArguments = require('is-arguments');
var isArray = require('isarray');
var isArrayBuffer = require('is-array-buffer');
var isDataView = require('is-data-view');
var isDate = require('is-date-object');
var isRegex = require('is-regex');
var isSharedArrayBuffer = require('is-shared-array-buffer');
Expand Down Expand Up @@ -276,8 +280,12 @@ function mapEquiv(a, b, opts, channel) {
return true;
}

function dataViewBytes(view) {
return new Uint8Array(dataViewBuffer(view), dataViewByteOffset(view), dataViewByteLength(view));
}

function objEquiv(a, b, opts, channel) {
/* eslint max-statements: [2, 100], max-lines-per-function: [2, 120], max-depth: [2, 5], max-lines: [2, 400] */
/* eslint max-statements: [2, 105], max-lines-per-function: [2, 120], max-depth: [2, 5], max-lines: [2, 420] */
var i, key;

if (typeof a !== typeof b) { return false; }
Expand Down Expand Up @@ -354,6 +362,13 @@ function objEquiv(a, b, opts, channel) {
return typeof Uint8Array === 'function' && internalDeepEqual(new Uint8Array(a), new Uint8Array(b), opts, channel);
}

var aIsDataView = isDataView(a);
var bIsDataView = isDataView(b);
if (aIsDataView !== bIsDataView) { return false; }
if (aIsDataView && !internalDeepEqual(dataViewBytes(a), dataViewBytes(b), opts, channel)) { // bIsDataView is the same here
return false;
}

if (typeof a !== typeof b) { return false; }

var ka = objectKeys(a);
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@
"dependencies": {
"array-buffer-byte-length": "^1.0.2",
"call-bound": "^1.0.4",
"data-view-buffer": "^1.0.2",
"data-view-byte-length": "^1.0.2",
"data-view-byte-offset": "^1.0.1",
"es-get-iterator": "^1.1.3",
"get-intrinsic": "^1.3.0",
"is-arguments": "^1.2.0",
"is-array-buffer": "^3.0.5",
"is-data-view": "^1.0.2",
"is-date-object": "^1.1.0",
"is-regex": "^1.2.1",
"is-shared-array-buffer": "^1.0.4",
Expand Down
76 changes: 76 additions & 0 deletions test/cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,70 @@ test('TypedArrays', { skip: !hasTypedArrays }, function (t) {
st.end();
});

t.test('DataViews', { skip: typeof DataView !== 'function' }, function (st) {
st.deepEqualTest(
new DataView(new Uint8Array([1, 2, 3]).buffer),
new DataView(new Uint8Array([1, 2, 3]).buffer),
'DataViews with the same contents',
true,
true
);

st.deepEqualTest(
new DataView(new Uint8Array([1, 2, 3]).buffer),
new DataView(new Uint8Array([4, 5, 6]).buffer),
'DataViews with different contents',
false,
false
);

st.deepEqualTest(
new DataView(new ArrayBuffer(2)),
new DataView(new ArrayBuffer(3)),
'DataViews with different byte lengths',
false,
false
);

var bytes = new Uint8Array([1, 2, 1, 2]);

// node < 0.12 puts `buffer`, `byteOffset`, and `byteLength` on each DataView as own enumerable properties
var hasOwnByteOffset = Object.prototype.hasOwnProperty.call(new DataView(new ArrayBuffer(1)), 'byteOffset');

st.test('at different offsets, with the same contents', { skip: hasOwnByteOffset }, function (s2t) {
s2t.deepEqualTest(
new DataView(bytes.buffer, 0, 2),
new DataView(bytes.buffer, 2, 2),
'DataViews at different offsets, with the same contents',
true,
true
);

s2t.end();
});

st.deepEqualTest(
new DataView(bytes.buffer, 0, 2),
new DataView(bytes.buffer, 1, 2),
'DataViews at different offsets, with different contents',
false,
false
);

var withKey = new DataView(new ArrayBuffer(1));
withKey.foo = 'bar';

st.deepEqualTest(
withKey,
new DataView(new ArrayBuffer(1)),
'DataViews with the same contents, one with an extra key',
false,
false
);

st.end();
});

t.end();
});

Expand Down Expand Up @@ -1575,3 +1639,15 @@ test('SharedArrayBuffer vs non-SharedArrayBuffer', { skip: typeof SharedArrayBuf

t.end();
});

test('DataView vs non-DataView', { skip: typeof DataView !== 'function' || !hasSymbols || !Symbol.toStringTag }, function (t) {
t.deepEqualTest(
new DataView(new ArrayBuffer(0)),
tag({}, 'DataView'),
'DataView vs fake DataView',
false,
false
);

t.end();
});
Loading