Skip to content

Instantly share code, notes, and snippets.

@aquaductape
Last active January 16, 2023 01:33
Show Gist options
  • Save aquaductape/7394f7ffa508517946986d77b5320731 to your computer and use it in GitHub Desktop.
Save aquaductape/7394f7ffa508517946986d77b5320731 to your computer and use it in GitHub Desktop.
exercise if two are values are strictly equal without using Equality operators: `==`, `===`, `!=`, `!==`, or `Object.is()`
// exercise if two are values are strictly equal
// without using Equality operators: ==, ===, !=, !==
// or Object.is()
const isObject = (val) => {
if (!val) return false;
return (typeof val).match(/object|function/);
};
const strictEquals = (a, b) => {
// a and b are both objects
if (isObject(a) && isObject(b)) {
let aLength = Object.keys(a).length;
let bLength = Object.keys(b).length;
// amount of keys don't match
if (aLength - bLength) {
return false;
}
let isEmpty = true;
let tempKey;
let tempVal;
// remove one key in a,
// which will reflect in b
// if it points to same object as a
for (const key in a) {
tempKey = key;
tempVal = a[key];
delete a[key];
isEmpty = false;
break;
}
// checking if a and b were already empty to begin with
if (!isEmpty) {
// check amount of keys for both again
aLength = Object.keys(a).length;
bLength = Object.keys(b).length;
// if amount of keys match, they share the same object
if (!(aLength - bLength)) {
a[tempKey] = tempVal;
return true;
}
a[tempKey] = tempVal;
return false;
} else {
// it's safe to add a key since both
// a and b are empty
a.prod = true;
if (b.prod) {
// restore input object, avoid side effects
delete a.prod;
return true;
}
// restore input object, avoid side effects
delete a.prod;
return false;
}
}
// if a and b are primitives or one of them are objects
const map = {};
map[a] = true;
map[b] = true;
// keys are converted to strings
// this is problem where 0 and "0" will both result in "0"
// storing the type as key as well is an additional measure
map[typeof a] = true;
map[typeof b] = true;
// if a and b are the same, the map will only have two keys
const result = !(Object.keys(map).length - 2);
if (result) {
if (map["NaN"]) return false;
}
return result;
};
/***** Tests ****/
const objA = { docker: "πŸ‹" };
const objB = { docker: "πŸ‹" };
const objC = { key: "🐠", docker: "πŸ‹" };
const objD = {};
const arrA = ["🌊", "πŸ‘‹"];
const arrB = ["🌊", "πŸ‘‹"];
const arrC = ["🌊", "πŸ‘‹", "wut"];
const arrD = [];
const funcA = () => {
return 42;
};
const funcB = () => {
return 42;
};
console.log(NaN === NaN, strictEquals(NaN, NaN));
console.log(NaN === 4, strictEquals(NaN, 4));
console.log(0 === -0, strictEquals(0, -0));
console.log(0 === "0", strictEquals(0, "0"));
console.log(3 === 3, strictEquals(3, 3));
console.log("0" === "-0", strictEquals("0", "-0"));
console.log(-0 === +0, strictEquals(-0, +0));
console.log(0 === 0, strictEquals(0, 0));
console.log("" === 0, strictEquals("", 0));
console.log("" === 1, strictEquals("", 1));
console.log("" === "", strictEquals("", ""));
console.log(1 === 1, strictEquals(1, 1));
console.log(1 === -1, strictEquals(1, -1));
console.log(undefined === null, strictEquals(undefined, null));
console.log(null === null, strictEquals(null, null));
console.log(Infinity === Infinity, strictEquals(Infinity, Infinity));
console.log(Infinity === -Infinity, strictEquals(Infinity, -Infinity));
console.log("hi" === "hi", strictEquals("hi", "hi"));
console.log("hi" === "bye", strictEquals("hi", "bye"));
console.log(true === true, strictEquals(true, true));
console.log(true === false, strictEquals(true, false));
console.log(false === false, strictEquals(false, false));
console.log(funcA === funcA, strictEquals(funcA, funcA));
console.log(funcA === funcB, strictEquals(funcA, funcB));
console.log(objA === objA, strictEquals(objA, objA));
console.log(objA === objB, strictEquals(objA, objB));
console.log(objA === objC, strictEquals(objA, objC));
console.log(objD === objD, strictEquals(objD, objD));
console.log({} === {}, strictEquals({}, {}));
console.log(arrA === arrA, strictEquals(arrA, arrA));
console.log(arrA === arrB, strictEquals(arrA, arrB));
console.log(arrA === arrC, strictEquals(arrA, arrC));
console.log(arrD === arrD, strictEquals(arrD, arrD));
console.log([] === [], strictEquals([], []));
console.log({} === [], strictEquals({}, []));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment