1.1.0 • Published 3 years ago
@vates/predicates v1.1.0
@vates/predicates
Utilities to compose predicates
Install
Installation of the npm package:
> npm install --save @vates/predicatesUsage
undefined predicates are ignored and undefined is returned if all predicates are undefined, this permits the most efficient composition:
const compositePredicate = not(every(undefined, some(not(predicate2), undefined)))
// ends up as
const compositePredicate = predicate2Predicates can also be passed wrapped in an array:
const compositePredicate = every([predicate1, some([predicate2, predicate3])])this and all arguments are passed to the nested predicates.
every(predicates)
Returns a predicate that returns
trueiff every predicate returnstrue.
const isBetween3And7 = every(
n => n >= 3,
n => n <= 7
)
isBetween3And10(0)
// → false
isBetween3And10(5)
// → true
isBetween3And10(10)
// → falsenot(predicate)
Returns a predicate that returns the negation of the predicate.
const isEven = n => n % 2 === 0
const isOdd = not(isEven)
isOdd(1)
// true
isOdd(2)
// falsesome(predicates)
Returns a predicate that returns
trueiff some predicate returnstrue.
const isAliceOrBob = some(
name => name === 'Alice',
name => name === 'Bob'
)
isAliceOrBob('Alice')
// → true
isAliceOrBob('Bob')
// → true
isAliceOrBob('Oscar')
// → falseContributions
Contributions are very welcomed, either on the documentation or on the code.
You may:
- report any issue you've encountered;
- fork and create a pull request.