aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-13 21:20:35 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-13 21:42:11 +0530
commitba94e799aa352c2fd472b694c745388e3e9feee8 (patch)
treee27c99304ed0268f6c9f8c438ae1d17dd269557a
parent016821207f1f2294dd09935a4c1d14d78ff3aed1 (diff)
downloadelxr-ba94e799aa352c2fd472b694c745388e3e9feee8.tar.gz
elxr-ba94e799aa352c2fd472b694c745388e3e9feee8.zip
feat(eval): implements or
Diffstat (limited to '')
-rw-r--r--src/eval/index.ts15
-rw-r--r--src/types.ts2
-rw-r--r--tests/basic.spec.ts49
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 = <T>(
)
},
+ 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 = <T>(
)
},
- // 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<Expr>()
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 },
+ ])
+ })
})
})