aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-13 17:58:36 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-13 17:59:03 +0530
commite26086fef3cf142cffd145836a4c7c12cfeb9656 (patch)
tree3db5cbe208ac30b9281599fa61dbe6c910fe4163
parent1a281a717fe5bab2694bf51c1b03a7569d8facb4 (diff)
downloadelxr-e26086fef3cf142cffd145836a4c7c12cfeb9656.tar.gz
elxr-e26086fef3cf142cffd145836a4c7c12cfeb9656.zip
refactor: propertymatch changed to use single expression (group)
-rw-r--r--TODO.md4
-rw-r--r--src/eval/index.ts8
-rw-r--r--src/parser/index.ts2
-rw-r--r--src/types.ts2
-rw-r--r--tests/eval.spec.ts19
-rw-r--r--tests/parser.spec.ts4
6 files changed, 15 insertions, 24 deletions
diff --git a/TODO.md b/TODO.md
index 51850a3..c0aeca0 100644
--- a/TODO.md
+++ b/TODO.md
@@ -6,12 +6,8 @@
- [ ] Parse: `{2, 5}` quantifiers
- [ ] Eval: `^$` start-end
- [ ] Func: filter
- - [ ] Func: matchAll
- [ ] Func: replace
- [ ] Not/Inverse
- [ ] Named capture groups
- [ ] Non-capture groups
-## Refactor
- - [ ] Create group inside PropertyMatch and send single `expr`
-
diff --git a/src/eval/index.ts b/src/eval/index.ts
index 075ff81..0579579 100644
--- a/src/eval/index.ts
+++ b/src/eval/index.ts
@@ -82,10 +82,10 @@ const checkExpr = <T>(
)
},
- PropertyMatch: ({ name, exprs }) =>
+ PropertyMatch: ({ name, expr }) =>
pipe(
Object.prototype.hasOwnProperty.call(item ?? {}, name)
- ? checkExpr(Expr.Group({ exprs }), item[name], list, index)
+ ? checkExpr(expr, item[name], list, index)
: [],
res => (res.length > 0 ? [group(item, index)] : []), // FIXME: doesn't allow nested matching
skip(1),
@@ -146,8 +146,8 @@ export const find = <T>([startO, exprs, endO]: ListExpr, list: T[]): any => {
Truthy: _ => !!x,
Falsey: _ => !x,
Group: ({ exprs }) => exprs.every(e => check(e)(x, i, ls)),
- PropertyMatch: ({ name, exprs }) =>
- name in x && exprs.every(e => check(e)(x[name], i, ls)),
+ PropertyMatch: ({ name, expr }) =>
+ name in x && check(expr)(x[name], i, ls),
OneOrMore: ({ expr }) => {
// TODO: Nested quantified expression
const x = pipe(
diff --git a/src/parser/index.ts b/src/parser/index.ts
index 3943848..e815334 100644
--- a/src/parser/index.ts
+++ b/src/parser/index.ts
@@ -76,7 +76,7 @@ const objectProperty = (input: string) =>
pair(propertyName, many0(expressionP)),
symbol(']'),
),
- ([name, exprs]) => Expr.PropertyMatch({ name, exprs }),
+ ([name, exprs]) => Expr.PropertyMatch({ name, expr: Expr.Group({ exprs }) }),
),
)
diff --git a/src/types.ts b/src/types.ts
index ac577a1..b09bde5 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -25,7 +25,7 @@ export type Expr = Union<{
Truthy: _,
Falsey: _,
Group: { exprs: Expr[] },
- PropertyMatch: { name: string, exprs: Expr[] },
+ PropertyMatch: { name: string, expr: Expr },
Literal: Literal,
}>
export const Expr = constructors<Expr>()
diff --git a/tests/eval.spec.ts b/tests/eval.spec.ts
index 2d7e1c0..f4999b6 100644
--- a/tests/eval.spec.ts
+++ b/tests/eval.spec.ts
@@ -26,18 +26,10 @@ describe('Eval', () => {
it('basic evaluation', () => {
const list = [0, 1, '2', 3, [4], 5]
- const liexp: ListExpr = [
- none,
- [Expr.AnyNumber(), Expr.Truthy()],
- none,
- ]
+ const liexp: ListExpr = [none, [Expr.AnyNumber(), Expr.Truthy()], none]
expect(find(liexp, list)).toEqual([1, 3, 5])
- const liexp2: ListExpr = [
- none,
- [Expr.Falsey(), Expr.Truthy()],
- none,
- ]
+ const liexp2: ListExpr = [none, [Expr.Falsey(), Expr.Truthy()], none]
expect(find(liexp2, list)).toEqual([])
})
@@ -69,9 +61,12 @@ describe('Eval', () => {
[
Expr.PropertyMatch({
name: 'name',
- exprs: [Expr.AnyString(), Expr.Truthy()],
+ expr: Expr.Group({ exprs: [Expr.AnyString(), Expr.Truthy()] }),
+ }),
+ Expr.PropertyMatch({
+ name: 'age',
+ expr: Expr.Group({ exprs: [Expr.AnyNumber()] }),
}),
- Expr.PropertyMatch({ name: 'age', exprs: [Expr.AnyNumber()] }),
],
none,
]
diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts
index c935165..bd71509 100644
--- a/tests/parser.spec.ts
+++ b/tests/parser.spec.ts
@@ -89,9 +89,9 @@ describe('Parser', () => {
wrap([
Expr.PropertyMatch({
name: 'name',
- exprs: [Expr.AnyString(), Expr.Truthy()],
+ expr: Expr.Group({ exprs: [Expr.AnyString(), Expr.Truthy()] }),
}),
- Expr.PropertyMatch({ name: 'age', exprs: [Expr.AnyNumber()] }),
+ Expr.PropertyMatch({ name: 'age', expr: Expr.Group({ exprs: [Expr.AnyNumber()] }) }),
]),
)
})