Skip to content

Instantly share code, notes, and snippets.

@kishoreandra
Created July 31, 2022 12:08
Show Gist options
  • Save kishoreandra/5bb501790a23178f9e2ae61715da9363 to your computer and use it in GitHub Desktop.
Save kishoreandra/5bb501790a23178f9e2ae61715da9363 to your computer and use it in GitHub Desktop.
Object.is - Poly fill - YDKJS Kyle Simpson πŸ™‡β€β™‚οΈ
// TODO: define polyfill for `Object.is(..)`
// my solution πŸ˜›
if (!Object.is || true) {
Object.is = function ObjectIs(p1, p2) {
if (Number.isNaN(p1) && Number.isNaN(p2)) {
return true;
} else if (2 / p1 === -Infinity || 2 / p2 === -Infinity) {
if (2 / p1 === -Infinity && 2 / p2 === -Infinity) {
return true;
}
return false;
} else if (p1 === p2) {
return true;
}
return false;
};
}
// Kyle's solution 🀯
// if (!Object.is /*|| true*/) {
// Object.is = function ObjectIs(x,y) {
// var xNegZero = isItNegZero(x);
// var yNegZero = isItNegZero(y);
// if (xNegZero || yNegZero) {
// return xNegZero && yNegZero;
// }
// else if (isItNaN(x) && isItNaN(y)) {
// return true;
// }
// else if (x === y) {
// return true;
// }
// return false;
// // **********
// function isItNegZero(x) {
// return x === 0 && (1 / x) === -Infinity;
// }
// function isItNaN(x) {
// return x !== x;
// }
// };
// }
// tests:
console.log(Object.is(42, 42) === true);
console.log(Object.is("foo", "foo") === true);
console.log(Object.is(false, false) === true);
console.log(Object.is(null, null) === true);
console.log(Object.is(undefined, undefined) === true);
console.log(Object.is(NaN, NaN) === true);
console.log(Object.is(-0, -0) === true);
console.log(Object.is(0, 0) === true);
console.log(Object.is(-0, 0) === false);
console.log(Object.is(0, -0) === false);
console.log(Object.is(0, NaN) === false);
console.log(Object.is(NaN, 0) === false);
console.log(Object.is(42, "42") === false);
console.log(Object.is("42", 42) === false);
console.log(Object.is("foo", "bar") === false);
console.log(Object.is(false, true) === false);
console.log(Object.is(null, undefined) === false);
console.log(Object.is(undefined, null) === false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment