Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions __tests__/eq.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { eq } from "@extremejs/utils";

it("should perform a SameValueZero comparison between two values to determine if they are equivalent.", () => {
const object = { a: 1 };
const other = { a: 1 };

expect(eq(object, object)).toBe(true);

expect(eq(object, other)).toBe(false);

expect(eq("a", "a")).toBe(true);

expect(eq("a", Object("a"))).toBe(false);

expect(eq(NaN, NaN)).toBe(true);
});
29 changes: 29 additions & 0 deletions src/eq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Performs a [SameValueZero](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
* comparison between two values to determine if they are equivalent.
* @since 1.0.0
* @param value
* @param other
* @example
* const object = { a: 1 };
* const other = { a: 1 };
*
* eq(object, object);
* // => true
*
* eq(object, other);
* // => false
* @example
* eq('a', 'a');
* // => true
* @example
* eq('a', Object('a'));
* // => false
* @example
* eq(NaN, NaN);
* // => true
*/
export default function eq(value: unknown, other: unknown): boolean {
// eslint-disable-next-line no-self-compare
return value === other || (value !== value && other !== other);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { default as chunk } from "./chunk.js";
export { default as compact, type CompactT } from "./compact.js";
export { default as concat } from "./concat.js";
export { default as endsWith } from "./ends-with.js";
export { default as eq } from "./eq.js";
export { default as first, type FirstT } from "./first.js";
export { default as get, type RecordT, type CalculatedPropertyT, type CalculatedPathT, type ValueAtT } from "./get.js";
export { default as head, type HeadT } from "./head.js";
Expand Down