-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathutils.js
More file actions
255 lines (218 loc) · 6.16 KB
/
utils.js
File metadata and controls
255 lines (218 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import escapeStringRegExp from 'escape-string-regexp';
import {meta} from '../../hydration';
import {formatDataForPreview} from '../../utils';
import isArray from 'react-devtools-shared/src/isArray';
import hasOwnProperty from 'shared/hasOwnProperty';
import type {HooksTree} from 'react-debug-tools/src/ReactDebugHooks';
export function alphaSortEntries(
entryA: [string, mixed],
entryB: [string, mixed],
): number {
const a = entryA[0];
const b = entryB[0];
if (String(+a) === a) {
if (String(+b) !== b) {
return -1;
}
return +a < +b ? -1 : 1;
}
return a < b ? -1 : 1;
}
export function createRegExp(string: string): RegExp {
// Allow /regex/ syntax with optional last /
if (string[0] === '/') {
// Cut off first slash
string = string.slice(1);
// Cut off last slash, but only if it's there
if (string[string.length - 1] === '/') {
string = string.slice(0, string.length - 1);
}
try {
return new RegExp(string, 'i');
} catch (err) {
// Bad regex. Make it not match anything.
// TODO: maybe warn in console?
return new RegExp('.^');
}
}
function isLetter(char: string) {
return char.toLowerCase() !== char.toUpperCase();
}
function matchAnyCase(char: string) {
if (!isLetter(char)) {
// Don't mess with special characters like [.
return char;
}
return '[' + char.toLowerCase() + char.toUpperCase() + ']';
}
// 'item' should match 'Item' and 'ListItem', but not 'InviteMom'.
// To do this, we'll slice off 'tem' and check first letter separately.
const escaped = escapeStringRegExp(string);
const firstChar = escaped[0];
let restRegex = '';
// For 'item' input, restRegex becomes '[tT][eE][mM]'
// We can't simply make it case-insensitive because first letter case matters.
for (let i = 1; i < escaped.length; i++) {
restRegex += matchAnyCase(escaped[i]);
}
if (!isLetter(firstChar)) {
// We can't put a non-character like [ in a group
// so we fall back to the simple case.
return new RegExp(firstChar + restRegex);
}
// Construct a smarter regex.
return new RegExp(
// For example:
// (^[iI]|I)[tT][eE][mM]
// Matches:
// 'Item'
// 'ListItem'
// but not 'InviteMom'
'(^' +
matchAnyCase(firstChar) +
'|' +
firstChar.toUpperCase() +
')' +
restRegex,
);
}
export function getMetaValueLabel(data: Object): string | null {
if (hasOwnProperty.call(data, meta.preview_long)) {
return data[meta.preview_long];
} else {
return formatDataForPreview(data, true);
}
}
function sanitize(data: Object): void {
for (const key in data) {
const value = data[key];
if (value && value[meta.type]) {
data[key] = getMetaValueLabel(value);
} else if (value != null) {
if (isArray(value)) {
sanitize(value);
} else if (typeof value === 'object') {
sanitize(value);
}
}
}
}
export function serializeDataForCopy(props: Object): string {
const cloned = isArray(props) ? props.slice(0) : Object.assign({}, props);
sanitize(cloned);
try {
return JSON.stringify(cloned, null, 2);
} catch (error) {
return '';
}
}
export function serializeHooksForCopy(hooks: HooksTree | null): string {
// $FlowFixMe[not-an-object] "HooksTree is not an object"
const cloned = Object.assign(([]: Array<any>), hooks);
const queue = [...cloned];
while (queue.length > 0) {
const current = queue.pop();
// These aren't meaningful
// $FlowFixMe[incompatible-use]
delete current.id;
// $FlowFixMe[incompatible-use]
delete current.isStateEditable;
// $FlowFixMe[incompatible-use]
if (current.subHooks.length > 0) {
// $FlowFixMe[incompatible-use]
queue.push(...current.subHooks);
}
}
sanitize(cloned);
try {
return JSON.stringify(cloned, null, 2);
} catch (error) {
return '';
}
}
// Keeping this in memory seems to be enough to enable the browser to download larger profiles.
// Without this, we would see a "Download failed: network error" failure.
let downloadUrl = null;
export function downloadFile(
element: HTMLAnchorElement,
filename: string,
text: string,
): void {
const blob = new Blob([text], {type: 'text/plain;charset=utf-8'});
if (downloadUrl !== null) {
URL.revokeObjectURL(downloadUrl);
}
downloadUrl = URL.createObjectURL(blob);
element.setAttribute('href', downloadUrl);
element.setAttribute('download', filename);
element.click();
}
export function truncateText(text: string, maxLength: number): string {
const {length} = text;
if (length > maxLength) {
return (
text.slice(0, Math.floor(maxLength / 2)) +
'…' +
text.slice(length - Math.ceil(maxLength / 2) - 1)
);
} else {
return text;
}
}
export function pluralize(word: string): string {
if (!/^[a-z]+$/i.test(word)) {
// If it's not a single a-z word, give up.
return word;
}
// Bail out if it's already plural.
switch (word) {
case 'men':
case 'women':
case 'children':
case 'feet':
case 'teeth':
case 'mice':
case 'people':
return word;
}
if (
/(ches|shes|ses|xes|zes)$/i.test(word) ||
/[^s]ies$/i.test(word) ||
/ves$/i.test(word) ||
/[^s]s$/i.test(word)
) {
return word;
}
switch (word) {
case 'man':
return 'men';
case 'woman':
return 'women';
case 'child':
return 'children';
case 'foot':
return 'feet';
case 'tooth':
return 'teeth';
case 'mouse':
return 'mice';
case 'person':
return 'people';
}
// Words ending in s, x, z, ch, sh → add "es"
if (/(s|x|z|ch|sh)$/i.test(word)) return word + 'es';
// Words ending in consonant + y → replace y with "ies"
if (/[bcdfghjklmnpqrstvwxz]y$/i.test(word)) return word.slice(0, -1) + 'ies';
// Words ending in f or fe → replace with "ves"
if (/(?:f|fe)$/i.test(word)) return word.replace(/(?:f|fe)$/i, 'ves');
// Default: just add "s"
return word + 's';
}