From 724b7d23486f462a4310588419d02f4f072ef74f Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 10 Jan 2022 21:44:58 +0530 Subject: fix: fixes property matching + test changes --- src/eval/index.ts | 9 +++++---- 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 = ( 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 = ( (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 = ( 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 = ( //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, '' ])) }) }) -- cgit v1.3.1