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:
// 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));
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);
// 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
// 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));
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
array.filter(not(is(5)));
array.filter(and(gte(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
Looking forward for some help, especially with the TypeScript annotations which is currently just a draft.
Copyright (c) 2023-present, Pierluigi Pesenti (Oaxoa)