From ba94e799aa352c2fd472b694c745388e3e9feee8 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 13 Jan 2022 21:20:35 +0530 Subject: feat(eval): implements or --- src/eval/index.ts | 15 ++++++++------- src/types.ts | 2 +- tests/basic.spec.ts | 49 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/eval/index.ts b/src/eval/index.ts index 4c47adf..d0d7363 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -82,6 +82,14 @@ const checkExpr = ( ) }, + Or: ({ exprs }) => { + const match = exprs.find(expr => checkExpr(expr, item, list, index).length > 0) + return pipe( + match ? [group(item, index)] : [], + skip(1), + ) + }, + PropertyMatch: ({ name, expr }) => pipe( Object.prototype.hasOwnProperty.call(item ?? {}, name) @@ -103,13 +111,6 @@ const checkExpr = ( ) }, - // Or: ({ left, right }) => { - // return pipe( - // [], - // skip(1), - // ) - // }, - _: _ => { throw new Error(`TODO: ${expr.tag} not implemented for match`) }, }), ) diff --git a/src/types.ts b/src/types.ts index 97ff81d..bc30453 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,7 +16,6 @@ export type Expr = Union<{ Optional: { expr: Expr }, OneOrMore: { expr: Expr }, ZeroOrMore: { expr: Expr }, - NextItem: _, AnyItem: _, Or: { exprs: Expr[] }, AnyString: _, @@ -27,6 +26,7 @@ export type Expr = Union<{ Group: { exprs: Expr[] }, PropertyMatch: { name: string, expr: Expr }, Literal: Literal, + NextItem: _, }> export const Expr = constructors() diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index 9336841..3c49267 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -16,20 +16,22 @@ describe('Basic tests', () => { // jlog(matchAll(/[age \n]+/, [ {}, { age: 1 }, '' ])) // jlog(matchAll(/([age \n])+/, [ {}, { age: 1 }, { age: 2 }, { age: 0 }, '' ])) // jlog(matchAll(/\n+/, [ '', 1, 2, 0, '6', 5, '' ])) - - jlog(matchAll(/ -2.05|2|true|\s|\T /, [ 2, -2.05, 5, -2.05, 2.05, 0.05, 'wow', '-2.05', '-2' ])) }) describe('matchAll', () => { - it ('should match literals', () => { - expect(matchAll(/ -2.05 /, [ 2, NaN, -2.05, '-2.05', -2.05, 2.05 ]).groups).toEqual([ + it('should match literals', () => { + expect( + matchAll(/ -2.05 /, [2, NaN, -2.05, '-2.05', -2.05, 2.05]).groups, + ).toEqual([ { value: -2.05, index: 2 }, { value: -2.05, index: 4 }, ]) - expect(matchAll(/ true /, [ 1, true, false, true, 'true' ]).groups).toEqual([ - { value: true, index: 1 }, - { value: true, index: 3 }, - ]) + expect(matchAll(/ true /, [1, true, false, true, 'true']).groups).toEqual( + [ + { value: true, index: 1 }, + { value: true, index: 3 }, + ], + ) }) it('should match simple expression', () => { @@ -87,5 +89,36 @@ describe('Basic tests', () => { { value: [{ num: 3 }], index: 5 }, ]) }) + + it('should match alternatives', () => { + expect( + matchAll(/ -2.01|true|\s\T /, [-2.01, true, false, 'foobar', '']) + .groups, + ).toEqual([ + { value: -2.01, index: 0 }, + { value: true, index: 1 }, + { value: 'foobar', index: 3 }, + ]) + + expect( + matchAll(/ [v 1|2|\s\T|3]+ /, [ + { v: 1 }, + { v: 3 }, + { v: 2 }, + null, + { v: 3 }, + { v: 2 }, + { v: 'wow' }, + { v: '' }, + 2, + 3, + { v: 1 }, + ]).groups, + ).toEqual([ + { value: [{ v: 1 }, { v: 3 }, { v: 2 }], index: 0 }, + { value: [{ v: 3 }, { v: 2 }, { v: 'wow' }], index: 4 }, + { value: [{ v: 1 }], index: 10 }, + ]) + }) }) }) -- cgit v1.3.1