-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathstr.js
1098 lines (995 loc) · 32.4 KB
/
str.js
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable no-control-regex */
import _ from 'underscore';
import {AllHtmlEntities} from 'html-entities';
import replaceAll from 'string.prototype.replaceall';
import {CONST} from './CONST';
const REMOVE_SMS_DOMAIN_PATTERN = new RegExp(`@${CONST.SMS.DOMAIN}`, 'gi');
const Str = {
/**
* Return true if the string is ending with the provided suffix
*
* @param {String} str String ot search in
* @param {String} suffix What to look for
* @return {Boolean}
*/
endsWith(str, suffix) {
if (!str || !suffix) {
return false;
}
return str.substr(-suffix.length) === suffix;
},
/**
* Converts a USD string into th number of cents it represents.
*
* @param {String} amountStr A string representing a USD value.
* @param {Boolean} allowFraction Flag indicating if fractions of cents should be
* allowed in the output.
*
* @return {Number} The cent value of the @p amountStr.
*/
fromUSDToNumber(amountStr, allowFraction) {
let amount = String(amountStr).replace(/[^\d.\-()]+/g, '');
if (amount.match(/\(.*\)/)) {
const modifiedAmount = amount.replace(/[()]/g, '');
amount = `-${modifiedAmount}`;
}
amount = Number(amount) * 100;
// We round it here to a precision of 3 because some floating point numbers, when multiplied by 100
// don't give us a very pretty result. Try this in the JS console:
// 0.678 * 100
// 67.80000000000001
// 0.679 * 100
// 67.9
amount = Math.round(amount * 1e3) / 1e3;
return allowFraction ? amount : Math.round(amount);
},
/**
* Truncates the middle section of a string based on the max allowed length
* @param {string} fullStr
* @param {int} maxLength
* @returns {string}
*/
truncateInMiddle(fullStr, maxLength) {
if (fullStr.length <= maxLength) {
return fullStr;
}
const separator = '...';
const halfLengthToShow = (maxLength - separator.length) / 2;
const beginning = fullStr.substr(0, Math.ceil(halfLengthToShow));
const end = fullStr.substr(fullStr.length - Math.floor(halfLengthToShow));
return beginning + separator + end;
},
/**
* Convert new line to <br />
*
* @param {String} str
* @returns {string}
*/
nl2br(str) {
return str.replace(/\n/g, '<br />');
},
/**
* Decodes the given HTML encoded string.
*
* @param {String} s The string to decode.
* @return {String} The decoded string.
*/
htmlDecode(s) {
// Use jQuery if it exists or else use html-entities
if (typeof jQuery !== 'undefined') {
return jQuery('<textarea/>').html(s).text();
}
return AllHtmlEntities.decode(s);
},
/**
* HTML encodes the given string.
*
* @param {String} s The string to encode.
* @return {String} @p s HTML encoded.
*/
htmlEncode(s) {
// Use jQuery if it exists or else use html-entities
if (typeof jQuery !== 'undefined') {
return jQuery('<textarea/>').text(s).html();
}
return AllHtmlEntities.encode(s);
},
/**
* Escape text while preventing any sort of double escape, so 'X & Y' -> 'X & Y' and 'X & Y' -> 'X & Y'
*
* @param {String} s the string to escape
* @return {String} the escaped string
*/
safeEscape(s) {
return _.escape(_.unescape(s));
},
/**
* HTML encoding insensitive equals.
*
* @param {String} first string to compare
* @param {String} second string to compare
* @return {Boolean} true when first === second, ignoring HTML encoding
*/
htmlEncodingInsensitiveEquals(first, second) {
return first === second
|| this.htmlDecode(first) === second
|| this.htmlEncode(first) === second;
},
/**
* Creates an ID that can be used as an HTML attribute from @p str.
*
* @param {String} str A string to create an ID from.
* @return {String} The ID string made from @p str.
*/
makeID(str) {
const modifiedString = String(str).replace(/[^A-Za-z0-9]/g, '_').toUpperCase();
return `id_${modifiedString}`;
},
/**
* Extracts an ID made with Str.makeID from a larger string.
*
* @param {String} str A string containing an id made with Str.makeID
* @return {String|null} The ID string.
*/
extractID(str) {
const matches = str.match(/id[A-Z0-9_]+/);
return matches.length > 0 ? matches[0] : null;
},
/**
* Modifies the string so the first letter of each word is capitalized and the
* rest lowercased.
*
* @param {String} val The string to modify
* @return {String}
*/
recapitalize(val) {
// First replace every letter with its lowercase equivalent
// Cast to string.
let str = String(val);
if (str.length <= 0) {
return str;
}
str = str.substr(0, 1).toUpperCase() + str.substr(1).toLowerCase();
function recap_callback(t, a, b) {
return a + b.toUpperCase();
}
return str.replace(
// **NOTE: Match to _libfop.php
new RegExp('([^A-Za-z\'.0-9])([a-z])', 'g'),
recap_callback,
);
},
/**
* Replace all the non alphanumerical character by _
*
* @param {String} input
* @returns {String}
*/
sanitizeToAlphaNumeric(input) {
return String(input).replace(/[^\d\w]/g, '_');
},
/**
* Strip out all the non numerical characters
*
* @param {String} input
* @returns {String}
*/
stripNonNumeric(input) {
return String(input).replace(/[^\d]/g, '');
},
/**
* Strips all non ascii characters from a string
* @param {String} input
* @returns {String} The ascii version of the string.
*/
stripNonASCIICharacters(input) {
return String(input).replace(/[\u0000-\u0019\u0080-\uffff]/g, '');
},
/**
* Shortens the @p text to @p length and appends an ellipses to it.
*
* The ellipses will only be appended if @p text is longer than the @p length
* given.
*
* @param {String} val The string to reduce in size.
* @param {Number} length The maximal length desired.
* @return {String} The shortened @p text.
*/
shortenText(val, length) {
// Remove extra spaces because they don't show up in html anyway.
const text = String(val).replace(/\s+/g, ' ');
const truncatedText = text.substr(0, length - 3);
return (text.length > length ? `${truncatedText}...` : text);
},
/**
* Returns the byte size of a character
* @param {String} inputChar You can input more than one character, but it will only return the size of the first
* one.
* @returns {Number} Byte size of the character
*/
getRawByteSize(inputChar) {
const onlyChar = String(inputChar);
const c = onlyChar.charCodeAt();
// If we are grabbing the byte size, we need to temporarily diable no-bitwise for linting
/* eslint-disable no-bitwise */
if (c < (1 << 7)) {
return 1;
}
if (c < (1 << 11)) {
return 2;
}
if (c < (1 << 16)) {
return 3;
}
if (c < (1 << 21)) {
return 4;
}
if (c < (1 << 26)) {
return 5;
}
if (c < (1 << 31)) {
return 6;
}
/* eslint-enable no-bitwise */
return Number.NaN;
},
/**
* Gets the length of a string in bytes, including non-ASCII characters
* @param {String} input
* @returns {Number} The number of bytes used by string
*/
getByteLength(input) {
// Force string type
const stringInput = String(input);
let byteLength = 0;
for (let i = 0; i < stringInput.length; i++) {
byteLength += this.getRawByteSize(stringInput[i]);
}
return byteLength;
},
/**
* Shortens the input by max byte size instead of by character length
* @param {String} input
* @param {Number} maxSize The max size in bytes, e.g. 256
* @returns {String} Returns a shorted input if the input size exceeds the max
*/
shortenByByte(input, maxSize) {
const stringInput = String(input);
let totalByteLength = 0;
for (let i = 0; i < stringInput.length; i++) {
const charByteSize = this.getRawByteSize(stringInput[i]);
if ((charByteSize + totalByteLength) > maxSize) {
// If the next character exceeds the limit, stop and return the truncated string.
return `${stringInput.substr(0, i - 3)}...`;
}
totalByteLength += charByteSize;
}
return stringInput;
},
/**
* Returns true if the haystack begins with the needle
*
* @param {String} haystack The full string to be searched
* @param {String} needle The case-sensitive string to search for
* @return {Boolean} Retruns true if the haystack starts with the needle.
*/
startsWith(haystack, needle) {
return this.isString(haystack)
&& this.isString(needle)
&& haystack.substring(0, needle.length) === needle;
},
/**
* Gets the textual value of the given string.
*
* @param {String} str The string to fetch the text value from.
* @return {String} The text from within the HTML string.
*/
stripHTML(str) {
if (!this.isString(str)) {
return '';
}
return str.replace(/<[^>]*>?/gm, '');
},
/**
* Modifies the string so the first letter of the string is capitalized
*
* @param {String} str The string to modify.
* @return {String} The recapitalized string.
*/
UCFirst(str) {
return str.substr(0, 1).toUpperCase() + str.substr(1);
},
/**
* Returns a string containing all the characters str from the beginning
* of str to the first occurrence of substr.
* Example: Str.cutAfter( 'hello$%world', '$%' ) // returns 'hello'
*
* @param {String} str The string to modify.
* @param {String} substr The substring to search for.
* @return {String} The cut/trimmed string.
*/
cutAfter(str, substr) {
const index = str.indexOf(substr);
if (index !== -1) {
return str.substring(0, index);
}
return str;
},
/**
* Returns a string containing all the characters str from after the first
* occurrence of substr to the end of the string.
* Example: Str.cutBefore( 'hello$%world', '$%' ) // returns 'world'
*
* @param {String} str The string to modify.
* @param {String} substr The substring to search for.
* @return {String} The cut/trimmed string.
*/
cutBefore(str, substr) {
const index = str.indexOf(substr);
if (index !== -1) {
return str.substring(index + substr.length);
}
return str;
},
/**
* Checks that the string is a domain name (e.g. example.com)
*
* @param {String} string The string to check for domainnameness.
*
* @returns {Boolean} True iff the string is a domain name
*/
isValidDomainName(string) {
return Boolean(String(string).match(CONST.REG_EXP.DOMAIN));
},
/**
* Checks that the string is a valid url
*
* @param {String} string
*
* @returns {Boolean} True if the string is a valid hyperlink
*/
isValidURL(string) {
return Boolean(String(string).match(CONST.REG_EXP.HYPERLINK));
},
/**
* Checks that the string is an email address.
* NOTE: TLDs are not just 2-4 characters. Keep this in sync with _inputrules.php
*
* @param {String} string The string to check for email validity.
*
* @returns {Boolean} True iff the string is an email
*/
isValidEmail(string) {
return Boolean(String(string).match(CONST.REG_EXP.EMAIL));
},
/**
* Checks if the string is an valid email address formed during comment markdown formation.
*
* @param {String} string The string to check for email validity.
*
* @returns {Boolean} True if the string is an valid email created by comment markdown.
*/
isValidEmailMarkdown(string) {
return Boolean(String(string).match(`^${CONST.REG_EXP.MARKDOWN_EMAIL}$`));
},
/**
* Remove trailing comma from a string.
*
* @param {String} string The string with any trailing comma to be removed.
*
* @returns {String} string with the trailing comma removed
*/
removeTrailingComma(string) {
return string.trim().replace(/(,$)/g, '');
},
/**
* Checks that the string is a list of coma separated email addresss.
*
* @param {String} str The string to check for emails validity.
*
* @returns {Boolean} True if all emails are valid or if input is empty
*/
areValidEmails(str) {
const string = this.removeTrailingComma(str);
if (string === '') {
return true;
}
const emails = string.split(',');
let result = true;
for (let i = 0; i < emails.length; i += 1) {
if (!this.isValidEmail(emails[i].trim())) {
result = false;
}
}
return result;
},
/**
* Extract the email addresses from a string
*
* @param {String} string
* @returns {String[]|null}
*/
extractEmail(string) {
return String(string).match(CONST.REG_EXP.EMAIL_SEARCH);
},
/**
* Extracts the domain name from the given email address
* (e.g. "domain.com" for "[email protected]").
*
* @param {String} email The email address.
*
* @returns {String} The domain name in the email address.
*/
extractEmailDomain(email) {
return this.cutBefore(email, '@');
},
/**
* Tries to extract the company name from the given email address
* (e.g. "yelp" for "[email protected]").
*
* @param {String} email The email address.
*
* @returns {String} The company name in the email address or null.
*/
extractCompanyNameFromEmailDomain(email) {
const domain = this.extractEmailDomain(email);
if (!domain) {
return null;
}
const domainParts = domain.split('.');
if (!domainParts.length) {
return null;
}
return domainParts[0];
},
/**
* Extracts the local part from the given email address
* (e.g. "joe" for "[email protected]").
*
* @param {String} email The email address.
*
* @returns {String} The local part in the email address.
*/
extractEmailLocalPart(email) {
return this.cutAfter(email, '@');
},
/**
* Sanitize phone number to return only numbers. Return null if non valid phone number.
*
* @param {String} str
* @returns {String|null}
*/
sanitizePhoneNumber(str) {
const string = str.replace(/(?!^\+)\D/g, '');
return string.length <= 15 && string.length >= 10 ? string : null;
},
/**
* Sanitize email. Return null if non valid email.
*
* @param {String} str
* @returns {String|null}
*/
sanitizeEmail(str) {
const string = str.toLowerCase().trim();
return CONST.REG_EXP.EMAIL.test(string) ? string : null;
},
/**
* Escapes all special RegExp characters from a string
*
* @param {String} string The subject
*
* @returns {String} The escaped string
*/
escapeForRegExp(string) {
return string.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
},
/**
* Escapes all special RegExp characters from a string except for the period
*
* @param {String} string The subject
* @returns {String} The escaped string
*/
escapeForExpenseRule(string) {
return string.replace(/[-[\]/{}()*+?\\^$|]/g, '\\$&');
},
/**
* Adds a backslash in front of each of colon
* if they don't already have a backslash in front of them
*
* @param {String} string The subject
* @returns {String} The escaped string
*/
addBackslashBeforeColonsForTagNamesComingFromQBD(string) {
return string.replace(/([^\\]):/g, '$1\\:');
},
/**
* Removes backslashes from string
* eg: myString\[\]\* -> myString[]*
*
* @param {String} string
* @returns {String}
*/
stripBackslashes(string) {
return string.replace(/\\/g, '');
},
/**
* Checks if a string's length is in the specified range
*
* @param {String} string The subject
* @param {Number} minimumLength
* @param {Number} [maximumLength]
*
* @returns {Boolean} true if the length is in the range, false otherwise
*/
isOfLength(string, minimumLength, maximumLength) {
if (!this.isString(string)) {
return false;
}
if (string.length < minimumLength) {
return false;
}
if (!this.isUndefined(maximumLength) && string.length > maximumLength) {
return false;
}
return true;
},
/**
* Count the number of occurences of needle in haystack.
* This is faster than counting the results of haystack.match( /needle/g )
* via http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string
*
* @param {String} haystack The string to look inside of
* @param {String} needle What we're looking for
* @param {Boolean} allowOverlapping Defaults to false
*
* @returns {Integer} The number of times needle is in haystack.
*/
occurences(haystack, needle, allowOverlapping) {
let count = 0;
let pos = 0;
// Force strings for input
const haystackStr = String(haystack);
const needleStr = String(needle);
if (needleStr.length <= 0) {
return haystackStr.length + 1;
}
const step = (allowOverlapping) ? (1) : (needleStr.length);
while (pos >= 0) {
pos = haystackStr.indexOf(needleStr, pos);
if (pos >= 0) {
count += 1;
pos += step;
}
}
return (count);
},
/**
* Uppercases the first letter of each word
* via https://github.com/kvz/phpjs/blob/master/functions/strings/ucwords.js
*
* @param {String} str to uppercase words
* @returns {String} Uppercase worded string
*/
ucwords(str) {
return (String(str)).replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g,
$1 => $1.toUpperCase());
},
/**
* Returns true if the haystack contains the needle
*
* @param {String} haystack The full string to be searched
* @param {String} needle The case-sensitive string to search for
*
* @return {Boolean} Retruns true if the haystack contains the needle
*/
contains(haystack, needle) {
return haystack.indexOf(needle) !== -1;
},
/**
* Returns true if the haystack contains the needle, ignoring case
*
* @param {String} haystack The full string to be searched
* @param {String} needle The case-insensitive string to search for
*
* @return {Boolean} Retruns true if the haystack contains the needle, ignoring case
*/
caseInsensitiveContains(haystack, needle) {
return this.contains(haystack.toLowerCase(), needle.toLowerCase());
},
/**
* Case insensitive compare function
*
* @param {String} string1 string to compare
* @param {String} string2 string to compare
*
* @return {Number} -1 if first string < second string
* 1 if first string > second string
* 0 if first string = second string
*/
caseInsensitiveCompare(string1, string2) {
const lowerCase1 = string1.toLocaleLowerCase();
const lowerCase2 = string2.toLocaleLowerCase();
return this.compare(lowerCase1, lowerCase2);
},
/**
* Case insensitive equals
*
* @param {String} first string to compare
* @param {String} second string to compare
* @return {Boolean} true when first == second except for case
*/
caseInsensitiveEquals(first, second) {
return this.caseInsensitiveCompare(first, second) === 0;
},
/**
* Compare function
*
* @param {String} string1 string to compare
* @param {String} string2 string to compare
*
* @return {Number} -1 if first string < second string
* 1 if first string > second string
* 0 if first string = second string
*/
compare(string1, string2) {
if (string1 < string2) {
return -1;
} if (string1 > string2) {
return 1;
}
return 0;
},
/**
* Check if a file extension is supported by SmartReports
* @param {String} filename
* @return {Boolean}
*/
isFileExtensionSmartReportsValid(filename) {
// Allowed extensions. Make sure to keep them in sync with those defined
// in SmartReport_Utils::templateFileUploadCheck()
const allowedExtensions = ['xls', 'xlsx', 'xlsm', 'xltm'];
const extension = filename.split('.').pop().toLowerCase();
return allowedExtensions.indexOf(extension) > -1;
},
/**
* Mask Permanent Account Number (PAN) the same way Auth does
* @param {Number|String} number account number
* @return {String} masked account number
*/
maskPAN(number) {
// cast to string
const accountNumber = String(number);
const len = accountNumber.length;
// Hide these numbers completely
// We should not be getting account numbers this small or large
if (len < 6 || len > 20) {
return this.maskFirstNCharacters(accountNumber, len, 'X');
}
// Can show last 4
if (len < 14) {
return this.maskFirstNCharacters(accountNumber, len - 4, 'X');
}
// Can show first 6 and last 4
const first = accountNumber.substr(0, 6);
const last = accountNumber.substr(7);
const masked = this.maskFirstNCharacters(last, len - 11, 'X');
return `${first}${masked}`;
},
/**
* Checks if something is a string
* Stolen from underscore
* @param {Mixed} obj
* @return {Boolean}
*/
isString(obj) {
return this.isTypeOf(obj, 'String');
},
/**
* Checks if something is a number
* Stolen from underscore
* @param {Mixed} obj
* @return {Boolean}
*/
isNumber(obj) {
return this.isTypeOf(obj, 'Number');
},
/**
* Checks if something is a certain type
* Stolen from underscore
* @param {Mixed} obj
* @param {String} type one of ['Arguments', 'Function', 'String', 'Number', 'Date',
* 'RegExp', 'Error', 'Symbol', 'Map', 'WeakMap', 'Set', 'WeakSet']
* @return {Boolean}
*/
isTypeOf(obj, type) {
return Object.prototype.toString.call(obj) === `[object ${type}]`;
},
/**
* Checks to see if something is undefined
* Stolen from underscore
* @param {Mixed} obj
* @return {Boolean}
*/
isUndefined(obj) {
// eslint-disable-next-line no-void
return obj === void 0;
},
/**
* Replace first N characters of the string with maskChar
* eg: maskFirstNCharacters( '1234567890', 6, 'X' ) yields XXXXXX7890
* @param {String} str string to mask
* @param {Number} n number of characters we want to mask from the string
* @param {String} mask string we want replace the first N chars with
* @return {String} masked string
*/
maskFirstNCharacters(str, n, mask) {
// if str is empty, str or mask aren't strings,
// or n is not a number, do nothing
if (!this.isString(str) || !this.isString(mask)
|| str.length === 0 || !this.isNumber(n)) {
return str;
}
return str.substring(0, n).replace(/./g, mask) + str.substring(n);
},
/**
* Trim a string
*
* @param {String} str
* @returns {string}
*/
trim(str) {
return $.trim(str);
},
/**
* Convert a percentage string like '25%' to 25/
* @param {String} percentageString The percentage as a string
* @returns {Number}
*/
percentageStringToNumber(percentageString) {
return Number(this.cutAfter(percentageString, '%'));
},
/**
* Remoce all the spaces from a string
* @param {string} input
* @returns {string}
*/
removeSpaces(input) {
return String(input).replace(' ', '');
},
/**
* Returns the proper phrase depending on the count that is passed.
* Example:
* console.log(Str.pluralize('puppy', 'puppies', 1)); // puppy
* console.log(Str.pluralize('puppy', 'puppies', 3)); // puppies
*
* @param {String} singular form of the phrase
* @param {String} plural form of the phrase
* @param {Number} n the count which determines the plurality
*
* @return {String}
*/
pluralize(singular, plural, n) {
if (!n || n > 1) {
return plural;
}
return singular;
},
/**
* Returns whether or not a string is an encrypted number or not.
*
* @param {String} number that we want to see if its encrypted or not
*
* @return {Boolean} Whether or not this string is an encrpypted number
*/
isEncryptedCardNumber(number) {
// Older encrypted versioning.
if (/^[\da-fA-F]+$/.test(number)) {
return number.length % 32 === 0;
}
// Check with the new versioning.
if (/^[vV][\d]+:[\da-fA-F]+$/.test(number)) {
return number.split(':')[1].length % 32 === 0;
}
return false;
},
/**
* Converts a value to boolean, case-insensitive.
* @param {mixed} value
* @return {Boolean}
*/
toBool(value) {
if (this.isString(value)) {
return value.toLowerCase() === 'true';
}
return Boolean(value);
},
/**
* Checks if a string could be the masked version of another one.
*
* @param {String} first string to compare
* @param {String} second string to compare
* @param {String} [mask] defaults to X
* @return {Boolean} true when first could be the masked version of second
*/
maskedEquals(first, second, mask) {
const firsts = first.match(/.{1,1}/g);
const seconds = second.match(/.{1,1}/g);
const defaultMask = mask || 'X';
if (firsts.length !== seconds.length) {
return false;
}
for (let i = 0; i < firsts.length; i += 1) {
if (firsts[i] !== seconds[i]
&& firsts[i] !== defaultMask
&& seconds[i] !== defaultMask) {
return false;
}
}
return true;
},
/**
* Bold any word matching the regexp in the text.
* @param {string} text, htmlEncoded
* @param {RegExp} regexp
* @return {string}
*/
boldify(text, regexp) {
return text.replace(regexp, '<strong>$1</strong>');
},
/**
* Check for whether a phone number is valid.
* @param {String} phone
*
* @return {bool}
*/
isValidPhone(phone) {
return CONST.SMS.E164_REGEX.test(phone);
},
/**
* We validate mentions by checking if it's first character is an allowed character
* and by checking that we make sure it isn't inside other tags where mentions aren't allowed.
* For example, *@[email protected]* is a valid mention because we allow bold styling for it,
* but `@[email protected]` is not because we do not allow mentions within code
*
* @param {String} mention
* @returns {bool}
*/
isValidMention(mention) {
// A valid mention starts with a space, *, _, #, ', ", or @ (with no preceding characters).
const startsWithValidChar = /[\s*~_#'"@]/g.test(mention.charAt(0));
// We don't support mention inside code or codefence styling,
// for example using `@[email protected]` or ```@[email protected]``` will be invalid.
const containsInvalidTag = /(<code>|<pre>|`)/g.test(mention);
return startsWithValidChar && !containsInvalidTag;
},
/**
* Returns text without our SMS domain
*
* @param {String} text
* @return {String}
*/
removeSMSDomain(text) {
return text.replace(REMOVE_SMS_DOMAIN_PATTERN, '');
},
/**
* Returns true if the text is a valid phone number with our SMS domain removed
*
* @param {String} text
* @return {String}
*/
isSMSLogin(text) {
return this.isValidPhone(this.removeSMSDomain(text));
},
/**
* This method will return all matches of a single regex like preg_match_all() in PHP. This is not a common part of
* JS yet, so this is a good way of doing it according to
* https://github.com/airbnb/javascript/issues/1439#issuecomment-306297399 and doesn't get us in trouble with
* linting rules.
*
* @param {String} str
* @param {RegExp} regex
*
* @returns {Array}
*/
matchAll(str, regex) {
const matches = [];
str.replace(regex, (...args) => {
const match = Array.prototype.slice.call(args, 0, -2);
match.input = args[args.length - 1];
match.index = args[args.length - 2];
matches.push(match);
});
return matches;
},
/**
* A simple GUID generator taken from https://stackoverflow.com/a/32760401/9114791