Package Exports
- magic-regexp
- magic-regexp/nuxt
- magic-regexp/transform
Readme
🦄 magic-regexp
A compiled-away, type-safe, readable RegExp alternative
Features
⚠️ magic-regexp
is currently a work in progress. ⚠️
- Runtime is zero-dependency and ultra-minimal
- Ships with transform for compiling runtime to pure RegExp
- Supports automatically typed capture groups
- Packed with useful utilities:
charIn
,charNotIn
,anyOf
,char
,word
,digit
,whitespace
,letter
,tab
,linefeed
,carriageReturn
,not
,maybe
,exactly
- All chainable with
and
,or
,after
,before
,notAfter
,notBefore
,times
,as
,at
Future ideas
- More TypeScript guard-rails
- More complex RegExp features/syntax
- Instrumentation for accurately getting coverage on RegExps
- Hybrid/partially-compiled RegExps for better dynamic support
Usage
Install package:
# npm
npm install magic-regexp
# yarn
yarn add magic-regexp
# pnpm
pnpm install magic-regexp
import { createRegExp, exactly } from 'magic-regexp'
const regExp = createRegExp(exactly('foo/test.js').after('bar/'))
console.log(regExp)
// /(?<=bar\/)foo\/test\.js/
In order to statically transform magic-regexps at build time, you can use the included unplugin.
Nuxt:
import { defineNuxtConfig } from 'nuxt'
// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
// This will also enable auto-imports of magic-regexp helpers
modules: ['magic-regexp/nuxt'],
})
Vite:
import { defineConfig } from 'vite'
import { MagicRegExpTransformPlugin } from 'magic-regexp'
export default defineConfig({
plugins: [MagicRegExpTransformPlugin.vite()],
})
unbuild:
import { defineBuildConfig } from 'unbuild'
import { MagicRegExpTransformPlugin } from 'magic-regexp'
export default defineBuildConfig({
hooks: {
'rollup:options': (options, config) => {
config.plugins.push(MagicRegExpTransformPlugin.rollup())
},
},
})
💻 Development
- Clone this repository
- Enable Corepack using
corepack enable
(usenpm i -g corepack
for Node.js < 16.10) - Install dependencies using
pnpm install
- Run interactive tests using
pnpm dev
License
Made with ❤️
Published under MIT License.