xen-orchestra/packages/value-matcher
2021-03-11 12:15:53 +01:00
..
src chore: change print width to 120 chars 2020-11-24 10:51:35 +01:00
.babelrc.js chore: change print width to 120 chars 2020-11-24 10:51:35 +01:00
.npmignore feat(value-matcher): new package (#30) 2017-12-27 15:21:27 +01:00
package.json feat(normalize-packages): delete empty bin field 2021-03-11 12:15:53 +01:00
README.md feat(value-matcher/README): document patterns 2020-10-23 10:21:52 +02:00
USAGE.md feat(value-matcher/README): document patterns 2020-10-23 10:21:52 +02:00

value-matcher

Package Version License PackagePhobia Node compatibility

Install

Installation of the npm package:

> npm install --save value-matcher

Usage

import { createPredicate } from 'value-matcher'
;[
  { user: 'sam', age: 65, active: false },
  { user: 'barney', age: 36, active: true },
  { user: 'fred', age: 40, active: false },
].filter(
  createPredicate({
    __or: [{ user: 'sam' }, { active: true }],
  })
)
// [
//   { user: 'sam', age: 65, active: false },
//   { user: 'barney', age: 36, active: true },
// ]

Supported predicates

any

The value must be strictly equal to the pattern.

const predicate = createPredicate(42)

predicate(42) // true
predicate('foo') // false

{ [property: string]: Pattern }

The value must be an object with all pattern properties matching.

const predicate = createPredicate({ foo: 'bar' })

predicate({ foo: 'bar', baz: 42 }) // true
predicate('foo') // false

Pattern[]

The value must be an array with some of its items matching each of pattern items.

const predicate = createPredicate([42, { foo: 'bar' }])

predicate([false, { foo: 'bar', baz: 42 }, null, 42]) // true
predicate('foo') // false

{ __all: Pattern[] }

All patterns must match.

{ __or: Pattern[] }

At least one pattern must match.

{ __not: Pattern }

The pattern must not match.

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