fp-filters

0.2.7 • Public • Published

https://github.com/Oaxoa/fp-filters

fp-filters

A collection of common filter functions that are written (and can be used) in a functional programming style.

fp-filters functions are:

  1. pure
  2. tiny
  3. composable
  4. zero-dependencies
  5. grouped by semantics
  6. tree-shakeable
  7. 100% tested by design

Why

See how fp-filters allows you to stop rewriting the same code over and over again and greatly improves readability:

Examples

values and numbers examples:

// JS
array.filter((id) => id === userId);
// fp-filters
array.filter(is(userId));
// JS
array.filter((element) => element !== 0);
// fp-filters
array.filter(isNotZero);
// JS
array.filter((arg) => arg % 2 === 0);
// fp-filters
array.filter(isEven);
// JS
array.filter((arg) => arg >= 10 && arg <= 50);
// fp-filters
array.filter(clamp(10, 50));

collections examples:

const selectedUsers = ['John', 'Gina', 'Ed'];
// JS
array.filter((arg) => selectedUsers.includes(arg));
// fp-filters
array.filter(isOneOf(selectedUsers));
// JS
array.filter((obj) => obj.valid !== undefined && obj.id !== undefined && obj.plu !== undefined);
// fp-filters
array.filter(hasProps(['valid', 'id', 'plu']));
// JS
array.filter((obj) => obj.country === countryId && obj.plu === plu);
// fp-filters
array.filter(hasProps(['country', 'plu'], [countryId, plu]));
// JS
array.filter((obj) => obj.id === someOtherObj.id && obj.brand === someOtherObj.brand);
// fp-filters
array.filter(sameProps(someOtherObj, ['id', 'brand']));
// JS
array.filter((arg) => arg.length > 0);
// fp-filters
array.filter(isNotEmpty);

types examples:

// JS
array.filter((arg) => arg !== null && arg !== undefined);
// fp-filters
array.filter(isNotNil);
// JS
array.filter((arg) => typeof arg === 'boolean');
// fp-filters
array.filter(isBoolean);
// do not be tricked by `array.filter(Boolean);` is very different as 
// it casts the content and then evaluate its truthyness

position examples:

// JS
array.filter((arg, index) => index % 3 === 0 || index % 2 === 2);
// fp-filters
array.filter(pattern(false, true, true));
// JS
array.filter((arg, index) => index % 3 === 1);
// fp-filters
array.filter(everyN(3, 1));

Negate or combine filters

Most of the functions include aliases for their negated versions:

array.filter(isNot(5))
array.filter(isNotNil)
array.filter(isNotEmpty)
array.filter(isNotInstanceOf(SomeClass));

but you can make your own.

fp-filters offers very powerful functions to combine or negate filters

Some examples:

array.filter(not(is(5)));
array.filter(and(gte(MIN_PRICE), not(isRound)));
array.filter(or(is('admin'), and(startsWith('user_'), isLowerCase)));

Getting started

Installation

fp-filters runs on Node.js and is available as a NPM package.

npm install --save fp-filters

or

yarn add fp-filters

Contributions

Looking forward for some help, especially with the TypeScript annotations which is currently just a draft.

MIT

Copyright (c) 2023-present, Pierluigi Pesenti (Oaxoa)

Package Sidebar

Install

npm i fp-filters

Weekly Downloads

0

Version

0.2.7

License

MIT

Unpacked Size

575 kB

Total Files

38

Last publish

Collaborators

  • oaxoa