forked from scratchfoundation/scratch-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-util.js
More file actions
41 lines (37 loc) · 1.28 KB
/
Copy pathstring-util.js
File metadata and controls
41 lines (37 loc) · 1.28 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
class StringUtil {
static withoutTrailingDigits (s) {
let i = s.length - 1;
while ((i >= 0) && ('0123456789'.indexOf(s.charAt(i)) > -1)) i--;
return s.slice(0, i + 1);
}
static unusedName (name, existingNames) {
if (existingNames.indexOf(name) < 0) return name;
name = StringUtil.withoutTrailingDigits(name);
let i = 2;
while (existingNames.indexOf(name + i) >= 0) i++;
return name + i;
}
/**
* Split a string on the first occurrence of a split character.
* @param {string} text - the string to split.
* @param {string} separator - split the text on this character.
* @returns {[string, string]} - the two parts of the split string, or [text, null] if no split character found.
* @example
* // returns ['foo', 'tar.gz']
* splitFirst('foo.tar.gz', '.');
* @example
* // returns ['foo', null]
* splitFirst('foo', '.');
* @example
* // returns ['foo', '']
* splitFirst('foo.', '.');
*/
static splitFirst (text, separator) {
const index = text.indexOf(separator);
if (index >= 0) {
return [text.substring(0, index), text.substring(index + 1)];
}
return [text, null];
}
}
module.exports = StringUtil;