diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-10 21:44:58 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-10 21:44:58 +0530 |
| commit | 724b7d23486f462a4310588419d02f4f072ef74f (patch) | |
| tree | b7685dc1be0fd2d4c5b22fb4e03cb212b69d4f09 | |
| parent | 20339806fdb2a0b396146e77ed1b4e568c244bbe (diff) | |
| download | elxr-724b7d23486f462a4310588419d02f4f072ef74f.tar.gz elxr-724b7d23486f462a4310588419d02f4f072ef74f.zip | |
fix: fixes property matching + test changes
Diffstat (limited to '')
| -rw-r--r-- | src/eval/index.ts | 9 | ||||
| -rw-r--r-- | tests/basic.spec.ts | 19 |
2 files changed, 22 insertions, 6 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts index 23832de..f1d6eae 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -39,7 +39,7 @@ export const matchAll = <T>( const next = (i: number = 1) => (curMatch: MatchGroupIndexed[]) => - [...curMatch, ...check(index + i, ls.slice(i), expr)] + [...curMatch, ...check(index + (i || 1), ls.slice(i), expr)] return pipe( expr, @@ -59,11 +59,11 @@ export const matchAll = <T>( (acc, exp) => pipe( acc, - chain(m => + chain(_m => pipe( check(index, [item], exp), res => res.length === 0 ? none : some(res), - map(ac => [...ac, ...m]), + map(ac => ac), // TODO: Doesn't seem right? ), ), ), @@ -78,6 +78,7 @@ export const matchAll = <T>( Object.prototype.hasOwnProperty.call(item, name) ? check(index, [item[name]], Expr.Group({ exprs })) : [], + res => res.length > 0 ? [group(item, index)] : [], // FIXME: doesn't allow nested matching next(), ), @@ -91,7 +92,7 @@ export const matchAll = <T>( //console.log(matches) return pipe( matches.length > 0 ? [group(matches, index)] : [], - next(matches.length || 1), + next(matches.length), ) }, diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index a9a3397..f1a2ced 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -16,8 +16,23 @@ describe('Basic tests', () => { // jlog(matchAll(/[age \n]+/, [ {}, { age: 1 }, '' ])) // jlog(matchAll(/([age \T][age \n])+/, [ {}, { age: 1 }, { age: 2 }, { age: 0 }, '' ])) - // jlog(matchAll(/\n/, [ 'b', 1, 2, 'a', 3 ])) - jlog(matchAll(/\n\T/, [1, 0, 2, '4', ''])) + expect(matchAll(/\n/, ['b', 1, 2, 'a', 3]).groups).toEqual([ + { value: 1, index: 1 }, + { value: 2, index: 2 }, + { value: 3, index: 4 }, + ]) + expect(matchAll(/\n\T/, [1, 0, 2, '4', '']).groups).toEqual([ + { value: 1, index: 0 }, + { value: 2, index: 2 }, + ]) + expect( + matchAll(/[age \n][age \T]/, [{}, { age: 1 }, { age: 2 }, { age: 0 }, '']) + .groups, + ).toEqual([ + { value: { age: 1 }, index: 1 }, + { value: { age: 2 }, index: 2 }, + ]) + // jlog(matchAll(/\n+/, [ '', 1, 2, 0, '', 5, '' ])) }) }) |
