xen-orchestra/@vates/predicates
Julien Fontanet e7a97fd175 test(audit-core,predicates,otp,xo-server): move from tap to test
Support of Mocha syntax is much easier than in latest versions of tap
2024-02-02 12:52:11 +01:00
..
.npmignore feat(@vates/predicates): utils to compose predicates 2022-02-10 16:55:15 +01:00
.USAGE.md feat(predicates): not operator 2022-10-20 12:47:02 +02:00
index.js feat(predicates): not operator 2022-10-20 12:47:02 +02:00
index.test.js test(audit-core,predicates,otp,xo-server): move from tap to test 2024-02-02 12:52:11 +01:00
package.json test(audit-core,predicates,otp,xo-server): move from tap to test 2024-02-02 12:52:11 +01:00
README.md docs: uniformize code blocks 2023-02-06 11:25:12 +01:00

@vates/predicates

Package Version License PackagePhobia Node compatibility

Utilities to compose predicates

Install

Installation of the npm package:

npm install --save @vates/predicates

Usage

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 = predicate2

Predicates 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 true iff every predicate returns true.

const isBetween3And7 = every(
  n => n >= 3,
  n => n <= 7
)

isBetween3And10(0)
// → false

isBetween3And10(5)
// → true

isBetween3And10(10)
// → false

not(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)
// false

some(predicates)

Returns a predicate that returns true iff some predicate returns true.

const isAliceOrBob = some(
  name => name === 'Alice',
  name => name === 'Bob'
)

isAliceOrBob('Alice')
// → true

isAliceOrBob('Bob')
// → true

isAliceOrBob('Oscar')
// → false

Contributions

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.

License

ISC © Vates SAS