From 6362218062a7b41e4aefe902ba5aed11aaa540c5 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 13 Jan 2022 14:40:45 +0530 Subject: feat: adds skip for multiple matches --- src/eval/index.ts | 14 ++++++++------ tests/basic.spec.ts | 24 +++++++++++------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/eval/index.ts b/src/eval/index.ts index 4a16b9b..3912031 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -33,9 +33,9 @@ const checkExpr = ( item: T, list: T[], index: number, - skip: (n: index) => (m: MatchGroupIndexed[]) => typeof m = _ => + skip: (n: index) => (m: MatchGroupIndexed[]) => MatchGroupIndexed[] = _ => identity, -): MatchGroupIndexed[] => { +): MatchGroupIndexed[] => { return pipe( expr, match[], Expr>({ @@ -51,25 +51,27 @@ const checkExpr = ( Group: ({ exprs }) => { const [head, ...tail] = exprs + const skipIndexes = [] as index[] + const localSkip = (i: index) => (x: any) => (skipIndexes.push(i), x) const matches = tail.reduce( (acc, exp) => pipe( acc, chain(ac => pipe( - checkExpr(exp, item, list, index), + checkExpr(exp, item, list, index, localSkip), zip(ac), z => z.map(([res, _cur]) => res), z => (z.length === 0 ? none : some(z)), ), ), ), - some(checkExpr(head, item, list, index)), + some(checkExpr(head, item, list, index, localSkip)), ) return pipe( matches, getOrElseW(() => []), - skip(1), + skip(Math.max(...skipIndexes) || 1), ) }, @@ -88,7 +90,7 @@ const checkExpr = ( list, takeLeftWhile(a => checkExpr(expr, a, list, index).length > 0), ) - return pipe(matches.length > 0 ? [group(matches, index)] : [], skip(1)) + return pipe(matches.length > 0 ? [group(matches, index)] : [], skip(matches.length || 1)) }, _: _ => [], diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index d57cb05..461a77c 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -1,9 +1,8 @@ import { jlog } from '../src/utils' import { filter, matchAll } from '../src' -import { takeLeftWhile } from 'fp-ts/Array' describe('Basic tests', () => { - it('should do it', () => { + it('should filter shit', () => { expect(filter(/\s/, [1, '2', 3, '4'])).toEqual(['2', '4']) expect(filter(/\n/, [1, '2', 3, '4'])).toEqual([1, 3]) expect(filter(/\T/, [1, 0, '4', ''])).toEqual([1, '4']) @@ -18,6 +17,16 @@ describe('Basic tests', () => { // jlog(matchAll(/([age \n])+/, [ {}, { age: 1 }, { age: 2 }, { age: 0 }, '' ])) // jlog(matchAll(/\n+/, [ '', 1, 2, 0, '6', 5, '' ])) // jlog(takeLeftWhile((x: number) => x < 5)([1, 2, 3, 4, 5, 6])) + + jlog( + matchAll(/([age \n][age \T])+/, [ + {}, + { age: 1 }, + { age: 2 }, + { age: 0 }, + '', + ]).groups, + ) }) describe('matchAll', () => { @@ -52,16 +61,6 @@ describe('Basic tests', () => { }) it('should match object property matchers', () => { - // FIXME: This is invalid match result - jlog( - matchAll(/([age \n][age \T])+/, [ - {}, - { age: 1 }, - { age: 2 }, - { age: 0 }, - '', - ]).groups, - ) expect( matchAll(/([age \n][age \T])+/, [ {}, @@ -72,7 +71,6 @@ describe('Basic tests', () => { ]).groups, ).toEqual([ { value: [{ age: 1 }, { age: 2 }], index: 1 }, - { value: [{ age: 2 }], index: 2 }, ]) }) }) -- cgit v1.3.1