summaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-08 21:17:10 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-08 21:17:10 +0530
commitb3b2c39df680f88e1e6d3615245754ae5f0380d0 (patch)
tree4af350ce393244d2c8dbcfdb0bd46cbb91ee41ef /src/eval
parente246f2341ee13926f4273c6d634cef4fe07f040e (diff)
downloadelxr-b3b2c39df680f88e1e6d3615245754ae5f0380d0.tar.gz
elxr-b3b2c39df680f88e1e6d3615245754ae5f0380d0.zip
feat(eval): implements object property matching
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/index.ts38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts
index f80c04d..b4dcf48 100644
--- a/src/eval/index.ts
+++ b/src/eval/index.ts
@@ -1,23 +1,27 @@
-import {pipe} from 'fp-ts/function';
+import { pipe } from 'fp-ts/function'
import { Expr, ListExpr } from '../types'
-import {match} from '../utils';
+import { match } from '../utils'
export const find = <T>([startO, exprs, endO]: ListExpr, list: T[]): any => {
- const check = (expr: Expr) => <T>(x: T, i: number, ls: T[]): boolean => {
- return pipe(
- expr,
- match<boolean, Expr>({
- AnyItem: _ => true,
- AnyNumber: _ => typeof x === 'number',
- AnyString: _ => typeof x === 'string',
- AnyBool: _ => typeof x === 'boolean',
- Truthy: _ => !!x,
- Falsey: _ => !x,
- Group: ({ exprs }) => exprs.every(e => check(e)(x, i, ls)),
- _: _ => false,
- })
- )
- };
+ const check =
+ (expr: Expr) =>
+ <T>(x: T, i: number, ls: T[]): boolean => {
+ return pipe(
+ expr,
+ match<boolean, Expr>({
+ AnyItem: _ => true,
+ AnyNumber: _ => typeof x === 'number',
+ AnyString: _ => typeof x === 'string',
+ AnyBool: _ => typeof x === 'boolean',
+ 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)),
+ _: _ => false,
+ }),
+ )
+ }
const cs = exprs.map(check)