Skip to content

Commit 45cba26

Browse files
committed
docs: add more documentation about usage and transform setup
1 parent bdc320e commit 45cba26

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

README.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
- [ ] Instrumentation for accurately getting coverage on RegExps
2929
- [ ] Hybrid/partially-compiled RegExps for better dynamic support
3030

31-
## Usage
31+
## Setup
3232

3333
Install package:
3434

@@ -52,9 +52,51 @@ console.log(regExp)
5252
// /(?<=bar\/)foo\/test\.js/
5353
```
5454

55-
In order to statically transform magic-regexps at build time, you can use the included unplugin.
55+
## Usage
56+
57+
Every regular expression you create with the library should be wrapped in `createRegExp`.
58+
59+
> **Note**
60+
> By default, all helpers from `magic-regexp` assume that input that is passed should be escaped - so no special RegExp characters apply. So `createRegExp('foo?\d')` will not match `food3` but only `foo?\d` exactly.
61+
62+
There are a range of helpers that can be used to activate pattern matching, and they can be chained.
63+
64+
They are:
65+
66+
- `charIn`, `charNotIn` - this matches or doesn't match any character in the string provided.
67+
- `anyOf` - this takes an array of inputs and matches any of them.
68+
- `char`, `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` and `carriageReturn` - these are helpers for specific RegExp characters.
69+
- `not` - this can prefix `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`.
70+
- `maybe` - equivalent to `?` - this marks the input as optional.
71+
- `exactly` - this escapes a string input to match it exactly.
72+
73+
All of these helpers return an object of type `Input` that can be chained with the following helpers:
74+
75+
- `and` - this adds a new pattern to the current input.
76+
- `or` - this provides an alternative to the current input.
77+
- `after`, `before`, `notAfter` and `notBefore` - these activate positive/negative lookahead/lookbehinds. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari).
78+
- `times` - this is a function you can call directly to repeat the previous pattern an exact number of times, or you can use `times.between(min, max)` to specify a range.
79+
- `as` - this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()`.
80+
- `at` - this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()`.
81+
82+
## Compilation at build
83+
84+
The best way to use `magic-regexp` is by making use of the included transform.
85+
86+
```js
87+
const regExp = createRegExp(exactly('foo/test.js').after('bar/'))
88+
// => gets _compiled_ to
89+
const regExp = /(?<=bar\/)foo\/test\.js/
90+
```
91+
92+
Of course, this only works with non-dynamic regexps. Within the `createRegExp` block you have to include all the helpers you are using from `magic-regexp` - and not rely on any external variables. This, for example, will not statically compile into a RegExp, although it will still continue to work with a minimal runtime:
93+
94+
```js
95+
const someString = 'test'
96+
const regExp = createRegExp(exactly(someString))
97+
```
5698

57-
**Nuxt**:
99+
### Nuxt
58100

59101
```js
60102
import { defineNuxtConfig } from 'nuxt'
@@ -66,7 +108,7 @@ export default defineNuxtConfig({
66108
})
67109
```
68110

69-
**Vite**:
111+
### Vite
70112

71113
```js
72114
import { defineConfig } from 'vite'
@@ -77,7 +119,7 @@ export default defineConfig({
77119
})
78120
```
79121

80-
**unbuild**:
122+
### unbuild
81123

82124
```js
83125
import { defineBuildConfig } from 'unbuild'

0 commit comments

Comments
 (0)