From 971c0390865bf28a0dd1d651e8b561537e8a2d20 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Fri, 26 Jan 2018 15:17:46 +0100 Subject: [PATCH] feat(complex-matcher): number are matched recursively --- packages/complex-matcher/src/index.js | 10 +++++++++- packages/complex-matcher/src/index.spec.js | 9 ++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/complex-matcher/src/index.js b/packages/complex-matcher/src/index.js index 7e881df21..347adb999 100644 --- a/packages/complex-matcher/src/index.js +++ b/packages/complex-matcher/src/index.js @@ -146,10 +146,18 @@ export class NumberNode extends Node { super() this.value = value + + // should not be enumerable for the tests + Object.defineProperty(this, 'match', { + value: this.match.bind(this), + }) } match (value) { - return value === this.value + return ( + value === this.value || + (value !== null && typeof value === 'object' && some(value, this.match)) + ) } toString () { diff --git a/packages/complex-matcher/src/index.spec.js b/packages/complex-matcher/src/index.spec.js index b39e419cc..edc21419b 100644 --- a/packages/complex-matcher/src/index.spec.js +++ b/packages/complex-matcher/src/index.spec.js @@ -1,12 +1,13 @@ /* eslint-env jest */ +import { ast, pattern } from './index.fixtures' import { getPropertyClausesStrings, Null, + NumberNode, parse, setPropertyClause, } from './' -import { ast, pattern } from './index.fixtures' it('getPropertyClausesStrings', () => { const tmp = getPropertyClausesStrings(parse('foo bar:baz baz:|(foo bar)')) @@ -40,6 +41,12 @@ describe('parse', () => { }) }) +describe('Number', () => { + it('match a number recursively', () => { + expect(new NumberNode(3).match([{ foo: 3 }])).toBe(true) + }) +}) + describe('setPropertyClause', () => { it('creates a node if none passed', () => { expect(setPropertyClause(undefined, 'foo', 'bar').toString()).toBe('foo:bar')