-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr-lib.js
More file actions
609 lines (552 loc) · 16.9 KB
/
Copy pathstr-lib.js
File metadata and controls
609 lines (552 loc) · 16.9 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
(function (global, Str) {
"use strict";
// region [ Private Functions ]
/**
* Test if the specified object is an array.
* @param obj {Array|object}
* @returns {boolean}
* @private
*/
function _isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
/**
* Convert json string into js object.
* @param s
* @private
*/
function _parseJson(s) {
var _parser;
if (typeof global.JSON !== 'undefined') {
_parser = global.JSON.parse;
}
else if (typeof window.jQuery !== 'undefined') {
_parser = window.jQuery.parseJSON;
}
if (typeof _parser === 'undefined') {
throw 'Undefined JSON method';
}
return _parser(s);
}
// endregion
/**
* Check for undefined, null, zero length, blanks or s is false.
* @param s {string|object} - string, array or object to test.
* @returns {boolean}
* Unit Test: http://jsfiddle.net/wao20/TGP3N/
*/
Str.empty = function (s) {
// s == undefined <= double equals is deliberate, check for null and undefined
return !!(s == undefined
|| s.length === 0
|| Str.trim(s).length === 0
|| !s);
};
/**
* Compare two strings
* @param s1 {?string}
* @param s2 {?string}
* @param caseSensitive {boolean=}
* @returns {boolean}
*/
Str.equals = function (s1, s2, caseSensitive) {
if (s1 == undefined || s2 == undefined) {
return false;
}
if (caseSensitive) {
return s1 == s2;
}
return s1.toLowerCase() == s2.toLowerCase();
};
/**
* empty(), '0', '0.0', 'false' => false. Otherwise return !!s.
*
* @param s {?string}
* @returns {boolean}
*/
Str.boolVal = function (s) {
if (Str.empty(s)) {
return false;
}
s = Str.trim(s).toLowerCase();
if (s == '0' || s == '0.0' || s == 'false') {
return false;
}
return !!s;
};
/**
* Escape the string to be use as a literal in regex expression.
*
* @param s {string|Array}
* @returns {string|Array}
*/
Str.regexEscape = function (s) {
if (!s) {
return '';
}
if (typeof s === 'string') {
return s.replace(/([.?*+\^$\[\]\\(){}|\-])/g, '\\$1');
}
else if (_isArray(s)) {
var result = [], i;
for (i = 0; i < s.length; i++) {
result.push(Str.regexEscape(s[i]));
}
return result;
}
return s;
};
/**
* Tests whether the beginning of a string matches pattern.
*
* @param s {string}
* @param pattern {string} - to find
* @param caseSensitive {boolean=}
* @return {boolean}
*/
Str.startsWith = function (s, pattern, caseSensitive) {
if (caseSensitive) {
return s.indexOf(pattern) === 0;
}
return s.toLowerCase().indexOf(pattern.toLowerCase()) === 0;
};
/**
* Test if string ends with specified pattern
* @param s {string}
* @param pattern {string}
* @param caseSensitive {boolean=}
* @returns {boolean}
*/
Str.endsWith = function (s, pattern, caseSensitive) {
var d = s.length - pattern.length;
if (caseSensitive) {
return d >= 0 && s.lastIndexOf(pattern) === d;
}
return d >= 0 && s.toLowerCase().lastIndexOf(pattern.toLowerCase()) === d;
};
/**
* Check if the string contains a substring.
* @param s {string}
* @param needle {string}
* @param caseSensitive {boolean=}
* @return {boolean}
*/
Str.contains = function (s, needle, caseSensitive) {
if (Str.empty(s) || Str.empty(needle)) {
return false;
}
if (caseSensitive) {
return s.indexOf(needle) > -1;
}
return s.toLowerCase().indexOf(needle.toLowerCase()) > -1;
};
/**
* Must contains all the element in the array.
* @param s {string}
* @param needles {Array|string}
* @param caseSensitive {boolean=}
* @return {boolean}
*/
Str.containsAll = function (s, needles, caseSensitive) {
var i = 0;
if (_isArray(needles)) {
for (i = 0; i < needles.length; i++) {
if (!Str.contains(s, needles[i], caseSensitive)) {
return false;
}
}
return true;
}
return Str.contains(s, needles, caseSensitive);
};
/**
* Must contains ANY the element in the array.
* @param s {string}
* @param needles {Array|string}
* @param caseSensitive {boolean=}
* @return {boolean}
*/
Str.containsAny = function (s, needles, caseSensitive) {
var i;
if (_isArray(needles)) {
for (i = 0; i < needles.length; i++) {
if (Str.contains(s, needles[i], caseSensitive)) {
return true;
}
}
return false;
}
return Str.contains(s, needles, caseSensitive);
};
/**
* Determine if the specified variable is a string
* @param o
* @returns {boolean}
*/
Str.isString = function (o) {
return typeof o === 'string';
};
/**
* Trims white space from the beginning and end of a string.
* @param s {string}
* @param c {string=}
* @return {string}
*/
Str.trim = function (s, c) {
if (!Str.isString(s)) {
return s;
}
if (c == undefined || c == ' ') {
if (String.prototype.trim) {
return String.prototype.trim.call(s);
}
return s.replace(/^\s+/, '').replace(/\s+$/, '');
}
return Str.trimStart(Str.trimEnd(s, c), c);
};
/**
* Remove chars/Str from the start of the string
* @param s
* @param c {string|Array=} - supports Str.trimStart(s, ['0x0', '0', 'x']);
*/
Str.trimStart = function (s, c) {
if (c == undefined || c == '') {
return s.replace(/^\s+/, '');
}
var trims = c, regex, result;
if (!_isArray(c)) {
trims = [c];
}
trims = Str.regexEscape(trims).join('|');
regex = '^(' + trims + '|\s)+';
regex = new RegExp(regex, 'g');
result = s.replace(regex, '');
return result;
};
/**
* Remove chars/Str(s) from the end of the string
* @param s {string}
* @param c {string|Array=} - supports Str.trimEnd(s, ['0x0', '0', 'x']);
*/
Str.trimEnd = function (s, c) {
if (c == undefined) {
return s.replace(/\s+$/, '');
}
var trims = c, regex, result;
if (!_isArray(c)) {
trims = [c];
}
trims = Str.regexEscape(trims).join('|');
regex = '(' + trims + '|\s)+$';
regex = new RegExp(regex, 'g');
result = s.replace(regex, '');
return result;
};
/**
* Extended substring, support negative index (ordinal js substr(startIndex, endIndex))
*
* @param s {string}
* @param index {number} - if negative take string from the right similar to php substr()
* @param len {number=} - number of char to take starting from the index to the right (even when index is negative)
* @return {string}
*/
Str.subStr = function (s, index, len) {
if (s == undefined) {
return '';
}
len = len || 0;
if (Math.abs(index) > s.length) {
return s;
}
// regular substring
if (index > -1) {
if (len > 0 && (index + len) < s.length) {
return s.substring(index, index + len);
}
return s.substring(index);
}
// Negative index, take string from the right
// Index is negative => subStr ('hello', -3) => 'llo'
var start = s.length + index;
if (len > 0 && (start + len) < s.length) {
return s.substring(start, start + len);
}
return s.substring(start);
};
/**
* Count number of occurrences of an substring.
* @param s {string} - the big string
* @param sub {string} - the little string you want to find.
* @param caseSensitive {boolean=}
* @returns {number}
*/
Str.subCount = function (s, sub, caseSensitive) {
sub = Str.regexEscape(sub);
if (caseSensitive) {
return s.split(sub).length - 1;
}
return s.toLowerCase().split(sub.toLowerCase()).length - 1;
};
/**
* Concatenate count number of copies of s together and return result.
* @param s {string}
* @param count {number} - Number of times to repeat s
* @return {string}
*/
Str.repeat = function (s, count) {
var result = '', i;
for (i = 0; i < count; i++) {
result += s;
}
return result;
};
/**
* Pad left
*
* @param s {!string}
* @param padStr {!string} - the padding
* @param totalLength {!number} - the final length after padding
* @return {string}
*/
Str.padLeft = function (s, padStr, totalLength) {
return s.length >= totalLength ? s : Str.repeat(padStr, (totalLength - s.length) / padStr.length) + s;
};
/**
* Pad right
*
* @param s {string}
* @param padStr {string} - the padding
* @param totalLength {number} - the final length after padding
* @return {string}
*/
Str.padRight = function (s, padStr, totalLength) {
return s.length >= totalLength ? s : s + Str.repeat(padStr, (totalLength - s.length) / padStr.length);
};
/**
* Pad string based on the boolean value.
*
* @param s {string}
* @param padStr {string} - the padding
* @param totalLength {number} - the final length after padding
* @param padRight {boolean} - pad right if true, pad left otherwise
* @return {string}
*/
Str.pad = function (s, padStr, totalLength, padRight) {
if (padRight) {
return Str.padRight(s, padStr, totalLength);
}
return Str.padLeft(s, padStr, totalLength);
};
/**
* Strips any HTML tags from the specified string.
* @param s {string}
* @return {string}
*/
Str.stripTags = function (s) {
return s.replace(/<\/?[^>]+>/gi, '');
};
/**
* escapeHTML from Prototype-1.6.0.2 -- If it's good enough for Webkit and IE, it's good enough for Gecko!
* Converts HTML special characters to their entity equivalents.
*
* @param s {string}
* @return {string}
*/
Str.escapeHTML = function (s) {
s = s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return s;
};
/**
* unescapeHTML from Prototype-1.6.0.2 -- If it's good enough for Webkit and IE, it's good enough for Gecko!
* Strips tags and converts the entity forms of special HTML characters to their normal form.
*
* @param s {string}
* @return {string}
*/
Str.unescapeHTML = function (s) {
return Str.stripTags(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
};
/**
* Remove all Viet's accents and replace it with the latin based alphabet
* @param s {string}
* @return {string}
*/
Str.stripViet = function (s) {
/*
data = data.replace(/[àáâãăạảấầẩẫậắằẳẵặ]/g, 'a');
data = data.replace(/[òóôõơọỏốồổỗộớờởỡợ]/g, 'o');
data = data.replace(/[èéêẹẻẽếềểễệ]/g, 'e');
data = data.replace(/[ùúũưụủứừửữự]/g, 'u');
data = data.replace(/[ìíĩỉị]/g, 'i');
data = data.replace(/[ýỳỵỷỹ]/g, 'y');
data = data.replace(/[đðĐ]/g, 'd');
*/
if (Str.empty(s)) {
return s;
}
s = s.replace(/[\u00E0\u00E1\u00E2\u00E3\u0103\u1EA1\u1EA3\u1EA5\u1EA7\u1EA9\u1EAB\u1EAD\u1EAF\u1EB1\u1EB3\u1EB5\u1EB7]/g, 'a');
s = s.replace(/[\u00F2\u00F3\u00F4\u00F5\u01A1\u1ECD\u1ECF\u1ED1\u1ED3\u1ED5\u1ED7\u1ED9\u1EDB\u1EDD\u1EDF\u1EE1\u1EE3]/g, 'o');
s = s.replace(/[\u00E8\u00E9\u00EA\u1EB9\u1EBB\u1EBD\u1EBF\u1EC1\u1EC3\u1EC5\u1EC7]/g, 'e');
s = s.replace(/[\u00F9\u00FA\u0169\u01B0\u1EE5\u1EE7\u1EE9\u1EEB\u1EED\u1EEF\u1EF1]/g, 'u');
s = s.replace(/[\u00EC\u00ED\u0129\u1EC9\u1ECB]/g, 'i');
s = s.replace(/[\u00FD\u1EF3\u1EF5\u1EF7\u1EF9]/g, 'y');
s = s.replace(/[\u0111\u00F0\u0110]/g, 'd');
return s;
};
/**
* Use this to constructs multi lines string
*
* eg. Str.multiLines(true,
* 'hello',
* 'world'
* );
* returns: "hello\nworld"
*
* @param glue {string} - the separator between each line (eg. '\n', ', ' or ' ')
* @param args {...string} - each line
*/
Str.multiLines = function (glue, args) {
args = Array.prototype.splice.call(arguments, 1);
return args.join(glue);
};
/**
* Try to parse the json, if valid return the object else return defaultValue
*
* @param s {string} - json string
* @param defaultValue {boolean|object=} - if not specified defaultValue=false
* @returns {boolean|object}
*/
Str.parseJson = function (s, defaultValue) {
defaultValue = defaultValue === undefined ? false : defaultValue;
if (Str.empty(s)) {
return defaultValue;
}
try {
if (typeof s === 'string') {
return _parseJson(s);
}
// it already an object
return s;
}
catch (err) {
return defaultValue;
}
};
/**
* Escape the attribute, make sure it doesn't break the attribute select or to be use a an attribute.
* @param s {string} - the string
*/
Str.escapeAttribute = function (s) {
return s.replace(/"/g, '\\"');
};
/**
* Reverse the string.
*
* @param s
* @returns {*}
*/
Str.reverse = function (s) {
if (s) {
return s.split('').reverse().join('');
}
return s;
};
/**
* Get all the matched based on the specified group.
*
* @param s {string}
* @param regex {RegExp}
* @param index {Number} - the index of the match.
* @returns {Array}
*/
Str.matchAll = function (s, regex, index) {
var m, result = [];
index = index || 0;
if (!s) {
return [];
}
while (m = regex.exec(s)) {
result.push(m[index]);
}
return result;
};
/**
* Split the string into multiple smaller chunks.
*
* @param s
* @param chunkSize
* @returns {Array}
*/
Str.chop = function (s, chunkSize) {
var regex;
if (!s) {
return [];
}
regex = new RegExp('.{1,' + chunkSize + '}', 'g');
return s.match(regex);
};
function _getWords(s) {
s = s.replace(/(\w)([A-Z][a-z])/, '$1-$2');
s = s.replace(' ', '-');
s = s.replace('_', '-');
s = s.replace(/-+/g, '-');
return s.split('-')
}
/**
* Convert any string to camel case.
*
* @param s
*/
Str.toCamelCase = function (s) {
var words = _getWords(s), result = '', i, word;
for (i = 0; i < words.length; i++) {
word = words[i];
if (i == 0) {
result += word.toLowerCase();
}
else {
result += word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
}
}
return result;
};
/**
* Convert any string to title case.
*
* @param s
*/
Str.toTitleCase = function (s) {
var words = _getWords(s), result = '', i, word;
for (i = 0; i < words.length; i++) {
word = words[i];
result += word.charAt(0).toUpperCase() + word.substr(1).toLowerCase() + ' ';
}
return Str.trimEnd(result);
};
/**
* Convert any string to snake case.
*
* @param s
*/
Str.toSnakeCase = function (s) {
var words = _getWords(s), result = '', i, word;
for (i = 0; i < words.length; i++) {
word = words[i];
result += word.toLowerCase() + '_';
}
return Str.trimEnd(result, '_');
};
/**
* Convert any string to-kebab-case.
*
* @param s
*/
Str.toKebabCase = function (s) {
var words = _getWords(s), result = '', i, word;
for (i = 0; i < words.length; i++) {
word = words[i];
result += word.toLowerCase() + '-';
}
return Str.trimEnd(result, '-');
};
}(window, window._Str = {}));