aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: eb2c9ee449fb9e55fec0067807d0a58254a9b6e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Elxr (List expressions)

Regular expression-like syntax for list operations. An experiment generalizing regex-like operations to a list.

[![forthebadge](https://forthebadge.com/images/badges/you-didnt-ask-for-this.svg)](https://forthebadge.com)
[![forthebadge](https://forthebadge.com/images/badges/kinda-sfw.svg)](https://forthebadge.com)
[![forthebadge](https://forthebadge.com/images/badges/0-percent-optimized.svg)](https://forthebadge.com)


### Syntax
Whitespaces are ignored (except within literals)

* `\s` => Any **string**
* `\n` => Any **number**
* `\b` => Any **boolean**
* `\o` => Any **object** (has to be a record) <sup>[TODO]</sup>
* `\a` => Any **array** <sup>[TODO]</sup>
* `\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 <sup>[TODO]</sup>
* `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 <sup>[TODO]</sup>
* `/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`)
* `(?<myMatch> \s\T)` => Named capture group (example matches `\s\T` pattern with the name `myMatch`) <sup>[TODO]</sup>
* `(?: \s\T)` => Non-capturing group (example checks for `\s\T` but doesn't return it as a match) <sup>[TODO]</sup>


### 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
// | Match for any number or any non-empty string or any object with `prop` is true
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' ]
```