aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/eval/index.ts16
-rw-r--r--tests/basic.spec.ts58
2 files changed, 44 insertions, 30 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts
index 96ccbef..f8d14d3 100644
--- a/src/eval/index.ts
+++ b/src/eval/index.ts
@@ -1,7 +1,7 @@
import { identity, pipe } from 'fp-ts/function'
import { filter, takeLeftWhile, zip, zipWith } from 'fp-ts/Array'
import * as Option from 'fp-ts/Option'
-import { index, Expr, ListExpr } from '../types'
+import { index, Expr, ListExpr, Literal } from '../types'
import { match } from '../utils'
export interface MatchGroupIndexed<T = any> {
@@ -52,11 +52,15 @@ const checkExpr = <T>(
pipe(typeof item === 'boolean' ? [group(item, index)] : [], skip(1)),
Truthy: _ => pipe(!!item ? [group(item, index)] : [], skip(1)),
Falsey: _ => pipe(!item ? [group(item, index)] : [], skip(1)),
- Literal: literal =>
- pipe(
- (literal.value as any) === item ? [group(item, index)] : [],
- skip(1),
- ),
+ Literal: literal => pipe(
+ literal,
+ match<boolean, Literal>({
+ RegExp: regex => regex && typeof item === 'string' && regex.test(item),
+ _: () => (literal.value as any) === item,
+ }),
+ passed => passed ? [group(item, index)] : [],
+ skip(1),
+ ),
Group: ({ exprs }) => {
const [head, ...tail] = exprs
diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts
index 86fdb7c..f02169c 100644
--- a/tests/basic.spec.ts
+++ b/tests/basic.spec.ts
@@ -1,4 +1,4 @@
-import {jlog} from '../src/utils'
+import { jlog } from '../src/utils'
import { matchAll } from '../src'
describe('Basic tests', () => {
@@ -43,6 +43,28 @@ describe('Basic tests', () => {
{ value: true, index: 3 },
],
)
+
+ expect(
+ matchAll(/ [name "foobar"] /, [
+ {},
+ { name: 'foobar' },
+ { name: 'fuck' },
+ ]).groups,
+ ).toEqual([{ value: { name: 'foobar' }, index: 1 }])
+
+ expect(
+ matchAll(/ [name "test" | \/foo(bar|baz)\/] /, [
+ { name: 'test' },
+ { name: 'foobar' },
+ { name: 'foo' },
+ { name: 'foobaz' },
+ {},
+ ]).groups,
+ ).toEqual([
+ { value: { name: 'test' }, index: 0 },
+ { value: { name: 'foobar' }, index: 1 },
+ { value: { name: 'foobaz' }, index: 3 },
+ ])
})
it('should match simple expression', () => {
@@ -200,31 +222,17 @@ describe('Basic tests', () => {
})
it('should match min-max quantified expressions', () => {
- expect(
- matchAll(/[age \n]{2, 4}/, [
- {},
- { age: 1 },
- '',
- ]).groups,
- ).toEqual([])
+ expect(matchAll(/[age \n]{2, 4}/, [{}, { age: 1 }, '']).groups).toEqual(
+ [],
+ )
expect(
- matchAll(/[age \n]{2, 4}/, [
- {},
- { age: 1 },
- { age: 0 },
- '',
- ]).groups,
+ matchAll(/[age \n]{2, 4}/, [{}, { age: 1 }, { age: 0 }, '']).groups,
).toEqual([{ value: [{ age: 1 }, { age: 0 }], index: 1 }])
expect(
- matchAll(/[age \n]{2, 4}/, [
- {},
- { age: 1 },
- { age: 2 },
- { age: 0 },
- '',
- ]).groups,
+ matchAll(/[age \n]{2, 4}/, [{}, { age: 1 }, { age: 2 }, { age: 0 }, ''])
+ .groups,
).toEqual([{ value: [{ age: 1 }, { age: 2 }, { age: 0 }], index: 1 }])
expect(
@@ -237,7 +245,9 @@ describe('Basic tests', () => {
{ age: 4 },
'',
]).groups,
- ).toEqual([{ value: [{ age: 1 }, { age: 2 }, { age: 0 }, { age: 8 }], index: 1 }])
+ ).toEqual([
+ { value: [{ age: 1 }, { age: 2 }, { age: 0 }, { age: 8 }], index: 1 },
+ ])
expect(
matchAll(/[age \n]{2, 4}/, [
@@ -251,8 +261,8 @@ describe('Basic tests', () => {
'',
]).groups,
).toEqual([
- { value: [{ age: 1 }, { age: 2 }, { age: 0 }, { age: 8 }], index: 1 },
- { value: [{ age: 4 }, { age: 9 }], index: 5 },
+ { value: [{ age: 1 }, { age: 2 }, { age: 0 }, { age: 8 }], index: 1 },
+ { value: [{ age: 4 }, { age: 9 }], index: 5 },
])
})
})