diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-13 17:54:27 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-13 17:54:35 +0530 |
| commit | 1a281a717fe5bab2694bf51c1b03a7569d8facb4 (patch) | |
| tree | ea0ff4e5ee4d579aed4750d53503b33aab5cb818 | |
| parent | fc548093f151355842122912c582fc10a2d5acb2 (diff) | |
| download | elxr-1a281a717fe5bab2694bf51c1b03a7569d8facb4.tar.gz elxr-1a281a717fe5bab2694bf51c1b03a7569d8facb4.zip | |
feat(eval): adds literal equality check
| -rw-r--r-- | src/eval/index.ts | 16 | ||||
| -rw-r--r-- | tests/basic.spec.ts | 33 |
2 files changed, 25 insertions, 24 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts index 276f686..075ff81 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -9,7 +9,7 @@ import { Option, some, } from 'fp-ts/lib/Option' -import { Expr, ListExpr } from '../types' +import { Expr, ListExpr, Literal } from '../types' import { match } from '../utils' export interface MatchGroupIndexed<T = any> { @@ -33,7 +33,9 @@ const checkExpr = <T>( item: T, list: T[], index: number, - skip: (n: index) => (m: MatchGroupIndexed<T | T[]>[]) => MatchGroupIndexed<T | T[]>[] = _ => + skip: ( + n: index, + ) => (m: MatchGroupIndexed<T | T[]>[]) => MatchGroupIndexed<T | T[]>[] = _ => identity, ): MatchGroupIndexed<T | T[]>[] => { return pipe( @@ -48,6 +50,11 @@ const checkExpr = <T>( 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)), + Literal: literal => + pipe( + (literal.value as any) === item ? [group(item, index)] : [], + skip(1), + ), Group: ({ exprs }) => { const [head, ...tail] = exprs @@ -90,7 +97,10 @@ const checkExpr = <T>( list, takeLeftWhile(a => checkExpr(expr, a, list, index).length > 0), ) - return pipe(matches.length > 0 ? [group(matches, index)] : [], skip(matches.length || 1)) + return pipe( + matches.length > 0 ? [group(matches, index)] : [], + skip(matches.length || 1), + ) }, _: _ => [], diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index febf77e..2d7af37 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -16,31 +16,22 @@ describe('Basic tests', () => { // 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])) - jlog( - matchAll(/ [num \n\T]+ /, [ - null, - { num: 1 }, - { num: 2 }, - {}, - { num: 0 }, - { num: 3 }, - ]), - ) - - jlog( - matchAll(/([age \n][age \T])+/, [ - {}, - { age: 1 }, - { age: 2 }, - { age: 0 }, - '', - ]).groups, - ) + //jlog(matchAll(/ -2.05 /, [ 2, -2.05, 5, -2.05, 2.05, 0.05, 'wow', '-2.05', '-2' ])) }) describe('matchAll', () => { + it ('should match literals', () => { + expect(matchAll(/ -2.05 /, [ 2, NaN, -2.05, '-2.05', -2.05, 2.05 ]).groups).toEqual([ + { value: -2.05, index: 2 }, + { value: -2.05, index: 4 }, + ]) + expect(matchAll(/ true /, [ 1, true, false, true, 'true' ]).groups).toEqual([ + { value: true, index: 1 }, + { value: true, index: 3 }, + ]) + }) + it('should match simple expression', () => { expect(matchAll(/\n/, ['b', 1, 2, 'a', 3]).groups).toEqual([ { value: 1, index: 1 }, |
