Skip to content

Commit de4e75c

Browse files
committed
[Fix] ensure Buffer + non-Buffer comparison order does not matter
1 parent 5b3bf31 commit de4e75c

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ function objEquiv(a, b, opts) {
6767
return a.getTime() === b.getTime();
6868
}
6969

70-
if (isBuffer(a)) {
71-
if (!isBuffer(b)) {
72-
return false;
73-
}
70+
var aIsBuffer = isBuffer(a);
71+
var bIsBuffer = isBuffer(b);
72+
if (aIsBuffer !== bIsBuffer) { return false; }
73+
if (aIsBuffer || bIsBuffer) { // && would work too, because both are true or both false here
7474
if (a.length !== b.length) { return false; }
7575
for (i = 0; i < a.length; i++) {
7676
if (a[i] !== b[i]) { return false; }

test/cmp.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,19 @@ test('dates', function (t) {
7272

7373
test('buffers', function (t) {
7474
/* eslint no-buffer-constructor: 1, new-cap: 1 */
75-
t.ok(equal(Buffer('xyz'), Buffer('xyz')));
75+
t.ok(equal(Buffer('xyz'), Buffer('xyz')), 'buffers with same contents are equal');
76+
t.ok(equal(Buffer('xyz'), Buffer('xyz'), { strict: true }), 'strict: buffers with same contents are equal');
77+
78+
t.notOk(equal(Buffer('abc'), Buffer('xyz')), 'buffers with different contents are not equal');
79+
t.notOk(equal(Buffer('xyz'), Buffer('abc')), 'buffers with different contents are not equal');
80+
t.notOk(equal(Buffer('abc'), Buffer('xyz'), { strict: true }), 'strict: buffers with different contents are not equal');
81+
t.notOk(equal(Buffer('xyz'), Buffer('abc'), { strict: true }), 'strict: buffers with different contents are not equal');
82+
83+
t.notOk(equal(Buffer(''), []), 'empty buffer and empty array are not equal');
84+
t.notOk(equal([], Buffer('')), 'empty array and empty buffer are not equal');
85+
t.notOk(equal(Buffer(''), [], { strict: true }), 'strict: empty buffer and empty array are not equal');
86+
t.notOk(equal([], Buffer(''), { strict: true }), 'strict: empty array and empty buffer are not equal');
87+
7688
t.end();
7789
});
7890

0 commit comments

Comments
 (0)