aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-24 19:38:34 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-24 19:38:50 +0530
commitfcb7bd4a61bd8614652a94b842b6d261ff8403a7 (patch)
tree44a7a3233810d1d47cffd1be7485391f8f56da3f
parentdb497bb9950648c056c079c591b239ad4932b5c9 (diff)
downloadelxr-fcb7bd4a61bd8614652a94b842b6d261ff8403a7.tar.gz
elxr-fcb7bd4a61bd8614652a94b842b6d261ff8403a7.zip
feat(eval): adds min-max quantifier evaluation
-rw-r--r--src/eval/index.ts17
-rw-r--r--tests/basic.spec.ts41
2 files changed, 58 insertions, 0 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts
index ed757e7..96ccbef 100644
--- a/src/eval/index.ts
+++ b/src/eval/index.ts
@@ -108,6 +108,23 @@ const checkExpr = <T>(
)
},
+ MinMax: ({ expr, min, max }) => {
+ const { localSkip, getSkips } = accumulateSkip()
+ const result = checkExpr(Expr.ZeroOrMore({ expr }), item, list, index, localSkip)
+ // TODO: Use nested skips
+
+ const matches = result[0].value.length
+ const capturedMatchCount = matches < min ? 0 : Math.min(matches, max)
+ // const skipCount = getSkips().reduce((a, b) => a + b, 0)
+
+ return pipe(
+ result
+ .map(r => ({ ...r, value: r.value.slice(0, capturedMatchCount) }))
+ .filter(r => r.value.length > 0),
+ skip(capturedMatchCount || 1),
+ )
+ },
+
ZeroOrMore: ({ expr }) => {
// TODO: Nested quantified expressions?
const matches = pipe(
diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts
index 33a4228..db074d4 100644
--- a/tests/basic.spec.ts
+++ b/tests/basic.spec.ts
@@ -198,5 +198,46 @@ 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 },
+ { age: 0 },
+ '',
+ ]).groups,
+ ).toEqual([{ value: [{ age: 1 }, { age: 0 }], index: 1 }])
+
+ expect(
+ matchAll(/[age \n]{2, 4}/, [
+ {},
+ { age: 1 },
+ { age: 2 },
+ { age: 0 },
+ '',
+ ]).groups,
+ ).toEqual([{ value: [{ age: 1 }, { age: 2 }, { age: 0 }], index: 1 }])
+
+ expect(
+ matchAll(/[age \n]{2, 4}/, [
+ {},
+ { age: 1 },
+ { age: 2 },
+ { age: 0 },
+ { age: 8 },
+ { age: 4 },
+ '',
+ ]).groups,
+ ).toEqual([{ value: [{ age: 1 }, { age: 2 }, { age: 0 }, { age: 8 }], index: 1 }])
+ })
})
})