Skip to content

Commit

Permalink
processRegex -> rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Sep 5, 2024
1 parent a86dca4 commit b0e0924
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,17 +751,19 @@ The final result after running all plugins is provided to the `RegExp` construct
- **`v`** — Forces the use of flag <kbd>v</kbd> even when it's not supported natively (resulting in an error).
</details>
### Getting output as a string
### Returning a string
Function `processRegex` returns an object with properties `expression` and `flags` as strings, rather than returning a `RegExp` instance. This can be useful for tools that want to further process the output.
Function `rewrite` returns an object with properties `expression` and `flags` as strings, rather than returning a `RegExp` instance. This can be useful for tools that want to process the output.
```js
import {processRegex} from 'regex';
processRegex('^ (ab | cd)', {flags: 'm'});
import {rewrite} from 'regex';
rewrite('^ (ab | cd)', {flags: 'm'});
// → {expression: '^(?:ab|cd)', flags: 'mv'}
```
`processRegex` shares all of `regex`'s options (described above) except `subclass`. Providing the resulting `expression` and `flags` properties to the `RegExp` constructor produces the same result as a tagged `regex` template. However, since `processRegex` isn't a template tag, it doesn't provide context-aware interpolation and doesn't automatically handle input as a raw string (you need to escape your backslashes).
`rewrite` shares all of `regex`'s options (described above) except `subclass`. Providing the resulting `expression` and `flags` to the `RegExp` constructor produces the same result as using the `regex` tag.
> Since `rewrite` isn't a template tag, it doesn't provide context-aware interpolation and doesn't automatically handle input as a raw string (you need to escape your backslashes).
## ⚡ Performance
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/browsers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if (globalThis.Regex) {
const {regex, pattern, processRegex} = Regex;
Object.assign(globalThis, {regex, pattern, processRegex});
const {regex, pattern, rewrite} = Regex;
Object.assign(globalThis, {regex, pattern, rewrite});
}
4 changes: 2 additions & 2 deletions spec/helpers/global.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {regex, pattern, processRegex} from '../../dist/regex.mjs';
import {regex, pattern, rewrite} from '../../dist/regex.mjs';

// So specs can be shared with the browser test runner
Object.assign(globalThis, {regex, pattern, processRegex});
Object.assign(globalThis, {regex, pattern, rewrite});
2 changes: 1 addition & 1 deletion spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<!-- Specs -->
<script src="./regex-tag.spec.js"></script>
<script src="./process.spec.js"></script>
<script src="./rewrite.spec.js"></script>
<script src="./pattern.spec.js"></script>
<script src="./interpolate-pattern.spec.js"></script>
<script src="./interpolate-regexp.spec.js"></script>
Expand Down
32 changes: 16 additions & 16 deletions spec/process.spec.js → spec/rewrite.spec.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
describe('processRegex', () => {
describe('rewrite', () => {
function toRegExp(expression, options) {
const result = processRegex(expression, options);
const result = rewrite(expression, options);
return new RegExp(result.expression, result.flags);
}

it('should accept empty arguments', () => {
expect(processRegex().expression).toBe('');
expect(processRegex(undefined).expression).toBe('');
expect(rewrite().expression).toBe('');
expect(rewrite(undefined).expression).toBe('');
});

it('should coerce first argument to string', () => {
expect(processRegex(null).expression).toBe('null');
expect(processRegex(false).expression).toBe('false');
expect(processRegex(10).expression).toBe('10');
expect(rewrite(null).expression).toBe('null');
expect(rewrite(false).expression).toBe('false');
expect(rewrite(10).expression).toBe('10');
});

it('should accept a string without options', () => {
expect(processRegex('').expression).toBe('');
expect(processRegex('.').expression).toBe('.');
expect(rewrite('').expression).toBe('');
expect(rewrite('.').expression).toBe('.');
});

describe('implicit flags', () => {
it('should implicitly add flag v or u', () => {
if (flagVSupported) {
expect(processRegex('').flags).toContain('v');
expect(processRegex('').flags).not.toContain('u');
expect(rewrite('').flags).toContain('v');
expect(rewrite('').flags).not.toContain('u');
} else {
expect(processRegex('').flags).toContain('u');
expect(processRegex('').flags).not.toContain('v');
expect(rewrite('').flags).toContain('u');
expect(rewrite('').flags).not.toContain('v');
}
});

it('should not allow explicitly adding implicit flags', () => {
const flags = ['n', 'u', 'v', 'x'];
flags.forEach(f => {
expect(() => processRegex('', {flags: f})).toThrow();
expect(() => rewrite('', {flags: f})).toThrow();
});
});

Expand All @@ -44,7 +44,7 @@ describe('processRegex', () => {

it('should process emulated flag n', () => {
expect(toRegExp('^(a)$').exec('a')).toHaveSize(1);
expect(() => processRegex('^(a)\\1$')).toThrow();
expect(() => rewrite('^(a)\\1$')).toThrow();
});
});

Expand All @@ -70,7 +70,7 @@ describe('processRegex', () => {

describe('options', () => {
it('should not allow enabling option subclass', () => {
expect(() => processRegex('', {subclass: true})).toThrow();
expect(() => rewrite('', {subclass: true})).toThrow();
});

it('should allow diabling implicit flags', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Returns the processed expression and flags as strings.
@param {RegexTagOptions} [options]
@returns {{expression: string; flags: string;}}
*/
function processRegex(expression, options = {}) {
function rewrite(expression, options = {}) {
const opts = getOptions(options);
if (opts.subclass) {
// Don't allow including emulation group markers in output
Expand Down Expand Up @@ -403,5 +403,6 @@ function unmarkEmulationGroups(expression) {
export {
regex,
pattern,
processRegex,
rewrite,
rewrite as processRegex, // Deprecated alias
};

0 comments on commit b0e0924

Please sign in to comment.