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 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/eval/index.ts') 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)) }, _: _ => [], -- cgit v1.3.1