array-filter-utils
is a library offering a bulk of functions to filter arrays with type
management.
npm install array-filter-utils
import { isSet } from "array-filter-utils";
const array = [1, undefined, 3];
// typeof array == Array<number | undefined>
const result = array.filter(isSet);
// typeof result == Array<number>
return true if element is set
const test_array: readonly T[] = [
{ key: "valid" },
{ key: null },
{ key: undefined },
{},
{ key: 0 },
{ key: false },
] as const;
const result = test_array.filter(requiredIsSet("key"));
expect(result).toStrictEqual([
{ key: "valid" },
{ key: null },
{ key: 0 },
{ key: false },
]);
return true if element is set and not null
const test_array: readonly T[] = [
{ key: "valid" },
{ key: null },
{ key: undefined },
{},
{ key: 0 },
{ key: false },
] as const;
const result = test_array.filter(requiredIsNotNullable("key"));
expect(result).toStrictEqual([{ key: "valid" }, { key: 0 }, { key: false }]);
remove undefined elements
const testArray = ["valid", null, undefined, 0, false] as const;
const result = testArray.filter(isSet);
expect(result).toStrictEqual(["valid", null, 0, false]);
remove null and undefined elements
const testArray = ["valid", null, undefined, 0, false] as const;
const result = testArray.filter(notNullable);
expect(result).toStrictEqual(["valid", 0, false]);