fast-is-equal
Version:
Blazing-fast equality checks, minus the baggage. A lean, standalone alternative to Lodash's isEqual—because speed matters.
181 lines (180 loc) • 7.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../index");
describe('isEqual', () => {
// **Primitives**
it('should return true for identical primitives', () => {
expect((0, index_1.fastIsEqual)(1, 1)).toBe(true);
expect((0, index_1.fastIsEqual)('a', 'a')).toBe(true);
expect((0, index_1.fastIsEqual)(true, true)).toBe(true);
});
it('should return false for different primitives', () => {
expect((0, index_1.fastIsEqual)(1, 2)).toBe(false);
expect((0, index_1.fastIsEqual)('a', 'b')).toBe(false);
expect((0, index_1.fastIsEqual)(true, false)).toBe(false);
});
// **NaN**
it('should return true for NaN and NaN', () => {
expect((0, index_1.fastIsEqual)(NaN, NaN)).toBe(true);
});
// **Null and Undefined**
it('should return true for null and null', () => {
expect((0, index_1.fastIsEqual)(null, null)).toBe(true);
});
it('should return true for undefined and undefined', () => {
expect((0, index_1.fastIsEqual)(undefined, undefined)).toBe(true);
});
it('should return false for null and undefined', () => {
expect((0, index_1.fastIsEqual)(null, undefined)).toBe(false);
});
// **Objects**
it('should return true for identical objects', () => {
const obj = { a: 1, b: { c: 2 } };
expect((0, index_1.fastIsEqual)(obj, obj)).toBe(true);
});
it('should return true for deeply equal objects', () => {
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
expect((0, index_1.fastIsEqual)(obj1, obj2)).toBe(true);
});
it('should return false for objects with different keys', () => {
const obj1 = { a: 1 };
const obj2 = { b: 1 };
expect((0, index_1.fastIsEqual)(obj1, obj2)).toBe(false);
});
it('should return false for objects with different values', () => {
const obj1 = { a: 1 };
const obj2 = { a: 2 };
expect((0, index_1.fastIsEqual)(obj1, obj2)).toBe(false);
});
// **Arrays**
it('should return true for identical arrays', () => {
const arr = [1, 2, 3];
expect((0, index_1.fastIsEqual)(arr, arr)).toBe(true);
});
it('should return true for deeply equal arrays', () => {
const arr1 = [1, [2, 3]];
const arr2 = [1, [2, 3]];
expect((0, index_1.fastIsEqual)(arr1, arr2)).toBe(true);
});
it('should return false for arrays with different lengths', () => {
const arr1 = [1, 2];
const arr2 = [1, 2, 3];
expect((0, index_1.fastIsEqual)(arr1, arr2)).toBe(false);
});
it('should return false for arrays with different elements', () => {
const arr1 = [1, 2];
const arr2 = [1, 3];
expect((0, index_1.fastIsEqual)(arr1, arr2)).toBe(false);
});
// **Dates**
it('should return true for identical dates', () => {
const date = new Date();
expect((0, index_1.fastIsEqual)(date, date)).toBe(true);
});
it('should return true for dates with the same timestamp', () => {
const date1 = new Date('2023-01-01');
const date2 = new Date('2023-01-01');
expect((0, index_1.fastIsEqual)(date1, date2)).toBe(true);
});
it('should return false for dates with different timestamps', () => {
const date1 = new Date('2023-01-01');
const date2 = new Date('2023-01-02');
expect((0, index_1.fastIsEqual)(date1, date2)).toBe(false);
});
// **Regular Expressions**
it('should return true for identical regexes', () => {
const regex = /a/g;
expect((0, index_1.fastIsEqual)(regex, regex)).toBe(true);
});
it('should return true for regexes with the same pattern and flags', () => {
const regex1 = /a/g;
const regex2 = /a/g;
expect((0, index_1.fastIsEqual)(regex1, regex2)).toBe(true);
});
it('should return false for regexes with different patterns', () => {
const regex1 = /a/g;
const regex2 = /b/g;
expect((0, index_1.fastIsEqual)(regex1, regex2)).toBe(false);
});
it('should return false for regexes with different flags', () => {
const regex1 = /a/g;
const regex2 = /a/i;
expect((0, index_1.fastIsEqual)(regex1, regex2)).toBe(false);
});
// **Maps**
it('should return true for identical maps', () => {
const map = new Map([['a', 1]]);
expect((0, index_1.fastIsEqual)(map, map)).toBe(true);
});
it('should return true for maps with the same key-value pairs', () => {
const map1 = new Map([['a', 1]]);
const map2 = new Map([['a', 1]]);
expect((0, index_1.fastIsEqual)(map1, map2)).toBe(true);
});
it('should return false for maps with different key-value pairs', () => {
const map1 = new Map([['a', 1]]);
const map2 = new Map([['a', 2]]);
expect((0, index_1.fastIsEqual)(map1, map2)).toBe(false);
});
it('should return false for maps with different sizes', () => {
const map1 = new Map([['a', 1]]);
const map2 = new Map([['a', 1], ['b', 2]]);
expect((0, index_1.fastIsEqual)(map1, map2)).toBe(false);
});
// **Sets**
it('should return true for identical sets', () => {
const set = new Set([1, 2]);
expect((0, index_1.fastIsEqual)(set, set)).toBe(true);
});
it('should return true for sets with the same elements', () => {
const set1 = new Set([1, 2]);
const set2 = new Set([1, 2]);
expect((0, index_1.fastIsEqual)(set1, set2)).toBe(true);
});
it('should return false for sets with different elements', () => {
const set1 = new Set([1, 2]);
const set2 = new Set([1, 3]);
expect((0, index_1.fastIsEqual)(set1, set2)).toBe(false);
});
it('should return false for sets with different sizes', () => {
const set1 = new Set([1, 2]);
const set2 = new Set([1]);
expect((0, index_1.fastIsEqual)(set1, set2)).toBe(false);
});
// **Circular References**
it('should return true for circular references', () => {
const obj1 = {};
obj1.self = obj1;
const obj2 = {};
obj2.self = obj2;
expect((0, index_1.fastIsEqual)(obj1, obj2)).toBe(true);
});
it('should return false for different circular references', () => {
const obj1 = {};
obj1.self = obj1;
const obj2 = { self: {} };
expect((0, index_1.fastIsEqual)(obj1, obj2)).toBe(false);
});
// **Other Types (Functions, Promises)**
it('should return true for the same function reference', () => {
const func = () => { };
expect((0, index_1.fastIsEqual)(func, func)).toBe(true);
});
it('should return false for different functions', () => {
const func1 = () => { };
const func2 = () => { };
expect((0, index_1.fastIsEqual)(func1, func2)).toBe(false);
});
it('should return false for different promises', () => {
const p1 = Promise.resolve(1);
const p2 = Promise.resolve(1);
expect((0, index_1.fastIsEqual)(p1, p2)).toBe(false);
});
// **Mixed Types**
it('should return false for different types', () => {
expect((0, index_1.fastIsEqual)(1, '1')).toBe(false);
expect((0, index_1.fastIsEqual)({}, [])).toBe(false);
expect((0, index_1.fastIsEqual)(new Map(), new Set())).toBe(false);
});
});