aboutsummaryrefslogtreecommitdiff
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
parent77b22abdbb9d2b451fe31d97fec2f3a6b9517c33 (diff)
downloadelxr-6362218062a7b41e4aefe902ba5aed11aaa540c5.tar.gz
elxr-6362218062a7b41e4aefe902ba5aed11aaa540c5.zip
feat: adds skip for multiple matches
-rw-r--r--src/eval/index.ts14
-rw-r--r--tests/basic.spec.ts24
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 = <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))
},
_: _ => [],
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 },
])
})
})