Skip to content

Commit

Permalink
feat(eq): Add eq (#642)
Browse files Browse the repository at this point in the history
Co-authored-by: Sojin Park <[email protected]>
  • Loading branch information
dayongkr and raon0211 authored Oct 3, 2024
1 parent e8826c5 commit 0e7ad28
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
32 changes: 32 additions & 0 deletions benchmarks/performance/eq.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { bench, describe } from 'vitest';
import { eq as eqToolkitCompat_ } from 'es-toolkit/compat';
import { eq as eqLodash_ } from 'lodash';

const eqToolkitCompat = eqToolkitCompat_;
const eqLodash = eqLodash_;

describe('eq', () => {
bench(
'es-toolkit/compat/eq',
() => {
eqToolkitCompat(NaN, NaN);
eqToolkitCompat(NaN, 0);
eqToolkitCompat(0, NaN);
eqToolkitCompat(0, -0);
eqToolkitCompat('abc', 'abc');
},
{ time: 100 } // We need to decrease the time. because vitest throws an error if the time is too long and tested function is too fast.
);

bench(
'lodash/eq',
() => {
eqLodash(NaN, NaN);
eqLodash(NaN, 0);
eqLodash(0, NaN);
eqLodash(0, -0);
eqLodash('abc', 'abc');
},
{ time: 100 }
);
});
1 change: 1 addition & 0 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,4 @@ export { toString } from './util/toString.ts';
export { toNumber } from './util/toNumber.ts';
export { toInteger } from './util/toInteger.ts';
export { toFinite } from './util/toFinite.ts';
export { eq } from './util/eq.ts';
21 changes: 21 additions & 0 deletions src/compat/util/eq.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest';
import { eq } from './eq';

describe('eq', () => {
it('should perform a `SameValueZero` comparison of two values', () => {
expect(eq()).toBe(true);
expect(eq(undefined)).toBe(true);
expect(eq(0, -0)).toBe(true);
expect(eq(NaN, NaN)).toBe(true);
expect(eq(1, 1)).toBe(true);

expect(eq(null, undefined)).toBe(false);
expect(eq(1, Object(1))).toBe(false);
expect(eq(1, '1')).toBe(false);
expect(eq(1, '1')).toBe(false);

const object = { a: 1 };
expect(eq(object, object)).toBe(true);
expect(eq(object, { a: 1 })).toBe(false);
});
});
16 changes: 16 additions & 0 deletions src/compat/util/eq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Performs a `SameValueZero` comparison between two values to determine if they are equivalent.
*
* @param {unknown} value - The value to compare.
* @param {unknown} other - The other value to compare.
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
*
* @example
* eq(1, 1); // true
* eq(0, -0); // true
* eq(NaN, NaN); // true
* eq('a', Object('a')); // false
*/
export function eq(value?: unknown, other?: unknown): boolean {
return value === other || (Number.isNaN(value) && Number.isNaN(other));
}

0 comments on commit 0e7ad28

Please sign in to comment.