import { expect, test } from "vitest" import { asIs, asNull, asUndefined, asVoid, cases, guards, ifElse, iif, isLooseEqual, isObjectEqual, isStrictEqual, tryCatch, unless, when, } from "#Source/basic/index.ts" test("asIs returns the input unchanged", () => { expect(asIs(5)).toBe(5) expect(asIs("hello", 1, 2, 3)).toBe("hello") }) test("asUndefined always returns undefined", () => { expect(asUndefined()).toBeUndefined() expect(asUndefined(1, "hello", null)).toBeUndefined() }) test("asNull always returns null", () => { expect(asNull()).toBeNull() expect(asNull(1, "hello", undefined)).toBeNull() }) test("asVoid always returns undefined", () => { expect(asVoid()).toBeUndefined() expect(asVoid(1, "hello", null)).toBeUndefined() }) test("isStrictEqual compares with ===", () => { expect(isStrictEqual(1, 1)).toBe(true) expect(isStrictEqual(1, "1")).toBe(false) }) test("isLooseEqual compares with ==", () => { expect(isLooseEqual(1, "1")).toBe(true) expect(isLooseEqual(0, "1")).toBe(false) }) test("isObjectEqual compares with Object.is", () => { expect(isObjectEqual(Number.NaN, Number.NaN)).toBe(true) expect(isObjectEqual(0, -0)).toBe(false) }) test("ifElse chooses branch based on predicate", () => { const yes = (): string => "yes" const no = (): string => "no" expect(ifElse(true, yes, no)).toBe("yes") expect(ifElse(false, yes, no)).toBe("no") expect(ifElse((value?: number) => (value ?? 0) > 3, yes, no, 4)).toBe("yes") }) test("when invokes whenFn only for truthy predicate", () => { const plusOne = (value?: number): number => (value ?? 0) + 1 expect(when(true, plusOne, 3)).toBe(4) expect(when(false, plusOne, 3)).toBe(3) expect(when((value?: number) => (value ?? 0) > 1, plusOne, 2)).toBe(3) }) test("unless invokes unlessFn only for falsy predicate", () => { const plusOne = (value?: number): number => (value ?? 0) + 1 expect(unless(false, plusOne, 1)).toBe(2) expect(unless(true, plusOne, 1)).toBe(1) expect(unless((value?: number) => (value ?? 0) > 1, plusOne, 0)).toBe(1) }) test("iif returns immediate values based on condition", () => { expect(iif(true, "yes", "no")).toBe("yes") expect(iif(false, "yes", "no")).toBe("no") expect(iif((value?: string) => value === "ok", "ok", "no")).toBe("ok") }) test("guards returns the first matching guard", () => { expect( guards( [ [false, "a"], [true, "b"], ], "z", ), ).toBe("b") expect(guards([[false, "a"]], "z")).toBe("z") }) test("cases returns the first predicate match", () => { expect( cases( 3, [ [(value?: number): boolean => (value ?? 0) > 5, "big"], [(value?: number): boolean => (value ?? 0) > 1, "mid"], ], "small", ), ).toBe("mid") expect(cases(0, [[(value?: number): boolean => (value ?? 0) > 1, "mid"]], "small")).toBe("small") }) test("tryCatch returns catch value or undefined", () => { expect( tryCatch( () => "ok", () => "error", ), ).toBe("ok") expect( tryCatch( () => { throw new Error("fail") }, () => "error", ), ).toBe("error") expect( tryCatch(() => { throw new Error("fail") }, 0), ).toBe(0) expect( tryCatch(() => { throw new Error("fail") }), ).toBeUndefined() })