feat(value-matcher): add __and operator (#43)

This commit is contained in:
Julien Fontanet 2018-02-05 15:34:55 +01:00 committed by GitHub
parent 9d9fdcff1e
commit 57ef84ad3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,9 @@
// eslint-disable-next-line no-use-before-define // eslint-disable-next-line no-use-before-define
export type Pattern = OrPattern | NotPattern | ObjectPattern | ArrayPattern | ValuePattern export type Pattern = OrPattern | NotPattern | ObjectPattern | ArrayPattern | ValuePattern
// all patterns must match
type AndPattern = {| __and: Array<Pattern> |}
// one of the pattern must match // one of the pattern must match
type OrPattern = {| __or: Array<Pattern> |} type OrPattern = {| __or: Array<Pattern> |}
@ -32,6 +35,10 @@ const match = (pattern: Pattern, value: any) => {
if (length === 1) { if (length === 1) {
const [ key ] = keys const [ key ] = keys
if (key === '__and') {
const andPattern: AndPattern = (pattern: any)
return andPattern.__and.every(subpattern => match(subpattern, value))
}
if (key === '__or') { if (key === '__or') {
const orPattern: OrPattern = (pattern: any) const orPattern: OrPattern = (pattern: any)
return orPattern.__or.some(subpattern => match(subpattern, value)) return orPattern.__or.some(subpattern => match(subpattern, value))