forked from APIJSON/APIJSON-Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtil.js
More file actions
182 lines (152 loc) · 4.09 KB
/
StringUtil.js
File metadata and controls
182 lines (152 loc) · 4.09 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
/*Copyright ©2017 TommyLemon(https://github.com/TommyLemon/APIAuto)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use StringUtil file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
/**util for string
* @author Lemon
*/
var StringUtil = {
TAG: 'StringUtil',
/**获取string,为null则返回''
* @param s
* @return
*/
get: function(s) {
return s == null ? '' : s;
},
/**获取去掉前后空格后的string,为null则返回''
* @param s
* @return
*/
trim: function(s) {
return s == null ? '' : s.trim();
},
/**获取去掉所有空格后的string,为null则返回''
* @param s
* @return
*/
noBlank: function(s) {
return s == null ? '' : s.replace(/ /g, '');
},
/**判断字符是否为空
* @param s
* @param trim
* @return
*/
isEmpty: function(s, trim) {
if (s == null) {
return true;
}
if (trim) {
s = s.trim();
}
return s.length <= 0;
},
isNotEmpty: function(s, trim) {
return ! this.isEmpty(s, trim);
},
/**判断是否为代码名称,只能包含字母,数字或下划线
* @param s
* @return
*/
isName(s) {
return s != null && s.length > 0 && /[a-zA-Z_]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
},
/**判断是否为代码名称,只能包含字母,数字或下划线
* @param s
* @return
*/
isBigName(s) {
return s != null && s.length > 0 && /[A-Z]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
},
/**判断是否为代码名称,只能包含字母,数字或下划线
* @param s
* @return
*/
isSmallName(s) {
return s != null && s.length > 0 && /[a-z]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
},
isConstName(s) {
return s != null && s.length > 0 && /[A-Z_]/.test(s.substring(0, 1)) && /^[0-9A-Z_]+$/.test(s);
},
/**添加后缀
* @param key
* @param suffix
* @return key + suffix,第一个字母小写
*/
addSuffix: function(key, suffix) {
key = StringUtil.noBlank(key);
if (key == '') {
return StringUtil.firstCase(suffix);
}
return StringUtil.firstCase(key) + StringUtil.firstCase(suffix, true);
},
/**首字母大写或小写
* TODO upper == null 时不处理,false 小写,true 大写
* @param key
* @param upper
* @return
*/
firstCase: function(key, upper) {
key = StringUtil.get(key);
if (key == '') {
return '';
}
const first = key.substring(0, 1);
key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length);
return key;
},
/**全部大写
* @param s
* @param trim
* @return
*/
toUpperCase: function(s, trim) {
s = trim ? StringUtil.trim(s) : StringUtil.get(s);
return s.toUpperCase();
},
/**全部小写
* @param s
* @return
*/
toLowerCase: function(s, trim) {
s = trim ? StringUtil.trim(s) : StringUtil.get(s);
return s.toLowerCase();
},
split: function (s, separator, trim) {
if (s == null) {
return null;
}
if (separator == null) {
separator = ',';
}
if (trim) {
while (s.startsWith(separator)) {
s = s.substring(1);
}
while (s.endsWith(separator)) {
s = s.substring(0, s.length - 1);
}
}
if (s.indexOf(separator) < 0) {
return [s];
}
return s.split(separator)
},
isNumber: function (s) {
return typeof s == 'string' && /^[0-9]+$/.test(s);
},
join: function (arr, separator) {
return arr == null ? '' : arr.join(separator)
}
};
if (typeof module == 'object') {
module.exports = StringUtil;
}
//校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>