From d19a1e4e02c6743f057d03caa9337a5b47bfa9cd Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 8 Jan 2022 18:37:57 +0530 Subject: feat(eval): implements basic eval filtering --- src/eval/index.ts | 24 +++++++++++++++ src/parser/index.ts | 4 +-- src/types.ts | 7 +++++ src/utils.ts | 3 ++ tests/basic.spec.ts | 85 ---------------------------------------------------- tests/eval.spec.ts | 20 +++++++++++++ tests/parser.spec.ts | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 87 deletions(-) create mode 100644 src/eval/index.ts delete mode 100644 tests/basic.spec.ts create mode 100644 tests/eval.spec.ts create mode 100644 tests/parser.spec.ts 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 = ([startO, exprs, endO]: ListExpr, list: T[]): any => { + const check = (e: Expr) => (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 = (input: string) => wrapAlt, ) -export const parser = tuple3(optional(start), many1(expressionP), optional(end)) +export const parser: Parser = 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 export const Expr = constructors() +export type ListExpr = [ + Option, + Expr[], + Option, +] + 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 = >(): { }, }, ) as any + +export const jlog = (x: any) => console.log(JSON.stringify(x, null, 2)) + diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts deleted file mode 100644 index 150966b..0000000 --- a/tests/basic.spec.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { left, right } from 'fp-ts/Either' -import { - integer, - whitespace, - whitespaces0, - delimited, -} from '../src/parser/utils' -import { parser } from '../src/parser' -import { none, some } from 'fp-ts/Option' -import { Expr } from '../src/types' - -const plog = (x: any) => console.log(JSON.stringify(x, null, 2)) - -describe('Foobar', () => { - it('should do shit', () => { - expect(integer('12901')).toEqual(right([12901, ''])) - expect(integer('12901asas')).toEqual(right([12901, 'asas'])) - expect(whitespace(' ')).toEqual(right([' ', ''])) - expect(whitespace('\n')).toEqual(right(['\n', ''])) - expect(whitespace('a')).toEqual(left(['unable to match', 'a'])) - - expect(delimited(whitespaces0, integer, whitespaces0)(' 20 ')).toEqual( - right([20, '']), - ) - expect(delimited(whitespaces0, integer, whitespaces0)(' 2 0 ')).toEqual( - right([2, '0 ']), - ) - }) - - it('should maybeshut', () => { - expect(parser(/^ .\s(\n)\b \T $/.source)).toEqual( - right([ - [ - some(Expr.Start(null)), - [ - Expr.AnyItem(null), - Expr.AnyString(null), - Expr.Group({ exprs: [Expr.AnyNumber(null)] }), - Expr.AnyBool(null), - Expr.Truthy(null), - ], - some(Expr.End(null)), - ], - '', - ]), - ) - - expect(parser(/^ \s* \T? \n+ $/.source)).toEqual( - right([ - [ - some(Expr.Start(null)), - [ - Expr.ZeroOrMore({ expr: Expr.AnyString(null) }), - Expr.Optional({ expr: Expr.Truthy(null) }), - Expr.OneOrMore({ expr: Expr.AnyNumber(null) }), - ], - some(Expr.End(null)), - ], - '', - ]), - ) - - expect(parser(/ \s|\b\T|\n /.source)).toEqual( - right([ - [ - none, - [ - Expr.Or({ - left: Expr.AnyString(null), - right: [ - Expr.AnyBool(null), - Expr.Or({ - left: Expr.Truthy(null), - right: [Expr.AnyNumber(null)], - }), - ], - }), - ], - none, - ], - '', - ]), - ) - }) -}) diff --git a/tests/eval.spec.ts b/tests/eval.spec.ts new file mode 100644 index 0000000..97995bf --- /dev/null +++ b/tests/eval.spec.ts @@ -0,0 +1,20 @@ +import { left, right } from 'fp-ts/Either' +import { none, some } from 'fp-ts/Option' +import { Expr, ListExpr } from '../src/types' +import { jlog } from '../src/utils' +import { find } from '../src/eval' + +describe('Eval', () => { + it('should do shit', () => { + const liexp: ListExpr = [ + none, + [ Expr.AnyNumber(null), Expr.Truthy(null) ], + none, + ] + + const list = [0, 1, '2', 3, [4], 5] + + expect(find(liexp, list)).toEqual([1, 3, 5]) + }) +}) + diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts new file mode 100644 index 0000000..150966b --- /dev/null +++ b/tests/parser.spec.ts @@ -0,0 +1,85 @@ +import { left, right } from 'fp-ts/Either' +import { + integer, + whitespace, + whitespaces0, + delimited, +} from '../src/parser/utils' +import { parser } from '../src/parser' +import { none, some } from 'fp-ts/Option' +import { Expr } from '../src/types' + +const plog = (x: any) => console.log(JSON.stringify(x, null, 2)) + +describe('Foobar', () => { + it('should do shit', () => { + expect(integer('12901')).toEqual(right([12901, ''])) + expect(integer('12901asas')).toEqual(right([12901, 'asas'])) + expect(whitespace(' ')).toEqual(right([' ', ''])) + expect(whitespace('\n')).toEqual(right(['\n', ''])) + expect(whitespace('a')).toEqual(left(['unable to match', 'a'])) + + expect(delimited(whitespaces0, integer, whitespaces0)(' 20 ')).toEqual( + right([20, '']), + ) + expect(delimited(whitespaces0, integer, whitespaces0)(' 2 0 ')).toEqual( + right([2, '0 ']), + ) + }) + + it('should maybeshut', () => { + expect(parser(/^ .\s(\n)\b \T $/.source)).toEqual( + right([ + [ + some(Expr.Start(null)), + [ + Expr.AnyItem(null), + Expr.AnyString(null), + Expr.Group({ exprs: [Expr.AnyNumber(null)] }), + Expr.AnyBool(null), + Expr.Truthy(null), + ], + some(Expr.End(null)), + ], + '', + ]), + ) + + expect(parser(/^ \s* \T? \n+ $/.source)).toEqual( + right([ + [ + some(Expr.Start(null)), + [ + Expr.ZeroOrMore({ expr: Expr.AnyString(null) }), + Expr.Optional({ expr: Expr.Truthy(null) }), + Expr.OneOrMore({ expr: Expr.AnyNumber(null) }), + ], + some(Expr.End(null)), + ], + '', + ]), + ) + + expect(parser(/ \s|\b\T|\n /.source)).toEqual( + right([ + [ + none, + [ + Expr.Or({ + left: Expr.AnyString(null), + right: [ + Expr.AnyBool(null), + Expr.Or({ + left: Expr.Truthy(null), + right: [Expr.AnyNumber(null)], + }), + ], + }), + ], + none, + ], + '', + ]), + ) + }) +}) -- cgit v1.3.1