diff --git a/spec/tests.js b/spec/tests.js index 1537f1d..180a0b3 100644 --- a/spec/tests.js +++ b/spec/tests.js @@ -225,7 +225,19 @@ module.exports = [ value1: {toString: ()=>'Hello world!'}, value2: {toString: ()=>'Hi!'}, equal: false - } + }, + { + description: "objects with `valueOf` property that isn't a function don't throw", + value1: {valueOf: {}}, + value2: {valueOf: {}}, + equal: true + }, + { + description: "objects with `toString` property that isn't a function don't throw", + value1: {toString: {}}, + value2: {toString: {}}, + equal: true + }, ] }, diff --git a/src/index.jst b/src/index.jst index 5d4ee2f..be8614d 100644 --- a/src/index.jst +++ b/src/index.jst @@ -48,8 +48,12 @@ module.exports = function equal(a, b) { {{?}} if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags; - if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf(); - if (a.toString !== Object.prototype.toString) return a.toString() === b.toString(); + if ((a.valueOf !== Object.prototype.valueOf || b.valueOf !== Object.prototype.valueOf) + && typeof a.valueOf === 'function' && typeof b.valueOf === 'function') + return a.valueOf() === b.valueOf(); + if ((a.toString !== Object.prototype.toString || b.toString !== Object.prototype.toString) + && typeof a.toString === 'function' && typeof b.toString === 'function') + return a.toString() === b.toString(); keys = Object.keys(a); length = keys.length;