Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magic-regexp

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-regexp - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

7

package.json
{
"name": "magic-regexp",
"version": "0.0.1",
"version": "0.0.2",
"description": "",

@@ -38,2 +38,3 @@ "repository": "danielroe/magic-regexp",

"magic-string": "^0.26.2",
"mlly": "^0.5.4",
"ufo": "^0.8.5",

@@ -67,6 +68,6 @@ "unplugin": "^0.7.2"

},
"packageManager": "pnpm@7.5.0",
"packageManager": "pnpm@7.5.1",
"volta": {
"node": "16.15.1"
"node": "16.16.0"
}
}

@@ -7,2 +7,3 @@ # 🦄 magic-regexp

[![Codecov][codecov-src]][codecov-href]
[![LGTM][lgtm-src]][lgtm-href]

@@ -31,3 +32,3 @@ > A compiled-away, type-safe, readable RegExp alternative

## Usage
## Setup

@@ -56,7 +57,49 @@ Install package:

In order to statically transform magic-regexps at build time, you can use the included unplugin.
## Usage
**Nuxt**:
Every regular expression you create with the library should be wrapped in `createRegExp`.
> **Note**
> 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.
There are a range of helpers that can be used to activate pattern matching, and they can be chained.
They are:
- `charIn`, `charNotIn` - this matches or doesn't match any character in the string provided.
- `anyOf` - this takes an array of inputs and matches any of them.
- `char`, `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` and `carriageReturn` - these are helpers for specific RegExp characters.
- `not` - this can prefix `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`.
- `maybe` - equivalent to `?` - this marks the input as optional.
- `exactly` - this escapes a string input to match it exactly.
All of these helpers return an object of type `Input` that can be chained with the following helpers:
- `and` - this adds a new pattern to the current input.
- `or` - this provides an alternative to the current input.
- `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).
- `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.
- `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()`.
- `at` - this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()`.
## Compilation at build
The best way to use `magic-regexp` is by making use of the included transform.
```js
const regExp = createRegExp(exactly('foo/test.js').after('bar/'))
// => gets _compiled_ to
const regExp = /(?<=bar\/)foo\/test\.js/
```
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:
```js
const someString = 'test'
const regExp = createRegExp(exactly(someString))
```
### Nuxt
```js
import { defineNuxtConfig } from 'nuxt'

@@ -71,3 +114,3 @@

**Vite**:
### Vite

@@ -83,3 +126,3 @@ ```js

**unbuild**:
### unbuild

@@ -118,5 +161,7 @@ ```js

[npm-downloads-href]: https://npmjs.com/package/magic-regexp
[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/magic-regexp/ci/main?style=flat-square
[github-actions-href]: https://github.com/unjs/magic-regexp/actions?query=workflow%3Aci
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/magic-regexp/main?style=flat-square
[codecov-href]: https://codecov.io/gh/unjs/magic-regexp
[github-actions-src]: https://img.shields.io/github/workflow/status/danielroe/magic-regexp/ci/main?style=flat-square
[github-actions-href]: https://github.com/danielroe/magic-regexp/actions?query=workflow%3Aci
[codecov-src]: https://img.shields.io/codecov/c/gh/danielroe/magic-regexp/main?style=flat-square
[codecov-href]: https://codecov.io/gh/danielroe/magic-regexp
[lgtm-src]: https://img.shields.io/lgtm/grade/javascript/github/danielroe/vue-bind-once?style=flat-square
[lgtm-href]: https://lgtm.com/projects/g/danielroe/magic-regexp

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc