https://github.com/Oaxoa/fp-filters
A collection of common filter functions that are written (and can be used) in a functional programming style.
fp-filters functions are:
- pure
- tiny
- composable
- zero-dependencies
- grouped by semantics
- tree-shakeable
- 100% tested by design
See how fp-filters allows you to stop rewriting the same code over and over again and greatly improves readability. So that you will probably never write another filter function 🚀!
// JS
ids.filter((id) => id === currentUserId);
// fp-filters
ids.filter(is(currentUserId));
// JS
scores.filter((element) => element !== 0);
// fp-filters
scores.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(isBetween(10, 50));
const allergens = ['crustaceans', 'gluten', 'mushrooms', 'peanuts',];
// JS
const allergenIngredients = ingredients.filter((arg) => allergens.includes(arg));
// fp-filters
const allergenIngredients = ingredients.filter(isIncludedIn(allergens));
// JS
products.filter((obj) => obj.id !== undefined && obj.plu !== undefined);
// fp-filters
products.filter(hasProps(['id', 'plu']));
// JS
products.find((obj) => obj.country === countryId && obj.plu === plu);
// fp-filters
products.find(hasProps(['country', 'plu'], [countryId, plu]));
// JS
array.filter((obj) => obj.id === someOtherObj.id && obj.brand === someOtherObj.brand);
// fp-filters
array.filter(hasSameProps(someOtherObj, ['id', 'brand']));
// JS
array.filter((arg) => arg.length > 0);
// fp-filters
array.filter(isNotEmpty);
// 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);`. It is different as
// it casts the content and then evaluate its truthyness
// JS
array.filter((arg, index) => index % 3 === 1 || index % 3 === 2);
// fp-filters
array.filter(pattern(false, true, true));
// JS
array.filter((arg, index) => index % 3 === 1);
// fp-filters
array.filter(isEveryNthIndex(3, 1));
Most of the functions include aliases for their negated versions ( using fp-booleans):
// E.g.:
array.filter(is(5))
array.filter(isNot(5))
array.filter(isBetween(5, 10))
array.filter(isNotBetween(5, 10))
array.filter(isNil)
array.filter(isNotNil)
array.filter(isEmpty)
array.filter(isNotEmpty)
array.filter(isInstanceOf(SomeClass));
array.filter(isNotInstanceOf(SomeClass));
but you can make your own.
fp-filters leaverages fp-booleans 's very powerful functions to combine or negate functions
array.filter(not(is(5)));
array.filter(and(isGreaterOrEqualTo(MIN_PRICE), not(isRound)));
array.filter(or(is('admin'), and(startsWith('user_'), isLowerCase)));
fp-filters runs on Node.js and is available as a NPM package.
npm install --save fp-filters
or
yarn add fp-filters
Copyright (c) 2023-present, Pierluigi Pesenti (Oaxoa)