summaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-13 14:40:45 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-13 14:40:45 +0530
commit6362218062a7b41e4aefe902ba5aed11aaa540c5 (patch)
tree2271ffc3bca7d1b68998ad54bdf9ba3e2ee0d001 /src/eval
parent77b22abdbb9d2b451fe31d97fec2f3a6b9517c33 (diff)
downloadelxr-6362218062a7b41e4aefe902ba5aed11aaa540c5.tar.gz
elxr-6362218062a7b41e4aefe902ba5aed11aaa540c5.zip
feat: adds skip for multiple matches
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/index.ts14
1 files changed, 8 insertions, 6 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 = <T>(
item: T,
list: T[],
index: number,
- skip: (n: index) => (m: MatchGroupIndexed<T | T[]>[]) => typeof m = _ =>
+ skip: (n: index) => (m: MatchGroupIndexed<T | T[]>[]) => MatchGroupIndexed<T | T[]>[] = _ =>
identity,
-): MatchGroupIndexed<T>[] => {
+): MatchGroupIndexed<T | T[]>[] => {
return pipe(
expr,
match<MatchGroupIndexed<any>[], Expr>({
@@ -51,25 +51,27 @@ const checkExpr = <T>(
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 = <T>(
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))
},
_: _ => [],