Skip to content

Instantly share code, notes, and snippets.

@wizardnet972
Created January 6, 2017 13:31
Show Gist options
  • Save wizardnet972/529a0b042e7e1322b12ced413a4fb7b1 to your computer and use it in GitHub Desktop.
Save wizardnet972/529a0b042e7e1322b12ced413a4fb7b1 to your computer and use it in GitHub Desktop.
function coercing_equal(left, right) {
if (left === right) {
return true ;
}
if (left === null) {
return right === undefined;
}
if (right === null) {
return left === undefined;
}
if (typeof left === 'number' && typeof right === 'string') {
return left === +right;
}
if (typeof left === 'string' && typeof right === 'number') {
return +left === right;
}
if (typeof left === 'boolean') {
return coercing_equal(+left, right);
}
if (typeof right === 'boolean') {
return coercing_equal(left, +right);
}
if
(typeof left === 'object' &&
(
left.constructor === Number ||
left.constructor === String ||
left.constructor === Boolean
) &&
(typeof right === 'string' || typeof right === 'number')
) {
return coercing_equal(left.valueOf(), right);
}
if (
(typeof left === 'string' || typeof left === 'number') &&
typeof right === 'object' &&
(
right.constructor === Number ||
right.constructor === String ||
right.constructor === Boolean
)
) {
return coercing_equal(left, right.valueOf());
}
return false ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment