diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-08 18:37:57 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-08 18:37:57 +0530 |
| commit | d19a1e4e02c6743f057d03caa9337a5b47bfa9cd (patch) | |
| tree | 44f4f27436d8fbc64a65e16bb122ae3775e22baf /src | |
| parent | e8322ca988fc20baf7892915e6ccb069b11c608e (diff) | |
| download | elxr-d19a1e4e02c6743f057d03caa9337a5b47bfa9cd.tar.gz elxr-d19a1e4e02c6743f057d03caa9337a5b47bfa9cd.zip | |
feat(eval): implements basic eval filtering
Diffstat (limited to 'src')
| -rw-r--r-- | src/eval/index.ts | 24 | ||||
| -rw-r--r-- | src/parser/index.ts | 4 | ||||
| -rw-r--r-- | src/types.ts | 7 | ||||
| -rw-r--r-- | src/utils.ts | 3 |
4 files changed, 36 insertions, 2 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts new file mode 100644 index 0000000..e88b583 --- /dev/null +++ b/src/eval/index.ts @@ -0,0 +1,24 @@ +import {pipe} from 'fp-ts/function'; +import { Expr, ListExpr } from '../types' +import {match} from '../utils'; + +export const find = <T>([startO, exprs, endO]: ListExpr, list: T[]): any => { + const check = (e: Expr) => <T>(x: T, _i: number, _l: T[]): boolean => { + return pipe( + e.tag as any, + match({ + AnyItem: () => true, + AnyNumber: () => typeof x === 'number', + AnyString: () => typeof x === 'string', + AnyBool: () => typeof x === 'boolean', + Truthy: () => !!x, + Falsey: () => !x, + _: () => false, + }) + ) + }; + + const cs = exprs.map(check) + + return list.filter((x, i, ls) => cs.every(c => c(x, i, ls))) +} diff --git a/src/parser/index.ts b/src/parser/index.ts index b8e6764..4ad3e7b 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -12,7 +12,7 @@ import { symbol, tuple3, } from './utils' -import { Expr } from '../types' +import { Expr, ListExpr } from '../types' export const start = mapTo(symbol('^'), _ => Expr.Start(null)) export const end = mapTo(symbol('$'), _ => Expr.End(null)) @@ -67,7 +67,7 @@ export const expressionP: Parser<Expr> = (input: string) => wrapAlt, ) -export const parser = tuple3(optional(start), many1(expressionP), optional(end)) +export const parser: Parser<ListExpr> = tuple3(optional(start), many1(expressionP), optional(end)) /* diff --git a/src/types.ts b/src/types.ts index 7f6546f..979745f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,4 @@ +import {Option} from "fp-ts/lib/Option" import { constructors, Union } from "./utils" type ExprT = { @@ -21,3 +22,9 @@ export type Expr = Union<ExprT> export const Expr = constructors<ExprT>() +export type ListExpr = [ + Option<Expr>, + Expr[], + Option<Expr>, +] + diff --git a/src/utils.ts b/src/utils.ts index 7e3009f..8e3abb9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -22,3 +22,6 @@ export const constructors = <T extends Record<string, any>>(): { }, }, ) as any + +export const jlog = (x: any) => console.log(JSON.stringify(x, null, 2)) + |
