forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.js
111 lines (97 loc) · 3.31 KB
/
string.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import escapeStringRegexp from 'escape-string-regexp';
// Initializing this RegExp outside of removeURLs because that function is in a
// hot path during sanitization and it's good to avoid the initialization of the
// RegExp which is costly.
const REMOVE_URLS_REGEXP = (function () {
const protocols = [
'http',
'https',
'ftp',
'file',
'moz-extension',
'moz-page-thumb',
];
return new RegExp(
`\\b((?:${protocols.join('|')})://)/?[^\\s/$.?#][^\\s)]*`,
// ^ ^ ^
// | | Matches any characters except
// | | whitespaces and ')' character.
// | | Other characters are allowed now
// | Matches any character except whitespace
// | and '/', '$', '.', '?' or '#' characters
// | because this is start of the URL
// Matches URL schemes we need to sanitize.
'gi'
);
})();
/**
* Takes a string and returns the string with public URLs removed.
* It doesn't remove the URLs like `chrome://..` because they are internal URLs
* and they shouldn't be removed.
*/
export function removeURLs(
string: string,
redactedText: string = '<URL>'
): string {
return string.replace(REMOVE_URLS_REGEXP, '$1' + redactedText);
}
/**
* Take an absolute file path string and sanitize it except the last file name segment.
*
* Note: Do not use this function if the string not only contains a file path but
* also contains more text. This function is intended to use only for path strings.
*/
export function removeFilePath(
filePath: string,
redactedText: string = '<PATH>'
): string {
let pathSeparator = null;
// Figure out which separator the path uses and the last separator index.
let lastSeparatorIndex = filePath.lastIndexOf('/');
if (lastSeparatorIndex !== -1) {
// This is a Unix-like path.
pathSeparator = '/';
} else {
lastSeparatorIndex = filePath.lastIndexOf('\\');
if (lastSeparatorIndex !== -1) {
// This is a Windows path.
pathSeparator = '\\';
}
}
if (pathSeparator === null) {
// There is no path separator, which means it's either not a file path or empty.
return filePath;
}
return redactedText + pathSeparator + filePath.slice(lastSeparatorIndex + 1);
}
/**
* Divide a search string into several parts by splitting on comma.
*/
export const splitSearchString = (searchString: string): string[] | null => {
if (!searchString) {
return null;
}
const result = searchString
.split(',')
.map((part) => part.trim())
.filter((part) => part);
if (result.length) {
return result;
}
return null;
};
/**
* Concatenate an array of strings into a RegExp that matches on all
* the strings.
*/
export const stringsToRegExp = (strings: string[] | null): RegExp | null => {
if (!strings || !strings.length) {
return null;
}
const regexpStr = strings.map(escapeStringRegexp).join('|');
return new RegExp(regexpStr, 'gi');
};