This repository was archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.js
More file actions
executable file
·65 lines (59 loc) · 1.58 KB
/
StringUtils.js
File metadata and controls
executable file
·65 lines (59 loc) · 1.58 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
// useful string util functions form the java apache commons lang:
// https://raw.github.com/apache/commons-lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
var StringUtils = {
isString : function(str) {
return typeof str === "string";
},
isEmpty : function(str) {
if (!StringUtils.isString(str)) {
return true;
}
return !str.length > 0;
},
substringBefore : function(str, separator) {
if (StringUtils.isEmpty(str) || !StringUtils.isString(separator)) {
return str;
}
if (separator.length === 0) {
return '';
}
var pos = str.indexOf(separator);
if (pos === -1) {
return str;
}
return str.substring(0, pos);
},
/**
* Gets the substring after the first occurrence of a separator. The separator is not returned.
*/
substringAfter : function(str, separator) {
if (StringUtils.isEmpty(str)) {
return str;
}
if (separator == null) {
return '';
}
var pos = str.indexOf(separator);
if (pos === -1) {
return '';
}
return str.substring(pos + separator.length);
},
/**
* Gets the substring after the last occurrence of a separator. The separator is not returned.
*/
substringAfterLast : function(str, separator) {
if (StringUtils.isEmpty(str)) {
return str;
}
if (StringUtils.isEmpty(separator)) {
return '';
}
var pos = str.lastIndexOf(separator);
if (pos === -1 || pos === str.length - separator.length) {
return '';
}
return str.substring(pos + separator.length);
}
};
module.exports = StringUtils;