Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/pluginutils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ exactRegex(['foo', 'bar']); // /^(?:foo|bar)$/
exactRegex('foo(bar)', 'i'); // /^foo\(bar\)$/i
```

### wordRegex

Constructs a RegExp that matches the specified string with word boundaries. This is useful when matching a complete word-like token, such as `node_modules` but not `node_modulesa`.

Parameters: `(str: String, flags?: String)`<br>
Returns: `RegExp`

#### Usage

```js
import { wordRegex } from '@rollup/pluginutils';

wordRegex('node_modules'); // /\bnode_modules\b/
wordRegex('foo.bar', 'i'); // /\bfoo\.bar\b/i
```

### makeLegalIdentifier

Constructs a bundle-safe identifier from a `String`.
Expand Down
6 changes: 5 additions & 1 deletion packages/pluginutils/src/filterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export function suffixRegex(str: string | string[], flags?: string): RegExp {
return new RegExp(`${combineMultipleStrings(str)}$`, flags);
}

const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
export function wordRegex(str: string, flags?: string): RegExp {
return new RegExp(`\\b${escapeRegex(str)}\\b`, flags);
Comment thread
btea marked this conversation as resolved.
Resolved
}

const escapeRegexRE = /[/\\^$*+?.()|[\]{}]/g;
function escapeRegex(str: string): string {
return str.replace(escapeRegexRE, '\\$&');
}
Expand Down
8 changes: 5 additions & 3 deletions packages/pluginutils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dataToEsm from './dataToEsm';
import extractAssignedNames from './extractAssignedNames';
import makeLegalIdentifier from './makeLegalIdentifier';
import normalizePath from './normalizePath';
import { exactRegex, prefixRegex, suffixRegex } from './filterUtils';
import { exactRegex, prefixRegex, suffixRegex, wordRegex } from './filterUtils';

export {
addExtension,
Expand All @@ -17,7 +17,8 @@ export {
makeLegalIdentifier,
normalizePath,
prefixRegex,
suffixRegex
suffixRegex,
wordRegex
};

// TODO: remove this in next major
Expand All @@ -31,5 +32,6 @@ export default {
makeLegalIdentifier,
normalizePath,
prefixRegex,
suffixRegex
suffixRegex,
wordRegex
};
29 changes: 28 additions & 1 deletion packages/pluginutils/test/filterUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exactRegex, prefixRegex, suffixRegex } from '../';
import { exactRegex, prefixRegex, suffixRegex, wordRegex } from '../';

test('exactRegex supports without flag parameter', () => {
expect(exactRegex('foo').toString()).toBe('/^foo$/');
Expand Down Expand Up @@ -71,3 +71,30 @@ test('suffixRegex escapes special characters for Regex', () => {
test('suffixRegex escapes special characters with multiple string for Regex', () => {
expect(suffixRegex(['foo(bar)', 'baz(qux)']).toString()).toBe('/(?:foo\\(bar\\)|baz\\(qux\\))$/');
});

test('wordRegex matches complete word-like tokens', () => {
const regex = wordRegex('node_modules');

expect(regex.toString()).toBe('/\\bnode_modules\\b/');
expect(regex.test('node_modules')).toBe(true);
expect(regex.test('/node_modules/')).toBe(true);
expect(regex.test('foo/node_modules/bar')).toBe(true);
expect(regex.test('node_modulesa')).toBe(false);
expect(regex.test('anode_modules')).toBe(false);
});

test('wordRegex supports flags and escapes special characters', () => {
const regex = wordRegex('foo.bar', 'i');

expect(regex.toString()).toBe('/\\bfoo\\.bar\\b/i');
expect(regex.test('FOO.BAR')).toBe(true);
expect(regex.test('foo.barx')).toBe(false);
});

test('wordRegex supports hyphens with the Unicode flag', () => {
const regex = wordRegex('foo-bar', 'u');

expect(regex.toString()).toBe('/\\bfoo-bar\\b/u');
expect(regex.test('foo-bar')).toBe(true);
expect(regex.test('foo-barx')).toBe(false);
});
9 changes: 9 additions & 0 deletions packages/pluginutils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ export function prefixRegex(str: string | string[], flags?: string): RegExp;
*/
export function suffixRegex(str: string | string[], flags?: string): RegExp;

/**
* Constructs a RegExp that matches the specified string with word boundaries.
* @param str the string to match.
* @param flags flags for the RegExp.
*/
export function wordRegex(str: string, flags?: string): RegExp;

export type AddExtension = typeof addExtension;
export type AttachScopes = typeof attachScopes;
export type CreateFilter = typeof createFilter;
Expand All @@ -109,6 +116,7 @@ export type NormalizePath = typeof normalizePath;
export type DataToEsm = typeof dataToEsm;
export type PrefixRegex = typeof prefixRegex;
export type SuffixRegex = typeof suffixRegex;
export type WordRegex = typeof wordRegex;

declare const defaultExport: {
addExtension: AddExtension;
Expand All @@ -121,5 +129,6 @@ declare const defaultExport: {
normalizePath: NormalizePath;
prefixRegex: PrefixRegex;
suffixRegex: SuffixRegex;
wordRegex: WordRegex;
};
export default defaultExport;
Loading