diff options
| -rw-r--r-- | README.md | 18 | ||||
| -rw-r--r-- | src/eval/index.ts | 2 | ||||
| -rw-r--r-- | tests/basic.spec.ts | 25 |
3 files changed, 37 insertions, 8 deletions
@@ -5,25 +5,31 @@ Regular expression-like syntax for list operations. You didn't ask for this so h Example - ```js -filter(/ \T (\s | \n) /, [ null, 0, '2', 3, true ]) // > [ '2', 3 ] +filter(/ \T (\s | \n) /, [ null, 0, '2', 3, true ]) +// > [ '2', 3 ] -filter(/ \T[name \s\T] /, [ null, 0, { name: '' }, { name: 'phenax' } ]) // > [ { name: 'phenax' } ] +filter(/ \T[name \s\T] /, [ null, 0, { name: '' }, { name: 'phenax' } ]) +// > [ { name: 'phenax' } ] + +matchAll(/ [num \n\T]+ /, [ null, { num: 1 }, { num: 2 }, {}, { num: 0 }, { num: 3 } ]) +// > [ { value: [{ num: 1 }, { num: 2 }], index: 1 }, { value: [{ num: 3 }], index: 5 } ] ``` ### Syntax -Whitespaces are ignore (exceptions: within string literals and nested regex) +Whitespaces are ignore (except within literals) * `\s` => Any string * `\n` => Any number * `\b` => Any boolean * `\T` => Any truthy value * `\F` => Any falsey value -* `a|b` => match `a` **or** `b` +* `a|b` => match `a` **or** `b` [TODO] * `a*` => Zero or more consecutive instances of pattern `a` in the list * `a+` => One or more consecutive instances of pattern `a` in the list * `(\s\T)` => Group (example matches any non-empty string) -* `^a$` => `^` indicates start of list, and `$` indicates end of list -* `a,b` => match `a` followed by `b` (next item) +* `^a$` => `^` indicates start of list, and `$` indicates end of list [TODO] +* `a,b` => match `a` followed by `b` (next item) [TODO] * `[name \s\T]` => match property of object (example matches items with property `name` as non-empty string) +* `> n` | `>= n` | `< n` | `<= n` => Comparison [TODO] diff --git a/src/eval/index.ts b/src/eval/index.ts index 3912031..276f686 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -77,7 +77,7 @@ const checkExpr = <T>( PropertyMatch: ({ name, exprs }) => pipe( - Object.prototype.hasOwnProperty.call(item, name) + Object.prototype.hasOwnProperty.call(item ?? {}, name) ? checkExpr(Expr.Group({ exprs }), item[name], list, index) : [], res => (res.length > 0 ? [group(item, index)] : []), // FIXME: doesn't allow nested matching diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index 461a77c..febf77e 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -19,6 +19,17 @@ describe('Basic tests', () => { // 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 }, @@ -69,8 +80,20 @@ describe('Basic tests', () => { { age: 0 }, '', ]).groups, + ).toEqual([{ value: [{ age: 1 }, { age: 2 }], index: 1 }]) + + expect( + matchAll(/ [num \n\T]+ /, [ + null, + { num: 1 }, + { num: 2 }, + {}, + { num: 0 }, + { num: 3 }, + ]).groups, ).toEqual([ - { value: [{ age: 1 }, { age: 2 }], index: 1 }, + { value: [{ num: 1 }, { num: 2 }], index: 1 }, + { value: [{ num: 3 }], index: 5 }, ]) }) }) |
