diff --git a/spec/tests.js b/spec/tests.js index 1537f1d..ec41a88 100644 --- a/spec/tests.js +++ b/spec/tests.js @@ -225,6 +225,12 @@ module.exports = [ value1: {toString: ()=>'Hello world!'}, value2: {toString: ()=>'Hi!'}, equal: false + }, + { + description: 'objects without `valueOf` and `toString` function do not throw error', + value1: Object.assign(Object.create(null), { a: 1 }), + value2: Object.assign(Object.create(null), { a: 1 }), + equal: true } ] }, @@ -397,4 +403,4 @@ module.exports = [ ]; function func1() {} -function func2() {} +function func2() {} \ No newline at end of file diff --git a/src/index.jst b/src/index.jst index 5d4ee2f..e247aef 100644 --- a/src/index.jst +++ b/src/index.jst @@ -48,8 +48,8 @@ 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 && typeof a.valueOf === 'function' && typeof b.valueOf === 'function') return a.valueOf() === b.valueOf(); + if (a.toString !== Object.prototype.toString && typeof a.toString === 'function' && typeof b.toString === 'function') return a.toString() === b.toString(); keys = Object.keys(a); length = keys.length;