aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--src/eval/index.ts15
-rw-r--r--tests/basic.spec.ts22
3 files changed, 34 insertions, 4 deletions
diff --git a/README.md b/README.md
index 415b00a..9f9d718 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,7 @@ Whitespaces are ignore (except within literals)
* `a,b` => match `a` followed by `b` (next item)
* `[name \s\T]` => match property of object (example matches items with property `name` as non-empty string)
* `> n` | `>= n` | `< n` | `<= n` => Comparison with literal number [TODO]
+* `/pat/` => Test string values against regex [TODO]
diff --git a/src/eval/index.ts b/src/eval/index.ts
index f56ff92..8e162b4 100644
--- a/src/eval/index.ts
+++ b/src/eval/index.ts
@@ -20,6 +20,8 @@ const group = <T>(value: T, index: number): MatchGroupIndexed<T> => ({
type index = number
+const indexed = <T>(ls: T[]): Array<[number, T]> => ls.map((x, i) => [i, x])
+
const accumulateSkip = () => {
const skipIndexes = [] as index[]
return {
@@ -100,13 +102,22 @@ const checkExpr = <T>(
),
OneOrMore: ({ expr }) => {
+ const { localSkip, getSkips } = accumulateSkip()
+ const result = checkExpr(Expr.ZeroOrMore({ expr }), item, list, index, localSkip)
+ return pipe(
+ result[0].value.length > 0 ? result : [],
+ skip(getSkips().reduce((a, b) => a + b, 0)),
+ )
+ },
+
+ ZeroOrMore: ({ expr }) => {
// TODO: Nested quantified expressions?
const matches = pipe(
list,
takeLeftWhile(a => checkExpr(expr, a, list, index).length > 0),
)
return pipe(
- matches.length > 0 ? [group(matches, index)] : [],
+ [group(matches, index)],
skip(matches.length || 1),
)
},
@@ -115,8 +126,6 @@ const checkExpr = <T>(
const { getSkips, localSkip } = accumulateSkip()
const getGroups = () => {
if (exprs.length > list.length) return []
- const indexed = <T>(ls: T[]): Array<[number, T]> =>
- ls.map((x, i) => [i, x])
const result = pipe(
zipWith(exprs, indexed(list), (expr, [i, val]) =>
checkExpr(expr, val, list.slice(i), index + i, localSkip),
diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts
index dfa5e40..33a4228 100644
--- a/tests/basic.spec.ts
+++ b/tests/basic.spec.ts
@@ -1,7 +1,12 @@
+import {jlog} from '../src/utils'
import { matchAll } from '../src'
describe('Basic tests', () => {
describe('matchAll', () => {
+ it('should do shit', () => {
+ // jlog(matchAll(/\s+/, [1, '2', '', '3', 5, '6']))
+ })
+
it('should filter shit', () => {
expect(matchAll(/\s/, [1, '2', 3, '4']).groups).toEqual([
{ value: '2', index: 1 },
@@ -70,7 +75,7 @@ describe('Basic tests', () => {
])
})
- it('should match object property matchers', () => {
+ it('should match object property matchers multi', () => {
expect(
matchAll(/([age \n][age \T])+/, [
{},
@@ -82,6 +87,21 @@ describe('Basic tests', () => {
).toEqual([{ value: [{ age: 1 }, { age: 2 }], index: 1 }])
expect(
+ matchAll(/([age \n][age \T])*/, [
+ {},
+ { age: 1 },
+ { age: 2 },
+ { age: 0 },
+ '',
+ ]).groups,
+ ).toEqual([
+ { value: [], index: 0 },
+ { value: [{ age: 1 }, { age: 2 }], index: 1 },
+ { value: [], index: 3 },
+ { value: [], index: 4 },
+ ])
+
+ expect(
matchAll(/ [num \n\T]+ /, [
null,
{ num: 1 },