@wopjs/cast
Advanced tools
| import { describe, it, expect } from "vitest"; | ||
| import { inertFilter, inertFilterMap, coalesce } from "./array"; | ||
| import { isNumber, isString } from "./is-to-as"; | ||
| /** Bypass TypeScript static type inference. */ | ||
| const castType = <T>(x: T): T => x; | ||
| describe("array.ts", () => { | ||
| describe("inertFilterMap", () => { | ||
| it("returns undefined for undefined input", () => { | ||
| const result = inertFilterMap(undefined, x => x); | ||
| expect(result).toBe(undefined); | ||
| }); | ||
| it("returns original array for empty array", () => { | ||
| const arr: number[] = []; | ||
| const result = inertFilterMap(arr, x => x); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("returns original array when all items map to themselves", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilterMap(arr, x => x); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("returns original array when callback returns same reference for objects", () => { | ||
| const obj1 = { a: 1 }; | ||
| const obj2 = { b: 2 }; | ||
| const arr = [obj1, obj2]; | ||
| const result = inertFilterMap(arr, x => x); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("returns new array when at least one item is mapped to different value", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilterMap(arr, x => x * 2); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([2, 4, 6]); | ||
| }); | ||
| it("returns new array when at least one item is filtered out", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilterMap(arr, x => (x === 2 ? undefined : x)); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("filters out all items when callback always returns undefined", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilterMap(arr, () => undefined); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([]); | ||
| }); | ||
| it("maps all items correctly", () => { | ||
| const arr = ["a", "b", "c"]; | ||
| const result = inertFilterMap(arr, x => x.toUpperCase()); | ||
| expect(result).toEqual(["A", "B", "C"]); | ||
| }); | ||
| it("filters and maps simultaneously", () => { | ||
| const arr = [1, 2, 3, 4, 5]; | ||
| const result = inertFilterMap(arr, x => (x % 2 === 0 ? x * 10 : undefined)); | ||
| expect(result).toEqual([20, 40]); | ||
| }); | ||
| it("handles mixed filter and identity mapping", () => { | ||
| const arr = [1, 2, 3, 4, 5]; | ||
| // Filter out even numbers, keep odd numbers as-is | ||
| const result = inertFilterMap(arr, x => (x % 2 === 0 ? undefined : x)); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3, 5]); | ||
| }); | ||
| it("uses Object.is for equality comparison", () => { | ||
| // NaN is equal to NaN with Object.is | ||
| const arr = [NaN, NaN]; | ||
| const result = inertFilterMap(arr, x => x); | ||
| expect(result).toBe(arr); | ||
| // +0 and -0 are different with Object.is | ||
| const arr2 = [0]; | ||
| const result2 = inertFilterMap(arr2, x => (x === 0 ? -0 : x)); | ||
| expect(result2).not.toBe(arr2); | ||
| expect(Object.is(result2[0], -0)).toBe(true); | ||
| }); | ||
| it("provides correct index to callback", () => { | ||
| const arr = ["a", "b", "c"]; | ||
| const indices: number[] = []; | ||
| inertFilterMap(arr, (_, index) => { | ||
| indices.push(index); | ||
| return undefined; | ||
| }); | ||
| expect(indices).toEqual([0, 1, 2]); | ||
| }); | ||
| it("provides array reference to callback", () => { | ||
| const arr = [1, 2, 3]; | ||
| let receivedArr: number[] | undefined; | ||
| inertFilterMap(arr, (_, __, a) => { | ||
| receivedArr = a; | ||
| return undefined; | ||
| }); | ||
| expect(receivedArr).toBe(arr); | ||
| }); | ||
| it("respects thisArg", () => { | ||
| const arr = [1, 2, 3]; | ||
| const context = { multiplier: 10 }; | ||
| const result = inertFilterMap( | ||
| arr, | ||
| function (this: typeof context, x) { | ||
| return x * this.multiplier; | ||
| }, | ||
| context | ||
| ); | ||
| expect(result).toEqual([10, 20, 30]); | ||
| }); | ||
| it("handles single element array - identity", () => { | ||
| const arr = [42]; | ||
| const result = inertFilterMap(arr, x => x); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("handles single element array - mapped", () => { | ||
| const arr = [42]; | ||
| const result = inertFilterMap(arr, x => x * 2); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([84]); | ||
| }); | ||
| it("handles single element array - filtered", () => { | ||
| const arr = [42]; | ||
| const result = inertFilterMap(arr, () => undefined); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([]); | ||
| }); | ||
| it("returns new array when first item changes", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilterMap(arr, (x, i) => (i === 0 ? x * 2 : x)); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([2, 2, 3]); | ||
| }); | ||
| it("returns new array when last item changes", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilterMap(arr, (x, i) => (i === 2 ? x * 2 : x)); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 2, 6]); | ||
| }); | ||
| it("returns new array when middle item is filtered", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilterMap(arr, (x, i) => (i === 1 ? undefined : x)); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("handles readonly arrays", () => { | ||
| const arr: readonly number[] = [1, 2, 3]; | ||
| const result = inertFilterMap(arr, x => x); | ||
| expect(result).toBe(arr); | ||
| const result2 = inertFilterMap(arr, x => x * 2); | ||
| expect(result2).toEqual([2, 4, 6]); | ||
| }); | ||
| it("handles arrays with undefined values - identity", () => { | ||
| const arr = [1, undefined, 3]; | ||
| // When callback returns the original undefined, it gets filtered out | ||
| const result = inertFilterMap(arr, x => x); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("handles sparse arrays", () => { | ||
| const arr = [1, , 3]; // eslint-disable-line no-sparse-arrays | ||
| const result = inertFilterMap(arr, x => x); | ||
| expect(result).not.toBe(arr); | ||
| // Sparse slots become undefined, which gets filtered out | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| // Type narrowing tests | ||
| { | ||
| it("type narrowing - mutable array input returns mutable array", () => { | ||
| const arr = castType<number[]>([1, 2, 3]); | ||
| const result = inertFilterMap(arr, x => x.toString()); | ||
| const _check: string[] = result; | ||
| expect(_check).toEqual(["1", "2", "3"]); | ||
| }); | ||
| it("type narrowing - readonly array input returns readonly array", () => { | ||
| const arr = castType<readonly number[]>([1, 2, 3]); | ||
| const result = inertFilterMap(arr, x => x.toString()); | ||
| const _check: readonly string[] = result; | ||
| expect(_check).toEqual(["1", "2", "3"]); | ||
| }); | ||
| it("type narrowing - undefined input returns undefined", () => { | ||
| const arr = castType<number[] | undefined>(undefined); | ||
| const result = inertFilterMap(arr, x => x); | ||
| const _check: number[] | undefined = result; | ||
| expect(_check).toBe(undefined); | ||
| }); | ||
| it("type narrowing - excludes undefined from result type", () => { | ||
| const arr = castType<(number | undefined)[]>([1, undefined, 3]); | ||
| const result = inertFilterMap(arr, x => x); | ||
| // Result type should be number[] (Defined<number | undefined> = number) | ||
| const _check: number[] = result; | ||
| expect(_check).toEqual([1, 3]); | ||
| }); | ||
| } | ||
| }); | ||
| describe("inertFilter", () => { | ||
| it("returns undefined for undefined input", () => { | ||
| const result = inertFilter(undefined, () => true); | ||
| expect(result).toBe(undefined); | ||
| }); | ||
| it("returns original array for empty array", () => { | ||
| const arr: number[] = []; | ||
| const result = inertFilter(arr, () => true); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("returns original array when all items pass predicate", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilter(arr, () => true); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("returns original array when all items pass actual condition", () => { | ||
| const arr = [2, 4, 6]; | ||
| const result = inertFilter(arr, x => x % 2 === 0); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("returns new array when at least one item is filtered out", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilter(arr, x => x !== 2); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("returns empty array when all items are filtered out", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilter(arr, () => false); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([]); | ||
| }); | ||
| it("filters correctly based on predicate", () => { | ||
| const arr = [1, 2, 3, 4, 5, 6]; | ||
| const result = inertFilter(arr, x => x % 2 === 0); | ||
| expect(result).toEqual([2, 4, 6]); | ||
| }); | ||
| it("provides correct index to predicate", () => { | ||
| const arr = ["a", "b", "c"]; | ||
| const indices: number[] = []; | ||
| inertFilter(arr, (_, index) => { | ||
| indices.push(index); | ||
| return true; | ||
| }); | ||
| expect(indices).toEqual([0, 1, 2]); | ||
| }); | ||
| it("provides array reference to predicate", () => { | ||
| const arr = [1, 2, 3]; | ||
| let receivedArr: number[] | undefined; | ||
| inertFilter(arr, (_, __, a) => { | ||
| receivedArr = a; | ||
| return true; | ||
| }); | ||
| expect(receivedArr).toBe(arr); | ||
| }); | ||
| it("respects thisArg", () => { | ||
| const arr = [1, 2, 3, 4, 5]; | ||
| const context = { threshold: 3 }; | ||
| const result = inertFilter( | ||
| arr, | ||
| function (this: typeof context, x) { | ||
| return x > this.threshold; | ||
| }, | ||
| context | ||
| ); | ||
| expect(result).toEqual([4, 5]); | ||
| }); | ||
| it("handles single element array - kept", () => { | ||
| const arr = [42]; | ||
| const result = inertFilter(arr, () => true); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("handles single element array - filtered", () => { | ||
| const arr = [42]; | ||
| const result = inertFilter(arr, () => false); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([]); | ||
| }); | ||
| it("returns new array when first item is filtered", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilter(arr, (_, i) => i !== 0); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([2, 3]); | ||
| }); | ||
| it("returns new array when last item is filtered", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilter(arr, (_, i) => i !== 2); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 2]); | ||
| }); | ||
| it("returns new array when middle item is filtered", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = inertFilter(arr, (_, i) => i !== 1); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("handles readonly arrays", () => { | ||
| const arr: readonly number[] = [1, 2, 3]; | ||
| const result = inertFilter(arr, () => true); | ||
| expect(result).toBe(arr); | ||
| const result2 = inertFilter(arr, x => x !== 2); | ||
| expect(result2).toEqual([1, 3]); | ||
| }); | ||
| it("handles arrays with falsy values correctly", () => { | ||
| const arr = [0, 1, "", "hello", false, true, null, undefined]; | ||
| const result = inertFilter(arr, () => true); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("handles sparse arrays", () => { | ||
| const arr = [1, , 3]; // eslint-disable-line no-sparse-arrays | ||
| const result = inertFilter(arr, x => x !== undefined); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("filters multiple consecutive items", () => { | ||
| const arr = [1, 2, 3, 4, 5]; | ||
| const result = inertFilter(arr, x => x === 1 || x === 5); | ||
| expect(result).toEqual([1, 5]); | ||
| }); | ||
| it("filters all but one item", () => { | ||
| const arr = [1, 2, 3, 4, 5]; | ||
| const result = inertFilter(arr, x => x === 3); | ||
| expect(result).toEqual([3]); | ||
| }); | ||
| // Type narrowing tests with type guard predicate | ||
| { | ||
| it("type narrowing - narrows type with type guard predicate", () => { | ||
| const arr = castType<(string | number)[]>([1, "a", 2, "b"]); | ||
| const result = inertFilter(arr, isString); | ||
| const _check: string[] = result; | ||
| expect(_check).toEqual(["a", "b"]); | ||
| }); | ||
| it("type narrowing - narrows to number type", () => { | ||
| const arr = castType<(string | number)[]>([1, "a", 2, "b"]); | ||
| const result = inertFilter(arr, isNumber); | ||
| const _check: number[] = result; | ||
| expect(_check).toEqual([1, 2]); | ||
| }); | ||
| it("type narrowing - mutable array input returns mutable array", () => { | ||
| const arr = castType<number[]>([1, 2, 3]); | ||
| const result = inertFilter(arr, x => x > 1); | ||
| const _check: number[] = result; | ||
| expect(_check).toEqual([2, 3]); | ||
| }); | ||
| it("type narrowing - readonly array input returns readonly array", () => { | ||
| const arr = castType<readonly number[]>([1, 2, 3]); | ||
| const result = inertFilter(arr, x => x > 1); | ||
| const _check: readonly number[] = result; | ||
| expect(_check).toEqual([2, 3]); | ||
| }); | ||
| it("type narrowing - undefined input returns undefined", () => { | ||
| const arr = castType<number[] | undefined>(undefined); | ||
| const result = inertFilter(arr, x => x > 1); | ||
| const _check: number[] | undefined = result; | ||
| expect(_check).toBe(undefined); | ||
| }); | ||
| it("type narrowing - type guard with readonly array", () => { | ||
| const arr = castType<readonly (string | number)[]>([1, "a", 2, "b"]); | ||
| const result = inertFilter(arr, isString); | ||
| const _check: readonly string[] = result; | ||
| expect(_check).toEqual(["a", "b"]); | ||
| }); | ||
| } | ||
| }); | ||
| describe("coalesce", () => { | ||
| it("returns original array when all items are truthy", () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = coalesce(arr); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("returns original array for array of truthy strings", () => { | ||
| const arr = ["a", "b", "c"]; | ||
| const result = coalesce(arr); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("returns original array for array of truthy objects", () => { | ||
| const arr = [{}, [], () => {}]; | ||
| const result = coalesce(arr); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("filters out null values", () => { | ||
| const arr = [1, null, 3]; | ||
| const result = coalesce(arr); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("filters out undefined values", () => { | ||
| const arr = [1, undefined, 3]; | ||
| const result = coalesce(arr); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("filters out false", () => { | ||
| const arr = [true, false, true]; | ||
| const result = coalesce(arr); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([true, true]); | ||
| }); | ||
| it("filters out 0", () => { | ||
| const arr = [1, 0, 2]; | ||
| const result = coalesce(arr); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 2]); | ||
| }); | ||
| it("filters out empty string", () => { | ||
| const arr = ["a", "", "b"]; | ||
| const result = coalesce(arr); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual(["a", "b"]); | ||
| }); | ||
| it("filters out NaN", () => { | ||
| const arr = [1, NaN, 2]; | ||
| const result = coalesce(arr); | ||
| expect(result).not.toBe(arr); | ||
| expect(result).toEqual([1, 2]); | ||
| }); | ||
| it("filters out all falsy values", () => { | ||
| const arr = [1, null, undefined, false, 0, "", NaN, 2]; | ||
| const result = coalesce(arr); | ||
| expect(result).toEqual([1, 2]); | ||
| }); | ||
| it("returns empty array when all values are falsy", () => { | ||
| const arr = [null, undefined, false, 0, "", NaN]; | ||
| const result = coalesce(arr); | ||
| expect(result).toEqual([]); | ||
| }); | ||
| it("returns original empty array", () => { | ||
| const arr: unknown[] = []; | ||
| const result = coalesce(arr); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| it("handles readonly arrays", () => { | ||
| const arr: readonly (number | null)[] = [1, null, 3]; | ||
| const result = coalesce(arr); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
| it("preserves truthy falsy-looking values", () => { | ||
| // These are all truthy | ||
| const arr = ["0", "false", "null", "undefined", " ", []]; | ||
| const result = coalesce(arr); | ||
| expect(result).toBe(arr); | ||
| }); | ||
| // Type narrowing tests | ||
| { | ||
| it("type narrowing - removes null and undefined from type", () => { | ||
| const arr = castType<(string | null | undefined)[]>(["a", null, "b", undefined]); | ||
| const result = coalesce(arr); | ||
| // Result type should be string[] (Truthy removes null, undefined, etc.) | ||
| const _check: string[] = result as string[]; | ||
| expect(_check).toEqual(["a", "b"]); | ||
| }); | ||
| it("type narrowing - removes false from boolean union", () => { | ||
| const arr = castType<(string | boolean)[]>(["a", true, false, "b"]); | ||
| const result = coalesce(arr); | ||
| expect(result).toEqual(["a", true, "b"]); | ||
| }); | ||
| it("type narrowing - handles mixed types", () => { | ||
| const arr = castType<(number | string | null | undefined | false | 0 | "")[]>([ | ||
| 1, | ||
| "hello", | ||
| null, | ||
| undefined, | ||
| false, | ||
| 0, | ||
| "", | ||
| 2, | ||
| "world", | ||
| ]); | ||
| const result = coalesce(arr); | ||
| expect(result).toEqual([1, "hello", 2, "world"]); | ||
| }); | ||
| } | ||
| }); | ||
| }); |
+129
| import type { Defined, Truthy } from "./is-to-as"; | ||
| import { isTruthy } from "./is-to-as"; | ||
| /** | ||
| * Lazy filterMap that avoids creating a new array when possible. | ||
| * | ||
| * @returns the original array if `callbackfn` returns the same value for all items, otherwise a new array with the mapped items. | ||
| * | ||
| * @param arr | ||
| * @param callbackfn Return `undefined` to filter out the item, or return a value to map the item. | ||
| * @param thisArg | ||
| */ | ||
| export function inertFilterMap<T, U = T>( | ||
| arr: T[], | ||
| callbackfn: (value: T, index: number, arr: T[]) => U | undefined, | ||
| thisArg?: any | ||
| ): Defined<U>[]; | ||
| export function inertFilterMap<T, U = T>( | ||
| arr: T[] | undefined, | ||
| callbackfn: (value: T, index: number, arr: T[]) => U | undefined, | ||
| thisArg?: any | ||
| ): Defined<U>[] | undefined; | ||
| export function inertFilterMap<T, U = T>( | ||
| arr: readonly T[], | ||
| callbackfn: (value: T, index: number, arr: readonly T[]) => U | undefined, | ||
| thisArg?: any | ||
| ): readonly Defined<U>[]; | ||
| export function inertFilterMap<T, U = T>( | ||
| arr: readonly T[] | undefined, | ||
| callbackfn: (value: T, index: number, arr: readonly T[]) => U | undefined, | ||
| thisArg?: any | ||
| ): readonly Defined<U>[] | undefined; | ||
| export function inertFilterMap<T, U = T>( | ||
| arr: readonly T[] | undefined, | ||
| callbackfn: (value: T, index: number, arr: T[]) => U | undefined, | ||
| thisArg?: any | ||
| ): readonly Defined<U>[] | undefined { | ||
| if (arr) { | ||
| let result: Defined<U>[] | undefined; | ||
| let index = -1; | ||
| let length = arr.length; | ||
| while (++index < length) { | ||
| const newItem = callbackfn.call(thisArg, arr[index], index, arr as T[]); | ||
| if (newItem === undefined || !Object.is(newItem, arr[index])) { | ||
| result ??= arr.slice(0, index) as Defined<U>[]; | ||
| } | ||
| if (newItem !== undefined) { | ||
| result?.push(newItem as Defined<U>); | ||
| } | ||
| } | ||
| return result || (arr as readonly Defined<U>[]); | ||
| } | ||
| } | ||
| /** | ||
| * Lazy filter that avoids creating a new array when possible. | ||
| * | ||
| * @returns the original array if `predicate` returns `true` for all items, otherwise a new array with the filtered items. | ||
| * | ||
| * @param arr | ||
| * @param predicate Return `false` to filter out the item, or `true` to keep it. | ||
| * @param thisArg | ||
| */ | ||
| export function inertFilter<T, U extends T>( | ||
| arr: T[], | ||
| predicate: (value: T, index: number, arr: T[]) => value is U, | ||
| thisArg?: any | ||
| ): U[]; | ||
| export function inertFilter<T, U extends T>( | ||
| arr: T[] | undefined, | ||
| predicate: (value: T, index: number, arr: T[]) => value is U, | ||
| thisArg?: any | ||
| ): U[] | undefined; | ||
| export function inertFilter<T, U extends T>( | ||
| arr: readonly T[], | ||
| predicate: (value: T, index: number, arr: readonly T[]) => value is U, | ||
| thisArg?: any | ||
| ): readonly U[]; | ||
| export function inertFilter<T, U extends T>( | ||
| arr: readonly T[] | undefined, | ||
| predicate: (value: T, index: number, arr: readonly T[]) => value is U, | ||
| thisArg?: any | ||
| ): readonly U[] | undefined; | ||
| export function inertFilter<T>(arr: T[], predicate: (value: T, index: number, arr: T[]) => boolean, thisArg?: any): T[]; | ||
| export function inertFilter<T>( | ||
| arr: T[] | undefined, | ||
| predicate: (value: T, index: number, arr: T[]) => boolean, | ||
| thisArg?: any | ||
| ): T[] | undefined; | ||
| export function inertFilter<T>( | ||
| arr: readonly T[], | ||
| predicate: (value: T, index: number, arr: readonly T[]) => boolean, | ||
| thisArg?: any | ||
| ): readonly T[]; | ||
| export function inertFilter<T>( | ||
| arr: readonly T[] | undefined, | ||
| predicate: (value: T, index: number, arr: readonly T[]) => boolean, | ||
| thisArg?: any | ||
| ): readonly T[] | undefined; | ||
| export function inertFilter<T>( | ||
| arr: readonly T[] | undefined, | ||
| predicate: (value: T, index: number, arr: T[]) => boolean, | ||
| thisArg?: any | ||
| ): readonly T[] | undefined { | ||
| if (arr) { | ||
| let result: T[] | undefined; | ||
| let index = -1; | ||
| let length = arr.length; | ||
| while (++index < length) { | ||
| if (predicate.call(thisArg, arr[index], index, arr as T[])) { | ||
| result?.push(arr[index]); | ||
| } else { | ||
| result ??= arr.slice(0, index); | ||
| } | ||
| } | ||
| return result || arr; | ||
| } | ||
| } | ||
| export interface Coalesce { | ||
| <T>(arr: readonly T[]): readonly Truthy<T>[]; | ||
| <T>(arr: T[]): Truthy<T>[]; | ||
| } | ||
| /** | ||
| * @returns the same array if all its items are truthy, otherwise a new array with all falsy items removed. | ||
| */ | ||
| export const coalesce: Coalesce = arr => inertFilter(arr, isTruthy) as unknown[]; |
+59
-19
@@ -1,9 +0,9 @@ | ||
| type _ = undefined; | ||
| /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */ | ||
| type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1]; | ||
| type Defined<T> = Exclude<T, undefined>; | ||
| /** Returns `true` if `x` is not `undefined`. */ | ||
| declare const isDefined: <T>(x: T | _) => x is Exclude<T, _>; | ||
| declare const isDefined: <T>(x: T | undefined) => x is Defined<T>; | ||
| declare const isTrue: (x: unknown) => x is true; | ||
| /** Returns `true` if `x` is `true`, otherwise returns `undefined`. */ | ||
| declare const toTrue: (x: unknown) => true | _; | ||
| declare const toTrue: (x: unknown) => true | undefined; | ||
| /** Returns `true` if `x` is `true`, otherwise returns `false`. */ | ||
@@ -16,14 +16,15 @@ declare const asTrue: (x: unknown) => boolean; | ||
| /** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */ | ||
| declare const toFalsy: <T>(x: T) => ExtractFalsy<T> | _; | ||
| declare const toFalsy: <T>(x: T) => ExtractFalsy<T> | undefined; | ||
| type Truthy<T> = Exclude<T, Falsy>; | ||
| /** Returns `true` if `Boolean(x)` is `true`. */ | ||
| declare const isTruthy: <T>(x: T) => x is Exclude<T, Falsy>; | ||
| declare const isTruthy: <T>(x: T) => x is Truthy<T>; | ||
| /** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */ | ||
| declare const toTruthy: <T>(x: T) => Exclude<T, Falsy> | _; | ||
| declare const toTruthy: <T>(x: T) => Truthy<T> | undefined; | ||
| declare const isBoolean: (x: unknown) => x is boolean; | ||
| /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */ | ||
| declare const toBoolean: (x: unknown) => boolean | _; | ||
| declare const toBoolean: (x: unknown) => boolean | undefined; | ||
| /** Returns `true` if `x` is a number, returns `false` if `x` is `NaN` or other value. */ | ||
| declare const isNumber: (x: unknown) => x is number; | ||
| /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `undefined`. */ | ||
| declare const toNumber: (x: unknown) => number | _; | ||
| declare const toNumber: (x: unknown) => number | undefined; | ||
| /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `0`. */ | ||
@@ -34,3 +35,3 @@ declare const asNumber: (x: unknown) => number; | ||
| /** Returns `x` if `x` is a string, otherwise returns `undefined`. */ | ||
| declare const toString: (x: unknown) => string | _; | ||
| declare const toString: (x: unknown) => string | undefined; | ||
| /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */ | ||
@@ -41,3 +42,3 @@ declare const asString: (x: unknown) => string; | ||
| /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyString: (x: unknown) => string | _; | ||
| declare const toNonEmptyString: (x: unknown) => string | undefined; | ||
| /** Extracts array type from `T`, or `unknown[]` if no array type found. */ | ||
@@ -47,3 +48,3 @@ type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>; | ||
| /** Returns `x` if `x` is an array. */ | ||
| declare const toArray: <T>(x: T) => ExtractArray<T> | _; | ||
| declare const toArray: <T>(x: T) => ExtractArray<T> | undefined; | ||
| /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
@@ -54,3 +55,3 @@ declare const asArray: <T>(x: T) => ExtractArray<T>; | ||
| /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _; | ||
| declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | undefined; | ||
| type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>; | ||
@@ -60,3 +61,3 @@ /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| /** Returns `x` if `x` is an object (including array). */ | ||
| declare const toObject: <T>(x: T) => ExtractObject<T> | _; | ||
| declare const toObject: <T>(x: T) => ExtractObject<T> | undefined; | ||
| /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
@@ -71,3 +72,3 @@ declare const asObject: <T>(x: T) => ExtractObject<T>; | ||
| /** Returns `x` if `x` is a plain object. */ | ||
| declare const toPlainObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| declare const toPlainObject: <T>(x: T) => ExtractPlainObject<T> | undefined; | ||
| /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */ | ||
@@ -78,7 +79,7 @@ declare const asPlainObject: <T>(x: T) => ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object and has at least one key. */ | ||
| declare const toNonEmptyPlainObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| declare const toNonEmptyPlainObject: <T>(x: T) => ExtractPlainObject<T> | undefined; | ||
| /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */ | ||
| declare const isNonEmptyJSONObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyJSONObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| declare const toNonEmptyJSONObject: <T>(x: T) => ExtractPlainObject<T> | undefined; | ||
| type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject<T>]; | ||
@@ -89,5 +90,5 @@ /** | ||
| */ | ||
| declare const toPlainObjectOf: <T, U extends ExtractPlainObjectValue<T>>(x: T, f: (v: ExtractPlainObjectValue<T>) => v is U) => Record<PropertyKey, U> | _; | ||
| declare const toPlainObjectOf: <T, U extends ExtractPlainObjectValue<T>>(x: T, f: (v: ExtractPlainObjectValue<T>) => v is U) => Record<PropertyKey, U> | undefined; | ||
| /** Filter props from object `x` whose values are `true`. */ | ||
| declare const toPlainObjectOfTrue: (x: unknown) => Record<PropertyKey, true> | _; | ||
| declare const toPlainObjectOfTrue: (x: unknown) => Record<PropertyKey, true> | undefined; | ||
| /** | ||
@@ -107,2 +108,41 @@ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if | ||
| export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| /** | ||
| * Lazy filterMap that avoids creating a new array when possible. | ||
| * | ||
| * @returns the original array if `callbackfn` returns the same value for all items, otherwise a new array with the mapped items. | ||
| * | ||
| * @param arr | ||
| * @param callbackfn Return `undefined` to filter out the item, or return a value to map the item. | ||
| * @param thisArg | ||
| */ | ||
| declare function inertFilterMap<T, U = T>(arr: T[], callbackfn: (value: T, index: number, arr: T[]) => U | undefined, thisArg?: any): Defined<U>[]; | ||
| declare function inertFilterMap<T, U = T>(arr: T[] | undefined, callbackfn: (value: T, index: number, arr: T[]) => U | undefined, thisArg?: any): Defined<U>[] | undefined; | ||
| declare function inertFilterMap<T, U = T>(arr: readonly T[], callbackfn: (value: T, index: number, arr: readonly T[]) => U | undefined, thisArg?: any): readonly Defined<U>[]; | ||
| declare function inertFilterMap<T, U = T>(arr: readonly T[] | undefined, callbackfn: (value: T, index: number, arr: readonly T[]) => U | undefined, thisArg?: any): readonly Defined<U>[] | undefined; | ||
| /** | ||
| * Lazy filter that avoids creating a new array when possible. | ||
| * | ||
| * @returns the original array if `predicate` returns `true` for all items, otherwise a new array with the filtered items. | ||
| * | ||
| * @param arr | ||
| * @param predicate Return `false` to filter out the item, or `true` to keep it. | ||
| * @param thisArg | ||
| */ | ||
| declare function inertFilter<T, U extends T>(arr: T[], predicate: (value: T, index: number, arr: T[]) => value is U, thisArg?: any): U[]; | ||
| declare function inertFilter<T, U extends T>(arr: T[] | undefined, predicate: (value: T, index: number, arr: T[]) => value is U, thisArg?: any): U[] | undefined; | ||
| declare function inertFilter<T, U extends T>(arr: readonly T[], predicate: (value: T, index: number, arr: readonly T[]) => value is U, thisArg?: any): readonly U[]; | ||
| declare function inertFilter<T, U extends T>(arr: readonly T[] | undefined, predicate: (value: T, index: number, arr: readonly T[]) => value is U, thisArg?: any): readonly U[] | undefined; | ||
| declare function inertFilter<T>(arr: T[], predicate: (value: T, index: number, arr: T[]) => boolean, thisArg?: any): T[]; | ||
| declare function inertFilter<T>(arr: T[] | undefined, predicate: (value: T, index: number, arr: T[]) => boolean, thisArg?: any): T[] | undefined; | ||
| declare function inertFilter<T>(arr: readonly T[], predicate: (value: T, index: number, arr: readonly T[]) => boolean, thisArg?: any): readonly T[]; | ||
| declare function inertFilter<T>(arr: readonly T[] | undefined, predicate: (value: T, index: number, arr: readonly T[]) => boolean, thisArg?: any): readonly T[] | undefined; | ||
| interface Coalesce { | ||
| <T>(arr: readonly T[]): readonly Truthy<T>[]; | ||
| <T>(arr: T[]): Truthy<T>[]; | ||
| } | ||
| /** | ||
| * @returns the same array if all its items are truthy, otherwise a new array with all falsy items removed. | ||
| */ | ||
| declare const coalesce: Coalesce; | ||
| export { type Coalesce, type Defined, type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, type Truthy, asArray, asNumber, asObject, asPlainObject, asString, asTrue, coalesce, inertFilter, inertFilterMap, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; |
+59
-19
@@ -1,9 +0,9 @@ | ||
| type _ = undefined; | ||
| /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */ | ||
| type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1]; | ||
| type Defined<T> = Exclude<T, undefined>; | ||
| /** Returns `true` if `x` is not `undefined`. */ | ||
| declare const isDefined: <T>(x: T | _) => x is Exclude<T, _>; | ||
| declare const isDefined: <T>(x: T | undefined) => x is Defined<T>; | ||
| declare const isTrue: (x: unknown) => x is true; | ||
| /** Returns `true` if `x` is `true`, otherwise returns `undefined`. */ | ||
| declare const toTrue: (x: unknown) => true | _; | ||
| declare const toTrue: (x: unknown) => true | undefined; | ||
| /** Returns `true` if `x` is `true`, otherwise returns `false`. */ | ||
@@ -16,14 +16,15 @@ declare const asTrue: (x: unknown) => boolean; | ||
| /** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */ | ||
| declare const toFalsy: <T>(x: T) => ExtractFalsy<T> | _; | ||
| declare const toFalsy: <T>(x: T) => ExtractFalsy<T> | undefined; | ||
| type Truthy<T> = Exclude<T, Falsy>; | ||
| /** Returns `true` if `Boolean(x)` is `true`. */ | ||
| declare const isTruthy: <T>(x: T) => x is Exclude<T, Falsy>; | ||
| declare const isTruthy: <T>(x: T) => x is Truthy<T>; | ||
| /** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */ | ||
| declare const toTruthy: <T>(x: T) => Exclude<T, Falsy> | _; | ||
| declare const toTruthy: <T>(x: T) => Truthy<T> | undefined; | ||
| declare const isBoolean: (x: unknown) => x is boolean; | ||
| /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */ | ||
| declare const toBoolean: (x: unknown) => boolean | _; | ||
| declare const toBoolean: (x: unknown) => boolean | undefined; | ||
| /** Returns `true` if `x` is a number, returns `false` if `x` is `NaN` or other value. */ | ||
| declare const isNumber: (x: unknown) => x is number; | ||
| /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `undefined`. */ | ||
| declare const toNumber: (x: unknown) => number | _; | ||
| declare const toNumber: (x: unknown) => number | undefined; | ||
| /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `0`. */ | ||
@@ -34,3 +35,3 @@ declare const asNumber: (x: unknown) => number; | ||
| /** Returns `x` if `x` is a string, otherwise returns `undefined`. */ | ||
| declare const toString: (x: unknown) => string | _; | ||
| declare const toString: (x: unknown) => string | undefined; | ||
| /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */ | ||
@@ -41,3 +42,3 @@ declare const asString: (x: unknown) => string; | ||
| /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyString: (x: unknown) => string | _; | ||
| declare const toNonEmptyString: (x: unknown) => string | undefined; | ||
| /** Extracts array type from `T`, or `unknown[]` if no array type found. */ | ||
@@ -47,3 +48,3 @@ type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>; | ||
| /** Returns `x` if `x` is an array. */ | ||
| declare const toArray: <T>(x: T) => ExtractArray<T> | _; | ||
| declare const toArray: <T>(x: T) => ExtractArray<T> | undefined; | ||
| /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
@@ -54,3 +55,3 @@ declare const asArray: <T>(x: T) => ExtractArray<T>; | ||
| /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _; | ||
| declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | undefined; | ||
| type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>; | ||
@@ -60,3 +61,3 @@ /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| /** Returns `x` if `x` is an object (including array). */ | ||
| declare const toObject: <T>(x: T) => ExtractObject<T> | _; | ||
| declare const toObject: <T>(x: T) => ExtractObject<T> | undefined; | ||
| /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
@@ -71,3 +72,3 @@ declare const asObject: <T>(x: T) => ExtractObject<T>; | ||
| /** Returns `x` if `x` is a plain object. */ | ||
| declare const toPlainObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| declare const toPlainObject: <T>(x: T) => ExtractPlainObject<T> | undefined; | ||
| /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */ | ||
@@ -78,7 +79,7 @@ declare const asPlainObject: <T>(x: T) => ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object and has at least one key. */ | ||
| declare const toNonEmptyPlainObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| declare const toNonEmptyPlainObject: <T>(x: T) => ExtractPlainObject<T> | undefined; | ||
| /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */ | ||
| declare const isNonEmptyJSONObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyJSONObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| declare const toNonEmptyJSONObject: <T>(x: T) => ExtractPlainObject<T> | undefined; | ||
| type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject<T>]; | ||
@@ -89,5 +90,5 @@ /** | ||
| */ | ||
| declare const toPlainObjectOf: <T, U extends ExtractPlainObjectValue<T>>(x: T, f: (v: ExtractPlainObjectValue<T>) => v is U) => Record<PropertyKey, U> | _; | ||
| declare const toPlainObjectOf: <T, U extends ExtractPlainObjectValue<T>>(x: T, f: (v: ExtractPlainObjectValue<T>) => v is U) => Record<PropertyKey, U> | undefined; | ||
| /** Filter props from object `x` whose values are `true`. */ | ||
| declare const toPlainObjectOfTrue: (x: unknown) => Record<PropertyKey, true> | _; | ||
| declare const toPlainObjectOfTrue: (x: unknown) => Record<PropertyKey, true> | undefined; | ||
| /** | ||
@@ -107,2 +108,41 @@ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if | ||
| export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| /** | ||
| * Lazy filterMap that avoids creating a new array when possible. | ||
| * | ||
| * @returns the original array if `callbackfn` returns the same value for all items, otherwise a new array with the mapped items. | ||
| * | ||
| * @param arr | ||
| * @param callbackfn Return `undefined` to filter out the item, or return a value to map the item. | ||
| * @param thisArg | ||
| */ | ||
| declare function inertFilterMap<T, U = T>(arr: T[], callbackfn: (value: T, index: number, arr: T[]) => U | undefined, thisArg?: any): Defined<U>[]; | ||
| declare function inertFilterMap<T, U = T>(arr: T[] | undefined, callbackfn: (value: T, index: number, arr: T[]) => U | undefined, thisArg?: any): Defined<U>[] | undefined; | ||
| declare function inertFilterMap<T, U = T>(arr: readonly T[], callbackfn: (value: T, index: number, arr: readonly T[]) => U | undefined, thisArg?: any): readonly Defined<U>[]; | ||
| declare function inertFilterMap<T, U = T>(arr: readonly T[] | undefined, callbackfn: (value: T, index: number, arr: readonly T[]) => U | undefined, thisArg?: any): readonly Defined<U>[] | undefined; | ||
| /** | ||
| * Lazy filter that avoids creating a new array when possible. | ||
| * | ||
| * @returns the original array if `predicate` returns `true` for all items, otherwise a new array with the filtered items. | ||
| * | ||
| * @param arr | ||
| * @param predicate Return `false` to filter out the item, or `true` to keep it. | ||
| * @param thisArg | ||
| */ | ||
| declare function inertFilter<T, U extends T>(arr: T[], predicate: (value: T, index: number, arr: T[]) => value is U, thisArg?: any): U[]; | ||
| declare function inertFilter<T, U extends T>(arr: T[] | undefined, predicate: (value: T, index: number, arr: T[]) => value is U, thisArg?: any): U[] | undefined; | ||
| declare function inertFilter<T, U extends T>(arr: readonly T[], predicate: (value: T, index: number, arr: readonly T[]) => value is U, thisArg?: any): readonly U[]; | ||
| declare function inertFilter<T, U extends T>(arr: readonly T[] | undefined, predicate: (value: T, index: number, arr: readonly T[]) => value is U, thisArg?: any): readonly U[] | undefined; | ||
| declare function inertFilter<T>(arr: T[], predicate: (value: T, index: number, arr: T[]) => boolean, thisArg?: any): T[]; | ||
| declare function inertFilter<T>(arr: T[] | undefined, predicate: (value: T, index: number, arr: T[]) => boolean, thisArg?: any): T[] | undefined; | ||
| declare function inertFilter<T>(arr: readonly T[], predicate: (value: T, index: number, arr: readonly T[]) => boolean, thisArg?: any): readonly T[]; | ||
| declare function inertFilter<T>(arr: readonly T[] | undefined, predicate: (value: T, index: number, arr: readonly T[]) => boolean, thisArg?: any): readonly T[] | undefined; | ||
| interface Coalesce { | ||
| <T>(arr: readonly T[]): readonly Truthy<T>[]; | ||
| <T>(arr: T[]): Truthy<T>[]; | ||
| } | ||
| /** | ||
| * @returns the same array if all its items are truthy, otherwise a new array with all falsy items removed. | ||
| */ | ||
| declare const coalesce: Coalesce; | ||
| export { type Coalesce, type Defined, type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, type Truthy, asArray, asNumber, asObject, asPlainObject, asString, asTrue, coalesce, inertFilter, inertFilterMap, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; |
+52
-15
| 'use strict'; | ||
| // src/is-to-as.ts | ||
| var _ = void 0; | ||
| var isDefined = (x) => x !== _; | ||
| var isDefined = (x) => x !== void 0; | ||
| var isTrue = (x) => x === true; | ||
| var toTrue = (x) => x === true ? true : _; | ||
| var toTrue = (x) => x === true ? true : void 0; | ||
| var asTrue = (x) => x === true ? x : false; | ||
| var isFalsy = (x) => !x; | ||
| var toFalsy = (x) => isFalsy(x) ? x : _; | ||
| var toFalsy = (x) => isFalsy(x) ? x : void 0; | ||
| var isTruthy = (x) => !!x; | ||
| var toTruthy = (x) => isTruthy(x) ? x : _; | ||
| var toTruthy = (x) => isTruthy(x) ? x : void 0; | ||
| var isBoolean = (x) => x === true || x === false; | ||
| var toBoolean = (x) => isBoolean(x) ? x : _; | ||
| var toBoolean = (x) => isBoolean(x) ? x : void 0; | ||
| var isNumber = (x) => typeof x === "number" && x === x; | ||
| var toNumber = (x) => isNumber(x) ? x : _; | ||
| var toNumber = (x) => isNumber(x) ? x : void 0; | ||
| var asNumber = (x) => isNumber(x) ? x : 0; | ||
| var isString = (x) => typeof x === "string"; | ||
| var toString = (x) => isString(x) ? x : _; | ||
| var toString = (x) => isString(x) ? x : void 0; | ||
| var asString = (x) => isString(x) ? x : ""; | ||
| var isNonEmptyString = (x) => isString(x) && x !== ""; | ||
| var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _; | ||
| var toNonEmptyString = (x) => isNonEmptyString(x) ? x : void 0; | ||
| var isArray = Array.isArray; | ||
| var toArray = (x) => isArray(x) ? x : _; | ||
| var toArray = (x) => isArray(x) ? x : void 0; | ||
| var asArray = (x) => isArray(x) ? x : []; | ||
| var isNonEmptyArray = (x) => isArray(x) && x.length > 0; | ||
| var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : _; | ||
| var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : void 0; | ||
| var isObject = (x) => x !== null && typeof x === "object"; | ||
| var toObject = (x) => isObject(x) ? x : _; | ||
| var toObject = (x) => isObject(x) ? x : void 0; | ||
| var asObject = (x) => isObject(x) ? x : {}; | ||
| var isPlainObject = (x) => isObject(x) && !isArray(x); | ||
| var toPlainObject = (x) => isPlainObject(x) ? x : _; | ||
| var toPlainObject = (x) => isPlainObject(x) ? x : void 0; | ||
| var asPlainObject = (x) => isPlainObject(x) ? x : {}; | ||
@@ -45,5 +44,5 @@ var walkPlainObjectValues = (x, predicate) => { | ||
| var isNonEmptyPlainObject = (x) => walkPlainObjectValues(x); | ||
| var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : _; | ||
| var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : void 0; | ||
| var isNonEmptyJSONObject = (x) => walkPlainObjectValues(x, isDefined); | ||
| var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : _; | ||
| var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : void 0; | ||
| var toPlainObjectOf = (x, f) => { | ||
@@ -85,2 +84,37 @@ if (isPlainObject(x)) { | ||
| // src/array.ts | ||
| function inertFilterMap(arr, callbackfn, thisArg) { | ||
| if (arr) { | ||
| let result; | ||
| let index = -1; | ||
| let length = arr.length; | ||
| while (++index < length) { | ||
| const newItem = callbackfn.call(thisArg, arr[index], index, arr); | ||
| if (newItem === void 0 || !Object.is(newItem, arr[index])) { | ||
| result ??= arr.slice(0, index); | ||
| } | ||
| if (newItem !== void 0) { | ||
| result?.push(newItem); | ||
| } | ||
| } | ||
| return result || arr; | ||
| } | ||
| } | ||
| function inertFilter(arr, predicate, thisArg) { | ||
| if (arr) { | ||
| let result; | ||
| let index = -1; | ||
| let length = arr.length; | ||
| while (++index < length) { | ||
| if (predicate.call(thisArg, arr[index], index, arr)) { | ||
| result?.push(arr[index]); | ||
| } else { | ||
| result ??= arr.slice(0, index); | ||
| } | ||
| } | ||
| return result || arr; | ||
| } | ||
| } | ||
| var coalesce = (arr) => inertFilter(arr, isTruthy); | ||
| exports.asArray = asArray; | ||
@@ -92,2 +126,5 @@ exports.asNumber = asNumber; | ||
| exports.asTrue = asTrue; | ||
| exports.coalesce = coalesce; | ||
| exports.inertFilter = inertFilter; | ||
| exports.inertFilterMap = inertFilterMap; | ||
| exports.isArray = isArray; | ||
@@ -94,0 +131,0 @@ exports.isBoolean = isBoolean; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";;;AAAA,IAAM,CAAA,GAAI,MAAA;AAOH,IAAM,SAAA,GAAY,CAAI,CAAA,KAAiC,CAAA,KAAM;AAE7D,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM;AAGhD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM,OAAO,IAAA,GAAO;AAG9D,IAAM,MAAA,GAAS,CAAC,CAAA,KAAyB,CAAA,KAAM,OAAO,CAAA,GAAI;AAO1D,IAAM,OAAA,GAAU,CAAI,C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O,GAAU,CAAA,EAAG;AAC1D,QAAA,MAAA,EAAQ,IAAA,CAAK,GAAA,CAAI,KAAK,CAAC,CAAA;AAAA,MACzB,CAAA,MAAO;AACL,QAAA,MAAA,KAAW,GAAA,CAAI,KAAA,CAAM,CAAA,EAAG,KAAK,CAAA;AAAA,MAC/B;AAAA,IACF;AACA,IAAA,OAAO,MAAA,IAAU,GAAA;AAAA,EACnB;AACF;AAUO,IAAM,QAAA,GAAqB,CAAA,GAAA,KAAO,WAAA,CAAY,GAAA,EAAK,QAAQ","file":"index.js"} |
+50
-16
| // src/is-to-as.ts | ||
| var _ = void 0; | ||
| var isDefined = (x) => x !== _; | ||
| var isDefined = (x) => x !== void 0; | ||
| var isTrue = (x) => x === true; | ||
| var toTrue = (x) => x === true ? true : _; | ||
| var toTrue = (x) => x === true ? true : void 0; | ||
| var asTrue = (x) => x === true ? x : false; | ||
| var isFalsy = (x) => !x; | ||
| var toFalsy = (x) => isFalsy(x) ? x : _; | ||
| var toFalsy = (x) => isFalsy(x) ? x : void 0; | ||
| var isTruthy = (x) => !!x; | ||
| var toTruthy = (x) => isTruthy(x) ? x : _; | ||
| var toTruthy = (x) => isTruthy(x) ? x : void 0; | ||
| var isBoolean = (x) => x === true || x === false; | ||
| var toBoolean = (x) => isBoolean(x) ? x : _; | ||
| var toBoolean = (x) => isBoolean(x) ? x : void 0; | ||
| var isNumber = (x) => typeof x === "number" && x === x; | ||
| var toNumber = (x) => isNumber(x) ? x : _; | ||
| var toNumber = (x) => isNumber(x) ? x : void 0; | ||
| var asNumber = (x) => isNumber(x) ? x : 0; | ||
| var isString = (x) => typeof x === "string"; | ||
| var toString = (x) => isString(x) ? x : _; | ||
| var toString = (x) => isString(x) ? x : void 0; | ||
| var asString = (x) => isString(x) ? x : ""; | ||
| var isNonEmptyString = (x) => isString(x) && x !== ""; | ||
| var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _; | ||
| var toNonEmptyString = (x) => isNonEmptyString(x) ? x : void 0; | ||
| var isArray = Array.isArray; | ||
| var toArray = (x) => isArray(x) ? x : _; | ||
| var toArray = (x) => isArray(x) ? x : void 0; | ||
| var asArray = (x) => isArray(x) ? x : []; | ||
| var isNonEmptyArray = (x) => isArray(x) && x.length > 0; | ||
| var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : _; | ||
| var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : void 0; | ||
| var isObject = (x) => x !== null && typeof x === "object"; | ||
| var toObject = (x) => isObject(x) ? x : _; | ||
| var toObject = (x) => isObject(x) ? x : void 0; | ||
| var asObject = (x) => isObject(x) ? x : {}; | ||
| var isPlainObject = (x) => isObject(x) && !isArray(x); | ||
| var toPlainObject = (x) => isPlainObject(x) ? x : _; | ||
| var toPlainObject = (x) => isPlainObject(x) ? x : void 0; | ||
| var asPlainObject = (x) => isPlainObject(x) ? x : {}; | ||
@@ -43,5 +42,5 @@ var walkPlainObjectValues = (x, predicate) => { | ||
| var isNonEmptyPlainObject = (x) => walkPlainObjectValues(x); | ||
| var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : _; | ||
| var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : void 0; | ||
| var isNonEmptyJSONObject = (x) => walkPlainObjectValues(x, isDefined); | ||
| var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : _; | ||
| var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : void 0; | ||
| var toPlainObjectOf = (x, f) => { | ||
@@ -83,4 +82,39 @@ if (isPlainObject(x)) { | ||
| export { asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| // src/array.ts | ||
| function inertFilterMap(arr, callbackfn, thisArg) { | ||
| if (arr) { | ||
| let result; | ||
| let index = -1; | ||
| let length = arr.length; | ||
| while (++index < length) { | ||
| const newItem = callbackfn.call(thisArg, arr[index], index, arr); | ||
| if (newItem === void 0 || !Object.is(newItem, arr[index])) { | ||
| result ??= arr.slice(0, index); | ||
| } | ||
| if (newItem !== void 0) { | ||
| result?.push(newItem); | ||
| } | ||
| } | ||
| return result || arr; | ||
| } | ||
| } | ||
| function inertFilter(arr, predicate, thisArg) { | ||
| if (arr) { | ||
| let result; | ||
| let index = -1; | ||
| let length = arr.length; | ||
| while (++index < length) { | ||
| if (predicate.call(thisArg, arr[index], index, arr)) { | ||
| result?.push(arr[index]); | ||
| } else { | ||
| result ??= arr.slice(0, index); | ||
| } | ||
| } | ||
| return result || arr; | ||
| } | ||
| } | ||
| var coalesce = (arr) => inertFilter(arr, isTruthy); | ||
| export { asArray, asNumber, asObject, asPlainObject, asString, asTrue, coalesce, inertFilter, inertFilterMap, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| //# sourceMappingURL=index.mjs.map | ||
| //# sourceMappingURL=index.mjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";AAAA,IAAM,CAAA,GAAI,MAAA;AAOH,IAAM,SAAA,GAAY,CAAI,CAAA,KAAiC,CAAA,KA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,EAAK;AACP,IAAA,IAAI,MAAA;AACJ,IAAA,IAAI,KAAA,GAAQ,EAAA;AACZ,IAAA,IAAI,SAAS,GAAA,CAAI,MAAA;AACjB,IAAA,OAAO,EAAE,QAAQ,MAAA,EAAQ;AACvB,MAAA,IAAI,SAAA,CAAU,KAAK,OAAA,EAAS,GAAA,CAAI,KAAK,CAAA,EAAG,KAAA,EAAO,GAAU,CAAA,EAAG;AAC1D,QAAA,MAAA,EAAQ,IAAA,CAAK,GAAA,CAAI,KAAK,CAAC,CAAA;AAAA,MACzB,CAAA,MAAO;AACL,QAAA,MAAA,KAAW,GAAA,CAAI,KAAA,CAAM,CAAA,EAAG,KAAK,CAAA;AAAA,MAC/B;AAAA,IACF;AACA,IAAA,OAAO,MAAA,IAAU,GAAA;AAAA,EACnB;AACF;AAUO,IAAM,QAAA,GAAqB,CAAA,GAAA,KAAO,WAAA,CAAY,GAAA,EAAK,QAAQ","file":"index.mjs"} |
+1
-1
| { | ||
| "name": "@wopjs/cast", | ||
| "version": "0.1.11", | ||
| "version": "0.1.12", | ||
| "description": "Type-safe utilities for filtering and coercing unknown values in TypeScript.", | ||
@@ -5,0 +5,0 @@ "repository": "wopjs/cast", |
+1
-0
| export * from "./is-to-as"; | ||
| export * from "./returns"; | ||
| export * from "./array"; |
+24
-21
@@ -1,9 +0,8 @@ | ||
| const _ = undefined; | ||
| export type _ = undefined; | ||
| /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */ | ||
| export type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1]; | ||
| export type Defined<T> = Exclude<T, undefined>; | ||
| /** Returns `true` if `x` is not `undefined`. */ | ||
| export const isDefined = <T>(x: T | _): x is Exclude<T, _> => x !== _; | ||
| export const isDefined = <T>(x: T | undefined): x is Defined<T> => x !== undefined; | ||
@@ -13,3 +12,3 @@ export const isTrue = (x: unknown): x is true => x === true; | ||
| /** Returns `true` if `x` is `true`, otherwise returns `undefined`. */ | ||
| export const toTrue = (x: unknown): true | _ => (x === true ? true : _); | ||
| export const toTrue = (x: unknown): true | undefined => (x === true ? true : undefined); | ||
@@ -27,9 +26,11 @@ /** Returns `true` if `x` is `true`, otherwise returns `false`. */ | ||
| /** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */ | ||
| export const toFalsy = <T>(x: T): ExtractFalsy<T> | _ => (isFalsy(x) ? x : _); | ||
| export const toFalsy = <T>(x: T): ExtractFalsy<T> | undefined => (isFalsy(x) ? x : undefined); | ||
| export type Truthy<T> = Exclude<T, Falsy>; | ||
| /** Returns `true` if `Boolean(x)` is `true`. */ | ||
| export const isTruthy = <T>(x: T): x is Exclude<T, Falsy> => !!x; | ||
| export const isTruthy = <T>(x: T): x is Truthy<T> => !!x; | ||
| /** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */ | ||
| export const toTruthy = <T>(x: T): Exclude<T, Falsy> | _ => (isTruthy(x) ? x : _); | ||
| export const toTruthy = <T>(x: T): Truthy<T> | undefined => (isTruthy(x) ? x : undefined); | ||
@@ -39,3 +40,3 @@ export const isBoolean = (x: unknown): x is boolean => x === true || x === false; | ||
| /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */ | ||
| export const toBoolean = (x: unknown): boolean | _ => (isBoolean(x) ? x : _); | ||
| export const toBoolean = (x: unknown): boolean | undefined => (isBoolean(x) ? x : undefined); | ||
@@ -46,3 +47,3 @@ /** Returns `true` if `x` is a number, returns `false` if `x` is `NaN` or other value. */ | ||
| /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `undefined`. */ | ||
| export const toNumber = (x: unknown): number | _ => (isNumber(x) ? x : _); | ||
| export const toNumber = (x: unknown): number | undefined => (isNumber(x) ? x : undefined); | ||
@@ -56,3 +57,3 @@ /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `0`. */ | ||
| /** Returns `x` if `x` is a string, otherwise returns `undefined`. */ | ||
| export const toString = (x: unknown): string | _ => (isString(x) ? x : _); | ||
| export const toString = (x: unknown): string | undefined => (isString(x) ? x : undefined); | ||
@@ -66,3 +67,3 @@ /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */ | ||
| /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */ | ||
| export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _); | ||
| export const toNonEmptyString = (x: unknown): string | undefined => (isNonEmptyString(x) ? x : undefined); | ||
@@ -75,3 +76,3 @@ /** Extracts array type from `T`, or `unknown[]` if no array type found. */ | ||
| /** Returns `x` if `x` is an array. */ | ||
| export const toArray = <T>(x: T): ExtractArray<T> | _ => (isArray(x) ? x : _); | ||
| export const toArray = <T>(x: T): ExtractArray<T> | undefined => (isArray(x) ? x : undefined); | ||
@@ -85,3 +86,3 @@ /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
| /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */ | ||
| export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? x : _); | ||
| export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | undefined => (isNonEmptyArray(x) ? x : undefined); | ||
@@ -94,3 +95,3 @@ export type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>; | ||
| /** Returns `x` if `x` is an object (including array). */ | ||
| export const toObject = <T>(x: T): ExtractObject<T> | _ => (isObject(x) ? x : _); | ||
| export const toObject = <T>(x: T): ExtractObject<T> | undefined => (isObject(x) ? x : undefined); | ||
@@ -110,3 +111,3 @@ /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
| /** Returns `x` if `x` is a plain object. */ | ||
| export const toPlainObject = <T>(x: T): ExtractPlainObject<T> | _ => (isPlainObject(x) ? x : _); | ||
| export const toPlainObject = <T>(x: T): ExtractPlainObject<T> | undefined => (isPlainObject(x) ? x : undefined); | ||
@@ -131,3 +132,4 @@ /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */ | ||
| /** Returns `x` if `x` is a plain object and has at least one key. */ | ||
| export const toNonEmptyPlainObject = <T>(x: T): ExtractPlainObject<T> | _ => (isNonEmptyPlainObject(x) ? x : _); | ||
| export const toNonEmptyPlainObject = <T>(x: T): ExtractPlainObject<T> | undefined => | ||
| isNonEmptyPlainObject(x) ? x : undefined; | ||
@@ -138,3 +140,4 @@ /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */ | ||
| /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */ | ||
| export const toNonEmptyJSONObject = <T>(x: T): ExtractPlainObject<T> | _ => (isNonEmptyJSONObject(x) ? x : _); | ||
| export const toNonEmptyJSONObject = <T>(x: T): ExtractPlainObject<T> | undefined => | ||
| isNonEmptyJSONObject(x) ? x : undefined; | ||
@@ -150,3 +153,3 @@ type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject<T>]; | ||
| f: (v: ExtractPlainObjectValue<T>) => v is U | ||
| ): Record<PropertyKey, U> | _ => { | ||
| ): Record<PropertyKey, U> | undefined => { | ||
| if (isPlainObject(x)) { | ||
@@ -156,3 +159,3 @@ let index = -1; | ||
| let length = props.length; | ||
| let result: Record<PropertyKey, U> | _; | ||
| let result: Record<PropertyKey, U> | undefined; | ||
@@ -172,3 +175,3 @@ while (++index < length) { | ||
| /** Filter props from object `x` whose values are `true`. */ | ||
| export const toPlainObjectOfTrue = (x: unknown): Record<PropertyKey, true> | _ => toPlainObjectOf(x, isTrue); | ||
| export const toPlainObjectOfTrue = (x: unknown): Record<PropertyKey, true> | undefined => toPlainObjectOf(x, isTrue); | ||
@@ -175,0 +178,0 @@ /** |
148702
28.13%16
14.29%3045
29.8%