1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | String.prototype.exists = function (str) {
|
12 | return this.indexOf(str) >= 0;
|
13 | };
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | String.prototype.equals = function (str) {
|
21 | return Object.isUndefinedOrNull(str) == false && this === str;
|
22 | };
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | String.prototype.equalsIgnoreCase = function (str) {
|
30 | return str !== undefined && str !== null && this.toLowerCase() === str.toLowerCase();
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | String.prototype.isEmpty = function () {
|
38 | return this.length === 0;
|
39 | };
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | String.prototype.isNotEmpty = function () {
|
46 | return this.length > 0;
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | String.prototype.isBlank = function () {
|
54 | return /^\s*$/.test(this.toString());
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | String.prototype.repeat = function (count) {
|
63 | if (count < 1) {
|
64 | return "";
|
65 | }
|
66 | else {
|
67 | var s = this.toString();
|
68 | var result = s;
|
69 | for (var i = 0; i < count; i++) {
|
70 | result += s;
|
71 | }
|
72 | return result;
|
73 | }
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | String.prototype.left = function (length) {
|
82 | return this.substring(0, length);
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | String.prototype.right = function (length) {
|
91 | return this.substring(this.length - length, this.length);
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | String.prototype.truncation = function (length, truncation) {
|
102 | if (truncation === void 0) { truncation = '...'; }
|
103 | truncation = truncation || "...";
|
104 | return this.length > length ? this.slice(0, length <= truncation.length ? length : length - truncation.length) + truncation : String(this);
|
105 | };
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | String.prototype.ltrim = function () {
|
112 | return Object.isFunction(this.trimStart) ? this.trimStart() : this.replace(/^\s*/g, "");
|
113 | };
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | String.prototype.rtrim = function () {
|
120 | return Object.isFunction(this.trimEnd) ? this.trimEnd() : this.replace(/\s*$/g, "");
|
121 | };
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | String.prototype.startsWith = function (str) {
|
129 | return this.indexOf(str) === 0;
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | String.prototype.endsWith = function (str) {
|
138 | var d = this.length - str.length;
|
139 | return d >= 0 && this.lastIndexOf(str) === d;
|
140 | };
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | String.prototype.lcfirst = function () {
|
147 | return this.charAt(0).toLowerCase() + this.substring(1);
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | String.prototype.ucfirst = function () {
|
155 | return this.charAt(0).toUpperCase() + this.substring(1);
|
156 | };
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | String.prototype.escapeHTML = function () {
|
163 | return this.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | String.prototype.unescapeHTML = function () {
|
171 | return this.replace(/"/g, '"').replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
172 | };
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | String.prototype.stripTag = function (tag) {
|
180 | return this.replace(new RegExp("<" + tag + "(\\s+(\"[^\"]*\"|'[^']*'|[^>])+)?(\/)?>|<\/" + tag + ">", "gi"), "");
|
181 | };
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | String.prototype.stripTags = function (tags) {
|
189 | if (typeof tags === "string") {
|
190 | return this.stripTag(tags);
|
191 | }
|
192 | else if (Array.isArray(tags)) {
|
193 | var result = this.toString();
|
194 | for (var i = 0; i < tags.length; i++) {
|
195 | result = result.stripTag(tags[i]);
|
196 | }
|
197 | return result;
|
198 | }
|
199 | else {
|
200 | return this.toString();
|
201 | }
|
202 | };
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | String.prototype.stripScripts = function () {
|
209 | return this.replace(/<script[^>]*>([\S\s]*?)<\/script>/img, "");
|
210 | };
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | String.prototype.toArray = function (delimiter) {
|
218 | return this.split(delimiter || "");
|
219 | };
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | String.prototype.inspect = function (useDoubleQuotes) {
|
227 | var specialChar = { '\b': '\\b', '\t': '\\t', '\r': '\\r', '\n': '\\n', '\f': '\\f', '\\': '\\\\' };
|
228 | var escapedString = this.replace(/[\x00-\x1f\\]/g, function (character) {
|
229 | if (character in specialChar) {
|
230 | return specialChar[character];
|
231 | }
|
232 | return '\\u00' + character.charCodeAt(0).toPaddedString(2, 16);
|
233 | });
|
234 | if (useDoubleQuotes) {
|
235 | return '"' + escapedString.replace(/"/g, '\\"') + '"';
|
236 | }
|
237 | else {
|
238 | return "'" + escapedString.replace(/'/g, '\\\'') + "'";
|
239 | }
|
240 | };
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | String.prototype.hashCode = function () {
|
247 | var result = 0;
|
248 | if (result === 0 && this.length > 0) {
|
249 | for (var i = 0; i < this.length; i++) {
|
250 | result = 31 * result + this.charCodeAt(i);
|
251 | }
|
252 | }
|
253 | return result;
|
254 | };
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | String.random = function (length, type) {
|
268 | if (type === void 0) { type = "LETTER_NUMERIC"; }
|
269 | var result = "";
|
270 | if (type === "CHINESE") {
|
271 | for (var i = 0; i < length; i++) {
|
272 | result += String.fromCharCode(Math.rand(19968, 40891));
|
273 | }
|
274 | return result;
|
275 | }
|
276 | var numeric = "0123456789";
|
277 | var letter = "abcdefghijklmnopqrstuvwxyz";
|
278 | var map = {
|
279 | "NUMERIC": numeric,
|
280 | "LETTER": letter + letter.toUpperCase(),
|
281 | "LETTER_NUMERIC": numeric + letter + letter.toUpperCase()
|
282 | };
|
283 | if (!map[type]) {
|
284 | throw "Invalid argument type value, must be: NUMERIC, LETTER, LETTER_NUMERIC or CHINESE";
|
285 | }
|
286 | for (var j = 0; j < length; j++) {
|
287 | result += map[type].charAt(Math.rand(0, map[type].length - 1));
|
288 | }
|
289 | return result;
|
290 | };
|
291 |
|
\ | No newline at end of file |