Fix special cases
Dan Abramov committed
Apr 4, 2019 at 17:00 UTC
0f0062a71c6e6d9f7b29fb30af2880cb7c316318
2 files changed
+26
-10
src/devtools/views/Components/SearchInput.js
+1
-1
@@ -95,7 +95,7 @@ export default function SearchInput(props: Props) {
95
onChange={handleTextChange}
96
onKeyDown={handleKeyDown}
97
onKeyPress={handleInputKeyPress}
98
- placeholder="Search (text or /regex/)"
98
+ placeholder="Search"
99
ref={inputRef}
100
value={searchText}
101
/>
src/devtools/views/utils.js
+25
-9
@@ -6,18 +6,36 @@ import { meta } from '../../hydration';
6
import type { HooksTree } from 'src/backend/types';
7
8
export function createRegExp(string: string): RegExp {
9
+ function isLetter(char: string) {
10
+ return char.toLowerCase() !== char.toUpperCase();
11
+ }
12
+
13
+ function matchAnyCase(char: string) {
14
+ if (!isLetter(char)) {
15
+ // Don't mess with special characters like [.
16
+ return char;
17
+ }
18
+ return '[' + char.toLowerCase() + char.toUpperCase() + ']';
19
+ }
20
+
21
// 'item' should match 'Item' and 'ListItem', but not 'InviteMom'.
22
// To do this, we'll slice off 'tem' and check first letter separately.
23
const escaped = escapeStringRegExp(string);
12
- const firstLetter = escaped[0];
24
+ const firstChar = escaped[0];
25
let restRegex = '';
26
// For 'item' input, restRegex becomes '[tT][eE][mM]'
27
// We can't simply make it case-insensitive because first letter case matters.
28
for (let i = 1; i < escaped.length; i++) {
17
- const char = escaped[i];
18
- restRegex += '[' + char.toLowerCase() + char.toUpperCase() + ']';
29
+ restRegex += matchAnyCase(escaped[i]);
30
+ }
31
+
32
+ if (!isLetter(firstChar)) {
33
+ // We can't put a non-character like [ in a group
34
+ // so we fall back to the simple case.
35
+ return new RegExp(firstChar + restRegex);
36
}
20
- // Respect first letter only if it starts a word.
37
+
38
+ // Construct a smarter regex.
39
return new RegExp(
40
// For example:
41
// (^[iI]|I)[tT][eE][mM]
@@ -25,12 +43,10 @@ export function createRegExp(string: string): RegExp {
43
// 'Item'
44
// 'ListItem'
45
// but not 'InviteMom'
28
- '(^[' +
29
- firstLetter.toLowerCase() +
30
- firstLetter.toUpperCase() +
31
- ']' +
46
+ '(^' +
47
+ matchAnyCase(firstChar) +
48
'|' +
33
- firstLetter.toUpperCase() +
49
+ firstChar.toUpperCase() +
50
')' +
51
restRegex
52
);