|
| 1 | +# 🦄 magic-regexp |
| 2 | + |
| 3 | +[![npm version][npm-version-src]][npm-version-href] |
| 4 | +[![npm downloads][npm-downloads-src]][npm-downloads-href] |
| 5 | +[![Github Actions][github-actions-src]][github-actions-href] |
| 6 | +[![Codecov][codecov-src]][codecov-href] |
| 7 | + |
| 8 | +> A compiled-away, type-safe, readable RegExp alternative |
| 9 | +
|
| 10 | +- [✨ Changelog](https://github.com/danielroe/magic-regexp/blob/main/CHANGELOG.md) |
| 11 | +- [▶️ Online playground](https://stackblitz.com/github/danielroe/magic-regexp/tree/main/playground) |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +**⚠️ `magic-regexp` is currently a work in progress. ⚠️** |
| 16 | + |
| 17 | +- Runtime is zero-dependency and ultra-minimal |
| 18 | +- Ships with transform for compiling runtime to pure RegExp |
| 19 | +- Supports automatically typed capture groups |
| 20 | +- Packed with useful utilities: `charIn`, `charNotIn`, `anyOf`, `char`, `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed`, `carriageReturn`, `not`, `maybe`, `exactly` |
| 21 | +- All chainable with `and`, `or`, `after`, `before`, `notAfter`, `notBefore`, `times`, `as`, `at` |
| 22 | + |
| 23 | +**Future ideas** |
| 24 | + |
| 25 | +- [ ] More TypeScript guard-rails |
| 26 | +- [ ] More complex RegExp features/syntax |
| 27 | +- [ ] Instrumentation for accurately getting coverage on RegExps |
| 28 | +- [ ] Hybrid/partially-compiled RegExps for better dynamic support |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +Install package: |
| 33 | + |
| 34 | +```sh |
| 35 | +# npm |
| 36 | +npm install magic-regexp |
| 37 | + |
| 38 | +# yarn |
| 39 | +yarn add magic-regexp |
| 40 | + |
| 41 | +# pnpm |
| 42 | +pnpm install magic-regexp |
| 43 | +``` |
| 44 | + |
| 45 | +```js |
| 46 | +import { createRegExp, exactly } from 'magic-regexp' |
| 47 | + |
| 48 | +const regExp = createRegExp(exactly('foo/test.js').after('bar/')) |
| 49 | +console.log(regExp) |
| 50 | + |
| 51 | +// /(?<=bar\/)foo\/test\.js/ |
| 52 | +``` |
| 53 | + |
| 54 | +In order to statically transform magic-regexps at build time, you can use the included unplugin. |
| 55 | + |
| 56 | +**Nuxt**: |
| 57 | + |
| 58 | +```js |
| 59 | +import { defineNuxtConfig } from 'nuxt' |
| 60 | + |
| 61 | +// https://v3.nuxtjs.org/api/configuration/nuxt.config |
| 62 | +export default defineNuxtConfig({ |
| 63 | + // This will also enable auto-imports of magic-regexp helpers |
| 64 | + modules: ['magic-regexp/nuxt'], |
| 65 | +}) |
| 66 | +``` |
| 67 | + |
| 68 | +**Vite**: |
| 69 | + |
| 70 | +```js |
| 71 | +import { defineConfig } from 'vite' |
| 72 | +import { MagicRegExpTransformPlugin } from 'magic-regexp' |
| 73 | + |
| 74 | +export default defineConfig({ |
| 75 | + plugins: [MagicRegExpTransformPlugin.vite()], |
| 76 | +}) |
| 77 | +``` |
| 78 | + |
| 79 | +**unbuild**: |
| 80 | + |
| 81 | +```js |
| 82 | +import { defineBuildConfig } from 'unbuild' |
| 83 | +import { MagicRegExpTransformPlugin } from 'magic-regexp' |
| 84 | + |
| 85 | +export default defineBuildConfig({ |
| 86 | + hooks: { |
| 87 | + 'rollup:options': (options, config) => { |
| 88 | + config.plugins.push(MagicRegExpTransformPlugin.rollup()) |
| 89 | + }, |
| 90 | + }, |
| 91 | +}) |
| 92 | +``` |
| 93 | + |
| 94 | +## 💻 Development |
| 95 | + |
| 96 | +- Clone this repository |
| 97 | +- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10) |
| 98 | +- Install dependencies using `pnpm install` |
| 99 | +- Run interactive tests using `pnpm dev` |
| 100 | + |
| 101 | +## License |
| 102 | + |
| 103 | +Made with ❤️ |
| 104 | + |
| 105 | +Published under [MIT License](./LICENCE). |
| 106 | + |
| 107 | +<!-- Badges --> |
| 108 | + |
| 109 | +[npm-version-src]: https://img.shields.io/npm/v/magic-regexp?style=flat-square |
| 110 | +[npm-version-href]: https://npmjs.com/package/magic-regexp |
| 111 | +[npm-downloads-src]: https://img.shields.io/npm/dm/magic-regexp?style=flat-square |
| 112 | +[npm-downloads-href]: https://npmjs.com/package/magic-regexp |
| 113 | +[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/magic-regexp/ci/main?style=flat-square |
| 114 | +[github-actions-href]: https://github.com/unjs/magic-regexp/actions?query=workflow%3Aci |
| 115 | +[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/magic-regexp/main?style=flat-square |
| 116 | +[codecov-href]: https://codecov.io/gh/unjs/magic-regexp |
0 commit comments