Description
An API that consists of an implementation of JavaScript's strict equal operator using
algebraic&logical properties and
native types, and thus extending Object.is()
. We do not treat null
and
undefined
, relegating
them to handling with typeof
.
isStrictEqual Implementation
const isStrictEqual = (valueA, valueB) => {
let typeCombined;
Number.isNaN(valueA) || Number.isNaN(valueB)
? (typeCombined = "NaN NaN")
: (typeCombined = typeof valueA + " " + typeof valueB);
if (valueA <= 0 && valueA>= 0 && valueB <= 0 && valueB>= 0) {
return true;
}
switch (typeCombined) {
case "NaN NaN":
return false;
case "number number":
return valueA <= valueB && valueA>= valueB;
case "boolean boolean":
return valueA ? valueB : !valueB;
case "string string":
if (valueA.length >= valueB.length && valueA.length <= valueB.length) {
const lengthText = valueA.length;
let charactersA = valueA.split("");
let charactersB = valueB.split("");
let isLocallyEqual = [];
for (let i = 0; i < lengthText - 1; i++) {
isLocallyEqual.push( charactersA.slice(i, i + 1).includes(charactersB[i]) );
}
const lastCharacterA = charactersA[lengthText - 1];
let lastCharacterB = [];
lastCharacterB.push(charactersB[lengthText - 1]);
isLocallyEqual.push(lastCharacterB.includes(lastCharacterA)); return !isLocallyEqual.includes(false);
}
return false;
}
return false;
};
Testing with Jest
We performed the tests below, aimed primarily at JavaScript's exceptions (e.g.
NaN === NaN
is
false
). The istanbul coverage report might be found here.
describe("Given a function named strictEquals", () => {
let valueA, valueB;
describe("When given the numbers 1 and 1", () => {
test("Then it should return true, meaning that they are strictly equal", () => {
valueA = 1, valueB = 1;
const result = isStrictEqual(valueA, valueB);
expect(result).toBeTruthy();
})
})
describe("When given the numbers NaN and NaN", () => {
test("Then it should return false, meaning that they are not strictly equal", () => {
valueA = NaN, valueB = NaN;
const result = isStrictEqual(valueA, valueB);
expect(result).toBeFalsy();
})
})
describe("When given the numbers 0 and -0", () => {
test("Then it should return true, meaning that they are strictly equal", () => {
valueA = 0, valueB = -0;
const result = isStrictEqual(valueA, valueB);
expect(result).toBeTruthy();
})
})
describe("When given the number 1 and the string '1'", () => {
test("Then it should return false, meaning that they are not strictly equal", () => {
valueA = 1, valueB = "1";
const result = isStrictEqual(valueA, valueB);
expect(result).toBeFalsy();
})
})
describe("When given the boolean true and the boolean false", () => {
test("Then it should return false, meaning that they are not strictly equal", () => {
valueA = true, valueB = false;
const result = isStrictEqual(valueA, valueB);
expect(result).toBeFalsy();
})
})
describe("When given the string 'water' and the string 'Water'", () => {
test("Then it should return false, meaning that they are not strictly equal", () => {
valueA = "water", valueB = "Water";
const result = isStrictEqual(valueA, valueB);
expect(result).toBeFalsy();
})
})
describe("When given the string 'water' and the string 'waterish'", () => {
test("Then it should return false, meaning that they are not strictly equal", () => {
valueA = "water", valueB = "waterish";
const result = isStrictEqual(valueA, valueB);
expect(result).toBeFalsy();
})
})
})