Skip to content

Commit

Permalink
feat: support toRegex and toRegexString with restrictions (#1)
Browse files Browse the repository at this point in the history
* feat: support toRegex and toRegexString with restrictions

* docs: add README
  • Loading branch information
arthurdenner authored Mar 17, 2021
1 parent 9a56c8f commit ed14812
Show file tree
Hide file tree
Showing 15 changed files with 13,822 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build project
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v1
- name: Install Node.js 10
uses: actions/setup-node@v1
with:
node-version: 10
- name: Setup cache
uses: bahmutov/npm-install@HEAD
- name: Install dependencies
run: npm t
- name: Test and build code
run: npm run validate
- name: Upload test reports
uses: codecov/codecov-action@v1
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
with:
branches: |
[
'master',
{
name: 'beta',
prerelease: true
}
]
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
coverage
dist
node_modules
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-exact=true
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('kcd-scripts/prettier')
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# super-expressive.macro

A macro to generate regular expressions at build-time with super-expressive.

[![version][version-badge]][package] [![MIT License][license-badge]][license]
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)

> You may like to watch
> [this YouTube video](https://www.youtube.com/watch?v=1queadQ0048&list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u)
> to get an idea of what macros are and how to use them.
## Motivation

[Super Expressive](https://github.com/francisrstokes/super-expressive) is
awesome, but it's a runtime library only.

Depending on the use case, Super Expressive is very useful for declaring the
regular expressions, but when building the project, the final expression is
preferred.

This library aims to be a drop-in replacement for Super Expressive where
applicable - see [restrictions](#restrictions).

## Installation

`npm install super-expressive.macro`

`yarn add super-expressive.macro`

> This library depends on `super-expressive`. Please make sure to install it.
## Usage

To use macros, make sure to
[set up `babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros/blob/main/other/docs/user.md).

## Example

```js
import SuperExpressive from 'super-expressive.macro'

const myRegex = SuperExpressive()
.startOfInput.optional.string('0x')
.capture.exactly(4)
.anyOf.range('A', 'F')
.range('a', 'f')
.range('0', '9')
.end()
.end()
.endOfInput.toRegex()
```

After compilation:

```js
const myRegex = /^(?:0x)?([A-Fa-f0-9]{4})$/
```

> Notice that the macro import disappeared after compilation
Check the
[`super-expressive`'s detailed API](https://github.com/francisrstokes/super-expressive/#api)
for more examples. Don't forget to check the [restrictions](#restrictions).

## Restrictions

Because this plugin runs at build-time, there are some restrictions to its
usage.

- You can't use dynamic variables inside the macro, all the params to its
methods must be literals;
- You can't use constant variables inside the macro at the moment but
[there's an issue](https://github.com/arthurdenner/super-expressive.macro/issues/2)
to track if this is possible as we improve this macro;
- You can't use [subexpressions] inside the macro at the moment but
[there's an issue](https://github.com/arthurdenner/super-expressive.macro/issues/3)
to track if this is possible as we improve this macro;

## Contributors

Thanks goes to these wonderful people
([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the
[all-contributors](https://github.com/kentcdodds/all-contributors)
specification. Contributions of any kind welcome!

## LICENSE

MIT

[license-badge]:
https://img.shields.io/npm/l/super-expressive.macro.svg?style=flat-square
[license]:
https://github.com/arthurdenner/super-expressive.macro/blob/master/LICENSE
[version-badge]:
https://img.shields.io/npm/v/super-expressive.macro.svg?style=flat-square
[package]: https://www.npmjs.com/package/super-expressive.macro
Loading

0 comments on commit ed14812

Please sign in to comment.