diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-13 14:00:23 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-13 14:00:23 +0530 |
| commit | 77b22abdbb9d2b451fe31d97fec2f3a6b9517c33 (patch) | |
| tree | 1070ad8efabce3c2b136e61fa000a91f9d97aa28 | |
| parent | 6b170757a811007db77a63952bc4afc0756b257a (diff) | |
| download | elxr-77b22abdbb9d2b451fe31d97fec2f3a6b9517c33.tar.gz elxr-77b22abdbb9d2b451fe31d97fec2f3a6b9517c33.zip | |
refactor: refactors in cps to skip items
Diffstat (limited to '')
| -rw-r--r-- | src/eval/index.ts | 42 | ||||
| -rw-r--r-- | tests/basic.spec.ts | 32 |
2 files changed, 43 insertions, 31 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts index 3ba688f..4a16b9b 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -1,4 +1,4 @@ -import { pipe } from 'fp-ts/function' +import { identity, pipe } from 'fp-ts/function' import { takeLeftWhile, zip } from 'fp-ts/lib/Array' import { chain, @@ -26,21 +26,28 @@ const group = <T>(value: T, index: number): MatchGroupIndexed<T> => ({ index, }) +type index = number + const checkExpr = <T>( expr: Expr, item: T, list: T[], index: number, + skip: (n: index) => (m: MatchGroupIndexed<T | T[]>[]) => typeof m = _ => + identity, ): MatchGroupIndexed<T>[] => { return pipe( expr, match<MatchGroupIndexed<any>[], Expr>({ - AnyItem: _ => [group(item, index)], - AnyNumber: _ => (typeof item === 'number' ? [group(item, index)] : []), - AnyString: _ => (typeof item === 'string' ? [group(item, index)] : []), - AnyBool: _ => (typeof item === 'boolean' ? [group(item, index)] : []), - Truthy: _ => (!!item ? [group(item, index)] : []), - Falsey: _ => (!item ? [group(item, index)] : []), + AnyItem: _ => pipe([group(item, index)], skip(1)), + AnyNumber: _ => + pipe(typeof item === 'number' ? [group(item, index)] : [], skip(1)), + AnyString: _ => + pipe(typeof item === 'string' ? [group(item, index)] : [], skip(1)), + AnyBool: _ => + pipe(typeof item === 'boolean' ? [group(item, index)] : [], skip(1)), + Truthy: _ => pipe(!!item ? [group(item, index)] : [], skip(1)), + Falsey: _ => pipe(!item ? [group(item, index)] : [], skip(1)), Group: ({ exprs }) => { const [head, ...tail] = exprs @@ -62,6 +69,7 @@ const checkExpr = <T>( return pipe( matches, getOrElseW(() => []), + skip(1), ) }, @@ -71,6 +79,7 @@ const checkExpr = <T>( ? checkExpr(Expr.Group({ exprs }), item[name], list, index) : [], res => (res.length > 0 ? [group(item, index)] : []), // FIXME: doesn't allow nested matching + skip(1), ), OneOrMore: ({ expr }) => { @@ -79,7 +88,7 @@ const checkExpr = <T>( list, takeLeftWhile(a => checkExpr(expr, a, list, index).length > 0), ) - return matches.length > 0 ? [group(matches, index)] : [] + return pipe(matches.length > 0 ? [group(matches, index)] : [], skip(1)) }, _: _ => [], @@ -87,7 +96,6 @@ const checkExpr = <T>( ) } -// :: ListExpr -> [a] -> [{ groups: [T] }] export const matchAll = <T>( [startO, exprs, endO]: ListExpr, list: T[], @@ -95,26 +103,14 @@ export const matchAll = <T>( const check = (index: number, ls: T[], expr: Expr): MatchGroupIndexed[] => { if (ls.length === 0) return [] - const [item, ...rest] = ls + const [item] = ls const next = (i: number = 1) => (curMatch: MatchGroupIndexed[]) => [...curMatch, ...check(index + i, ls.slice(i), expr)] - return pipe( - expr, - match<MatchGroupIndexed<any>[], Expr>({ - OneOrMore: ({ expr }) => { - return pipe( - checkExpr(expr, item, ls, index), - matches => next(1)(matches), // matches.length || - ) - }, - - _: _ => pipe(checkExpr(expr, item, ls, index), next()), - }), - ) + return checkExpr(expr, item, ls, index, next) } const expr = Expr.Group({ exprs }) diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index 2817a8f..d57cb05 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -1,6 +1,6 @@ import { jlog } from '../src/utils' import { filter, matchAll } from '../src' -import {takeLeftWhile} from 'fp-ts/Array' +import { takeLeftWhile } from 'fp-ts/Array' describe('Basic tests', () => { it('should do it', () => { @@ -16,9 +16,7 @@ describe('Basic tests', () => { // jlog(matchAll(/[age \n]+/, [ {}, { age: 1 }, { age: 2 }, { age: 0 }, '' ])) // jlog(matchAll(/[age \n]+/, [ {}, { age: 1 }, '' ])) // jlog(matchAll(/([age \n])+/, [ {}, { age: 1 }, { age: 2 }, { age: 0 }, '' ])) - // jlog(matchAll(/\n+/, [ '', 1, 2, 0, '6', 5, '' ])) - // jlog(takeLeftWhile((x: number) => x < 5)([1, 2, 3, 4, 5, 6])) }) @@ -40,8 +38,13 @@ describe('Basic tests', () => { it('should match object property matchers', () => { expect( - matchAll(/[age \n][age \T]/, [{}, { age: 1 }, { age: 2 }, { age: 0 }, '']) - .groups, + matchAll(/[age \n][age \T]/, [ + {}, + { age: 1 }, + { age: 2 }, + { age: 0 }, + '', + ]).groups, ).toEqual([ { value: { age: 1 }, index: 1 }, { value: { age: 2 }, index: 2 }, @@ -50,14 +53,27 @@ describe('Basic tests', () => { it('should match object property matchers', () => { // FIXME: This is invalid match result + jlog( + matchAll(/([age \n][age \T])+/, [ + {}, + { age: 1 }, + { age: 2 }, + { age: 0 }, + '', + ]).groups, + ) expect( - matchAll(/([age \n][age \T])+/, [{}, { age: 1 }, { age: 2 }, { age: 0 }, '']) - .groups, + matchAll(/([age \n][age \T])+/, [ + {}, + { age: 1 }, + { age: 2 }, + { age: 0 }, + '', + ]).groups, ).toEqual([ { value: [{ age: 1 }, { age: 2 }], index: 1 }, { value: [{ age: 2 }], index: 2 }, ]) }) - }) }) |
