Skip to content

Commit 5307b46

Browse files
committed
Incorporate escape-string-regexp function (#540)
1 parent db7501e commit 5307b46

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"dependencies": {
4343
"@babel/runtime": "^7.3.4",
4444
"classnames": "^2.2.0",
45-
"escape-string-regexp": "^3.0.0",
4645
"fast-deep-equal": "^3.1.1",
4746
"invariant": "^2.2.1",
4847
"lodash.debounce": "^4.0.8",

src/__tests__/utils/getMatchBounds.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getMatchBounds } from '../../utils';
1+
import { escapeStringRegexp, getMatchBounds } from '../../utils';
22

33
describe('getMatchBounds', () => {
44
test('handles a normal string', () => {
@@ -43,3 +43,14 @@ describe('getMatchBounds', () => {
4343
expect(bounds.end).toBe(22);
4444
});
4545
});
46+
47+
describe('escapeStringRegexp', () => {
48+
test('main', () => {
49+
expect(escapeStringRegexp('\\ ^ $ * + ? . ( ) | { } [ ]'))
50+
.toBe('\\\\ \\^ \\$ \\* \\+ \\? \\. \\( \\) \\| \\{ \\} \\[ \\]');
51+
});
52+
53+
test('escapes `-` in a way compatible with PCRE', () => {
54+
expect(escapeStringRegexp('foo - bar')).toBe('foo \\x2d bar');
55+
});
56+
});

src/utils/getMatchBounds.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22

3-
import escapeStringRegexp from 'escape-string-regexp';
3+
import invariant from 'invariant';
44
import stripDiacritics from './stripDiacritics';
55

66
const CASE_INSENSITIVE = 'i';
@@ -11,6 +11,22 @@ type MatchBounds = {
1111
start: number,
1212
};
1313

14+
// Export for testing.
15+
export function escapeStringRegexp(str: string): string {
16+
invariant(
17+
typeof str === 'string',
18+
'`escapeStringRegexp` expected a string.'
19+
);
20+
21+
// Escape characters with special meaning either inside or outside character
22+
// sets. Use a simple backslash escape when it’s always valid, and a \unnnn
23+
// escape when the simpler form would be disallowed by Unicode patterns’
24+
// stricter grammar.
25+
return str
26+
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
27+
.replace(/-/g, '\\x2d');
28+
}
29+
1430
export default function getMatchBounds(
1531
subject: string,
1632
str: string

src/utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export getHintText from './getHintText';
55
export getInputProps from './getInputProps';
66
export getInputText from './getInputText';
77
export getIsOnlyResult from './getIsOnlyResult';
8-
export getMatchBounds from './getMatchBounds';
8+
export getMatchBounds, { escapeStringRegexp } from './getMatchBounds';
99
export getMenuItemId from './getMenuItemId';
1010
export getOptionLabel from './getOptionLabel';
1111
export getOptionProperty from './getOptionProperty';

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,11 +3150,6 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
31503150
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
31513151
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
31523152

3153-
escape-string-regexp@^3.0.0:
3154-
version "3.0.0"
3155-
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-3.0.0.tgz#1dad9cc28aed682be0de197280f79911a5fccd61"
3156-
integrity sha512-11dXIUC3umvzEViLP117d0KN6LJzZxh5+9F4E/7WLAAw7GrHk8NpUR+g9iJi/pe9C0py4F8rs0hreyRCwlAuZg==
3157-
31583153
escodegen@^1.9.1:
31593154
version "1.14.1"
31603155
resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.14.1.tgz#ba01d0c8278b5e95a9a45350142026659027a457"

0 commit comments

Comments
 (0)