diff --git a/README.md b/README.md index 3909b9a..4806110 100644 --- a/README.md +++ b/README.md @@ -751,17 +751,19 @@ The final result after running all plugins is provided to the `RegExp` construct - **`v`** — Forces the use of flag v even when it's not supported natively (resulting in an error). -### 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 diff --git a/spec/helpers/browsers.js b/spec/helpers/browsers.js index 5dcd659..c3308c0 100644 --- a/spec/helpers/browsers.js +++ b/spec/helpers/browsers.js @@ -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}); } diff --git a/spec/helpers/global.mjs b/spec/helpers/global.mjs index 54a1678..b656f4d 100644 --- a/spec/helpers/global.mjs +++ b/spec/helpers/global.mjs @@ -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}); diff --git a/spec/index.html b/spec/index.html index 09770c8..41ce864 100644 --- a/spec/index.html +++ b/spec/index.html @@ -24,7 +24,7 @@ - + diff --git a/spec/process.spec.js b/spec/rewrite.spec.js similarity index 67% rename from spec/process.spec.js rename to spec/rewrite.spec.js index 351b218..50b333b 100644 --- a/spec/process.spec.js +++ b/spec/rewrite.spec.js @@ -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(); }); }); @@ -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(); }); }); @@ -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', () => { diff --git a/src/regex.js b/src/regex.js index 6ce3c9d..c47cd11 100644 --- a/src/regex.js +++ b/src/regex.js @@ -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 @@ -403,5 +403,6 @@ function unmarkEmulationGroups(expression) { export { regex, pattern, - processRegex, + rewrite, + rewrite as processRegex, // Deprecated alias };