array-filter-utils
TypeScript icon, indicating that this package has built-in type declarations

0.1.11 • Public • Published

array-filter-utils

array-filter-utils is a library offering a bulk of functions to filter arrays with type management.

GitHub Coveralls GitHub tag checks state npm GitHub tag (latest by date)

Usage

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>

Examples

array.filter(requiredIsSet)

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 },
]);

array.filter(requiredIsNotNullable)

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 }]);

array.filter(isSet)

remove undefined elements

const testArray = ["valid", null, undefined, 0, false] as const;

const result = testArray.filter(isSet);

expect(result).toStrictEqual(["valid", null, 0, false]);

array.filter(notNullable)

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]);

Package Sidebar

Install

npm i array-filter-utils

Weekly Downloads

124

Version

0.1.11

License

MIT

Unpacked Size

8.45 kB

Total Files

5

Last publish

Collaborators

  • lmarvaud