diff --git a/.eslintrc b/.eslintrc index 411501b..4ec73d8 100755 --- a/.eslintrc +++ b/.eslintrc @@ -16,6 +16,7 @@ }, "globals": { + "DataView": false, "Int8Array": false, "Uint8Array": false, }, diff --git a/index.js b/index.js index dc78f5c..a587b5a 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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'); @@ -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; } @@ -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); diff --git a/package.json b/package.json index 829d3db..7dadffa 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/cmp.js b/test/cmp.js index 4c99395..1d05b1d 100644 --- a/test/cmp.js +++ b/test/cmp.js @@ -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(); }); @@ -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(); +});