https://github.com/phenax/elxr
Regular expression-like syntax for list operations
https://github.com/phenax/elxr
Last synced: about 2 months ago
JSON representation
Regular expression-like syntax for list operations
- Host: GitHub
- URL: https://github.com/phenax/elxr
- Owner: phenax
- License: mit
- Created: 2022-01-14T15:55:18.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-17T16:44:41.000Z (over 3 years ago)
- Last Synced: 2024-10-10T22:39:29.971Z (8 months ago)
- Language: TypeScript
- Size: 154 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Elxr (List expressions)
Regular expression-like syntax for list operations. An experiment generalizing regex-like operations to a list.
[](https://www.npmjs.com/package/elxr)
## Install
To install the latest stable version of elxr -```bash
yarn add elxr
// OR
npm install --save elxr
```## Syntax
Whitespaces are ignored (except within literals)* `\s` => Any **string**
* `\n` => Any **number**
* `\b` => Any **boolean**
* `\o` => Any **object** (has to be a record) [TODO]
* `\a` => Any **array** [TODO]
* `\T` => Any **truthy** value
* `\F` => Any **falsey** value
* `a|b` => match `a` **or** `b`
* `a*` => **Zero or more** consecutive instances of pattern `a` in the list
* `a+` => **One or more** consecutive instances of pattern `a` in the list
* `a{2, 5}` => **Min-Max** quantifiers (example matches `a` more than 2 times but less than 5)
* `(\s\T)` => **Group** (example matches any non-empty string)
* `^a$` => `^` indicates **start** of list, and `$` indicates **end** of list [TODO]
* `a,b` => match `a` on current item followed by `b` on the next item (**sequence**)
* `[name \s\T]` => match **property** of object (example matches items with property `name` as non-empty string)
* `> n` | `>= n` | `< n` | `<= n` => **Comparison** with literal number [TODO]
* `/pat/` => Test string values against **regex**
* `"foobar"` => String literal (example matches the string `foobar`)
* `-2.05` => Number literal (example matches the number `-2.05`)
* `true` => Boolean literal (example matches the value `true`)
* `(? \s\T)` => Named capture group (example matches `\s\T` pattern with the name `myMatch`) [TODO]
* `(?: \s\T)` => Non-capturing group (example checks for `\s\T` but doesn't return it as a match) [TODO]## Examples
#### matchAll
```js
// | Match for any number or any non-empty string or any object with `prop` is true
matchAll(/ \n | \s\T /, [null, 23, '', 'wow', false ]// > {
// groups: [
// { index: 1, value: 23 }, // \n
// { index: 3, value: 'wow' }, // \s\T
// ]
// }
``````js
// | Match for property `seperator` true, followed by one or more list of id's that are non-empty strings
matchAll(/ [seperator true], [id \s\T]+ /, [
{ seperator: true },
{ id: '1' },
{ id: '2' },
{ id: '3' },
{ seperator: true },
{ id: '4' },
{ id: '5' },
{ id: '6' },
])// > {
// groups: [
// {
// index: 0,
// value: [
// [{ value: { seperator: true }, index: 0 }],
// [{ value: [{ id: '1' }, { id: '2' }, { id: '3' }], index: 1 }],
// ],
// },
// {
// index: 4,
// value: [
// [{ value: { seperator: true }, index: 4 }],
// [{ value: [{ id: '4' }, { id: '5' }, { id: '6' }], index: 5 }],
// ],
// },
// ]
// }
```#### replaceAll
```js
// | Replace any sequence of one or more numbers by their sum
const replacer = (_, matches) => matches.value.reduce((a, b) => a + b, 0)
replaceAll(/ \n+ /, replacer, [ 'start', 3, 5, 'mid', 2, 0, 4, 'end' ])// > [ 'start', 8, 'mid', 6, 'end' ]
```## License
Elxr is licensed under [MIT](./LICENSE)