/strictEquals.js Secret
-
Star
(190)
You must be signed in to star a gist -
Fork
(19)
You must be signed in to fork a gist
-
-
Save gaearon/08a85a33e3d08f3f2ca25fb17bd9d638 to your computer and use it in GitHub Desktop.
| // Your scientists were so preoccupied | |
| // with whether or not they could, | |
| // they didn't stop to think if they should. | |
| // Like a === b | |
| function strictEquals(a, b) { | |
| if (Object.is(a, b)) { | |
| // Same value. | |
| // Is this NaN? | |
| if (Object.is(a, NaN)) { // We already know a and b are the same, so it's enough to check a. | |
| // Special case #1. | |
| return false; | |
| } else { | |
| // They are equal! | |
| return true; | |
| } | |
| } else { | |
| // Different value. | |
| // Are these 0 and -0? | |
| if ( | |
| (Object.is(a, 0) && Object.is(b, -0)) || | |
| (Object.is(a, -0) && Object.is(b, 0)) | |
| ) { | |
| // Special case #2. | |
| return true; | |
| } else { | |
| // They are not equal! | |
| return false; | |
| } | |
| } | |
| } |
const strictEquals = (a, b) => {
if (!Object.is(a, b) && Object.is(a, -b) && Object.is(-a, b)) return true;
if (Number.isNaN(a) && Number.isNaN(b)) return false;
return Object.is(a, b);
};const strictEquals = (a, b) => { if (!Object.is(a, b) && Object.is(a, -b) && Object.is(-a, b)) return true; if (Number.isNaN(a) && Number.isNaN(b)) return false; return Object.is(a, b); };
@tobias-edwards your implementation won't work for negative-positive check on numbers other than 0. (try -1 === 1)
const strictEquals = (a, b) => { if (!Object.is(a, b) && Object.is(a, -b) && Object.is(-a, b)) return true; if (Number.isNaN(a) && Number.isNaN(b)) return false; return Object.is(a, b); };@tobias-edwards your implementation won't work for negative-positive check on numbers other than 0. (try -1 === 1)
Ah yes, you're right! Thanks. I'll leave my wrong solution to preserve chat integrity
function strictEquals(a: any, b: any) {
if(Number.isNaN(a) && Number.isNaN(b)) return !Object.is(a, b)
if(Object.is(a, 0) && Object.is(b, -0) || Object.is(a, -0) && Object.is(b, 0)) {
return true
}
return Object.is(a, b);
}const strictEqual = (a, b) => {
if(Number.isNaN(a) && Number.isNaN(b)){
return false
}
else if(Object.is(a, -0) && Object.is(b, 0)) {
return true
}
else if(Object.is(a, 0) && Object.is(b, -0)) {
return true
}
else {
return Object.is(a, b)
}
}am I missing something?
function strictEquals(a,b) { // consider below 2 special cases // -0, 0 // NaN NaN if (Object.is(a, NaN) && Object.is(b, NaN)) return false; if (Object.is(a, 0) && Object.is(b, -0)) return true; return Object.is(a, b); }
you are not holding the case
if (Object.is(a, -0) && Object.is(b, 0)) return true;
Hi, here is my solution::
const myInfinite = 1/0; // = Infinite
const myCero = 0/myInfinite; // = 0
function stringEquals(a: Number, b: Number){
if(Number.isNaN(a) && Number.isNaN(b))
return false;
if(Object.is(a, myCero) && Object.is(b, -myCero))
return true
if(Object.is(a, -myCero) && Object.is(b, myCero))
return true
return Object.is(a, b);
}
let a, b;
a=1;
b=1;
stringEquals(a,b);
console.log(a===b);
console.log(Object.is(a,b));
a=1;
b=2;
stringEquals(a,b);
console.log(a===b);
console.log(Object.is(a,b));
a=0;
b=0;
stringEquals(a,b);
console.log(a===b);
console.log(Object.is(a,b));
a=NaN;
b=NaN;
stringEquals(a,b);
console.log(a===b);
console.log("It must be different:: ",Object.is(a,b));
a=0;
b=-0;
stringEquals(a,b);
console.log(a===b);
console.log("It must be different:: ",Object.is(a,b));
a=-0;
b=0;
stringEquals(a,b);
console.log(a===b);
console.log("It must be different:: ",Object.is(a,b));
My Function is like this
function strictEquals(a,b) {
if (
(Object.is(a, 0) && Object.is(b, -0)) ||
(Object.is(a, -0) && Object.is(b, 0))
) {
return true;
}
if (Number.isNaN(a) && Number.isNaN(b)){
return false;
}
return Object.is(a,b)
}
console.log(strictEquals(NaN,NaN));
Many already made it.
My version:
function strictEquals(a, b) {
if (Number.isNaN(a) && Number.isNaN(b)) return !Object.is(a, b);
if ((Object.is(a, -0) ^ Object.is(b, -0)) && (Object.is(a, 0) ^ Object.is(b, 0))) return !Object.is(a, b);
return Object.is(a, b);
}Hope this is the shortest
function strictEquals(a,b){
if(Number.isNaN(a) && Number.isNaN(b)) return false
if(!Object.is(a,b) && Object.is(a+"",b+"")) return true
return Object.is(a,b)
}
function stringEquals(a, b) { if (isNaN(a)) return false if ([0, -0].includes(a) && Object.is(a, -b)) return true return Object.is(a, b) }First special case NaN === NaN => both a and b will be NaN and therefore, and if either value is NaN, should return false. Other case, return the result of Objects.is(a, b)
Second sepecial case 0 === -0 or -0 === 0 => if the a is 0 or -0 and Object.is(a -b), its one of the special situations, that needs to return true. Otherwise, return Object.is(a, b)
I found it interesting that you managed to shorten if (isNaN(a) && isNaN(b)) return false to if (isNaN(a)) return false. You don't need to check if either value is NaN, you have enough with checking if the first value is NaN, in which case it will definitely evaluate to false. And you don't need to care about the remaining case in which b is NaN while a isn't, because Object.is() will handle it perfectly.
function strictEquals(a,b){
if(typeof a === 'number' && typeof b === 'number'){
return a == b
}
return Object.is(a,b)
}
I couldn't figure it out on my own so I looked at your version and tried to minimize (not simplify!) it to the best of my ability
const strictEquals = (a,b) => (Object.is(a,b)
? !Object.is(a, NaN)
: (Object.is(a, 0) && Object.is(b, -0)) || (Object.is(a, -0) && Object.is(b, 0)))
}function strictEquals(a, b) {
if(Object.is(a, NaN) && Object.is(b, NaN)) return false;
if(Object.is(a, -0) && Object.is(b, 0)) return true;
if(Object.is(b, -0) && Object.is(a, 0)) return true;
return Object.is(a, b);
}
function strictEquals(a,b){
if(Number.isNaN(a)&&Number.isNaN(b)){
return false;
}
if(Number.isFinite(a) && Number.isFinite(b) && Math.abs(a)===0 && Math.abs(b)===0){
return true;
}
return Obejct.is(a,b);
}const strictEquals = (a, b) => {
if (Object.is(a, NaN) && Object.is(NaN, b)) {
return false;
} else if (Object.is(a, 0) && Object.is(a, -0)) {
return true
} else if (Object.is(a, -0) && Object.is(b, 0)) {
return true
}
return Object.is(a,b)
}
const strictEquals = (a, b) => {
const isSameValue = Object.is(a, b)
// === behaves as same as Object.is() checks the both args are of same value or not
// edge cases
// 0 === -0 || -0 === 0 // true
// NaN === NaN // false
if(isSameValue && Object.is(a, NaN)){
return false
}
if((Object.is(a , 0) && Object.is(b, -0)) || (Object.is(a, -0) && Object.is(b, 0))){
return true
}
return isSameValue
}
This is my solution:
function strictEquals(a, b) {
if (Number.isNaN(a) && Number.isNaN(b)) {
return false;
}
if (
(Object.is(a, 0) && Object.is(b, -0)) ||
(Object.is(a, -0) && Object.is(b, 0))
) {
return true;
}
return Object.is(a, b);
}function strictEquals(a, b) {
// handle NaN === NaN is false case
if (Object.is(a, NaN) && Object.is(b, NaN)) {
return false;
}
// handle 0 === -0 is true case
else if (Object.is(a, 0) && Object.is(b, -0)) {
return true;
}
// handle -0 === 0 is true case
else if (Object.is(a, -0) && Object.is(b, 0)) {
return true;
}
return Object.is(a, b);
}
console.assert(strictEquals(-0, 0) === true);
console.assert(strictEquals(0, -0) === true);
console.assert(strictEquals(NaN, NaN) === false);
console.assert(strictEquals(-1, -1) === true);
console.assert(strictEquals(-1, -2) === false);
console.assert(strictEquals(1, 1) === true);
console.assert(strictEquals(1, 2) === false);Here is my implementation:
function strictEquals(a, b) {
if (
(Object.is(a, 0) && Object.is(b, -0)) ||
(Object.is(a, -0) && Object.is(b, 0))
) {
return true;
} else if (Number.isNaN(a) || Number.isNaN(b)) {
return false;
} else {
return Object.is(a, b);
}
}This is my solution (better late than never):
function strictEquals(a,b){
if(Number.isNaN(a) && Number.isNaN(b)){
return false
}else if([0,-0].includes(a) && [0,-0].includes(b)){
return true;
}else{
return Object.is(a,b);
}
}function strictEquals(a, b) {
if (Number.isNaN(a) && Number.isNaN(b)) return false;
if ([0, -0].includes(a) && [0, -0].includes(b)) return true;
return Object.is(a, b);
}I'd like to leave my attempt:
function strictEquals(a, b) {
// Ensuring no NaN shennanigans
if(Object.is(a, NaN) && Object.is(b, NaN)) return false;
// Ensuring no -0 / 0 shennanigans
if(Object.is(a, -0)) a = 0;
if(Object.is(b, -0)) b = 0;
// Otherwise, compare
return Object.is(a, b)
}function strictEquals(a, b) {
// If both are NaN we always return false
if (Number.isNaN(a) && Number.isNaN(b)) return false;
// If the result of Object.is (a,b) is false we should confirm the -0,0 pair
return (
Object.is(a, b) ||
(Object.is(a, -0) && Object.is(b, 0)) ||
(Object.is(a, 0) && Object.is(b, -0))
);
}function strictEquals(a, b) {
// Exception 1: NaN === NaN = false
if (Number.isNaN(a) && Number.isNaN(b)) {
return false;
}
// Exception 2: 0 === -0 OR -0 === 0 -> true
if ((Number.isInteger(a) && a > -1 && a < 1) && (Number.isInteger(b) && b > -1 && b < 1)) {
return true;
}
return Object.is(a, b);
}const strictEquals = (a, b) => {
//special case #1 are a and b both NaN
if (Object.is(a, NaN) && Object.is(b, NaN)) {
return false;
}
//special case #2 if a and b are any combination of 0 and -0
else if ([-0, 0].includes(a) && [-0, 0].includes(b)) {
return true;
} else {
return Object.is(a, b);
}
};Any thoughts on this?
function strictEquals(a, b) {
return (a == 0 && b == 0) || Object.is(a, b) && !Number.isNaN(a)
}Edit: ah, nvm. it will fail whenever 0 gets loosely compared against values such as "" or any other similar values stated here.
strictEquals(a, b) {
if(Number.isNaN(a) && Number.isNaN(b)) {
return false;
}
if(!Number.isFinite(1/Math.abs(a)) && !Number.isFinite(1/Math.abs(b))) {
return true
}
return Object.is(a, b);
}
function strictEquals(a,b){
if(Object.is(typeof a,"number")&&Object.is(typeof b,"number")){
if(Object.is(a,NaN)&&Object.is(b,NaN) )return false
if(Object.is(a/b,NaN))return true
}
return Object.is(a,b)
}
Your implementation incorrectly returns
falseforstrictEquals(-0, 0)