1.0.0 • Published 4 years ago

babel-plugin-hoist-regex v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Babel Plugin Hoist Regex

A simple babel plugin to hoist regular expressions

Installation

yarn add babel-plugin-hoist-regex
# or 
npm i babel-plugin-hoist-regex

Usage

// .babelrc
{
  "plugins": [
      "babel-plugin-hoist-regex"
    ]
}

Before transpilation:

function literalRegExp(value) {
  const regex = /[0-9]+/
  const result = regex.exec(value)
  return (newRegExp = () => {
    const rgx = new RegExp('\\d+')
    const input = rgx.test(result.input)
    return input
  })()
}
literalRegExp(123456789)

After transpilation:

const _rgx = new RegExp('\\d+');

const _regex = /[0-9]+/;

function literalRegExp(value) {
  var result = _regex.exec(value);

  return (newRegExp = function newRegExp() {
    var input = _rgx.test(result.input);

    return input;
  })();
}

literalRegExp(123456789);

License

MIT