Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: objects without valueOf and toString function do not throw error #134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion spec/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
},
Expand Down Expand Up @@ -397,4 +403,4 @@ module.exports = [
];

function func1() {}
function func2() {}
function func2() {}
4 changes: 2 additions & 2 deletions src/index.jst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down