← Back to archive

Repo of the Day

unjs/magic-regexp

Published: Feb 6, 2023

Open repository ↗

Summary

magic-regexp is a TypeScript library that offers a readable, natural-language style for writing regular expressions. The project is designed to be compiled away, so builder calls are replaced with native RegExp objects in the final bundle, and it automatically infers the type of each capture group. The runtime is described in the README as zero-dependency and ultra-minimal, and the package is published under the MIT license.

What it is useful for

  • Writing regex in TypeScript with chained, English-like helpers instead of dense literals. The README highlights "natural language syntax" and notes that the generated RegExp is shown on hover in the editor.
  • Producing code where capture groups are typed automatically, so consumers of .match() and .exec() results receive specific types from TypeScript instead of plain string | undefined.
  • Keeping the production runtime small: because the builder calls are compiled away to plain RegExp, the runtime cost in shipped code is minimal.
  • Adopting the library in commercial or open-source projects without licensing friction; the README links to an MIT License file.
  • A caveat from the source: the README itself does not include copy-pasteable code samples, and the list of bundlers supported by the compile-away transform is not documented in the README, so readers should check regexp.dev before integrating.

How engineers can use it

  1. Install the magic-regexp package in a TypeScript project. The README does not include a verbatim install command, so the exact command and current version should be confirmed via the npm registry or the docs at regexp.dev.
  2. Import the builder, chain methods to compose a pattern, and pass the result where a RegExp is expected. The README links to regexp.dev and a TypeScript playground for the full API and live examples.
  3. Configure the build pipeline to use the project's transform, which the README describes as a way to "compile to pure RegExp." Whether this transform plugs into a specific bundler is not detailed in the README, so verify with your build tooling.
  4. To work on the project locally, the README documents these steps:
    • Clone the repository.
    • Run corepack enable (or npm i -g corepack on Node.js < 16.10).
    • Install dependencies with pnpm install.
    • Run the interactive tests with pnpm dev.