Created
January 6, 2017 13:31
-
-
Save wizardnet972/529a0b042e7e1322b12ced413a4fb7b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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