-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathstr.ts
1071 lines (957 loc) · 33 KB
/
str.ts
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
'worklet';
/* eslint-disable no-control-regex */
import {parsePhoneNumber} from 'awesome-phonenumber';
import * as HtmlEntities from 'html-entities';
import * as Constants from './CONST';
import * as UrlPatterns from './Url';
import * as Utils from './utils';
import Log from './Log';
const REMOVE_SMS_DOMAIN_PATTERN = /@expensify\.sms/gi;
/**
* Checks if parameter is a string or function
* if it is a function then we will call it with
* any additional arguments.
*/
function resultFn(parameter: string): string;
function resultFn<R, A extends unknown[]>(parameter: (...args: A) => R, ...args: A): R;
function resultFn<R, A extends unknown[]>(parameter: string | ((...a: A) => R), ...args: A): string | R {
if (typeof parameter === 'function') {
return parameter(...args);
}
return parameter;
}
const Str = {
/**
* Return true if the string is ending with the provided suffix
*
* @param str String ot search in
* @param suffix What to look for
*/
endsWith(str: string, suffix: string): boolean {
if (!str || !suffix) {
return false;
}
return str.substr(-suffix.length) === suffix;
},
/**
* Converts a USD string into th number of cents it represents.
*
* @param amountStr A string representing a USD value.
* @param allowFraction Flag indicating if fractions of cents should be
* allowed in the output.
*
* @returns The cent value of the @p amountStr.
*/
fromUSDToNumber(amountStr: string, allowFraction: boolean): number {
let amount: string | number = String(amountStr).replace(/[^\d.\-()]+/g, '');
if (amount.match(/\(.*\)/)) {
const modifiedAmount = amount.replace(/[()]/g, '');
amount = `-${modifiedAmount}`;
}
amount = Number(amount) * 100;
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
*/
truncateInMiddle(fullStr: string, maxLength: number): string {
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 />
*/
nl2br(str: string): string {
return str.replace(/\n/g, '<br />');
},
/**
* Decodes the given HTML encoded string.
*
* @param s The string to decode.
* @returns The decoded string.
*/
htmlDecode(s: string) {
return HtmlEntities.decode(s);
},
/**
* HTML encodes the given string.
*
* @param s The string to encode.
* @return string @p s HTML encoded.
*/
htmlEncode(s: string) {
return HtmlEntities.encode(s);
},
/**
* Escape text while preventing any sort of double escape, so 'X & Y' -> 'X & Y' and 'X & Y' -> 'X & Y'
*
* @param s The string to escape
* @returns The escaped string
*/
safeEscape(s: string) {
return Utils.escapeText(Utils.unescapeText(s));
},
/**
* HTML encoding insensitive equals.
*
* @param first string to compare
* @param second string to compare
* @returns True when first === second, ignoring HTML encoding
*/
htmlEncodingInsensitiveEquals(first: string, second: string): boolean {
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 str A string to create an ID from.
* @returns The ID string made from @p str.
*/
makeID(str: string): string {
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 str A string containing an id made with Str.makeID
* @returns The ID string.
*/
extractID(str: string): string | null {
const matches = str.match(/id[A-Z0-9_]+/);
return matches && matches.length > 0 ? matches[0] : null;
},
/**
* Modifies the string so the first letter of each word is capitalized and the
* rest lowercased.
*
* @param val The string to modify
*/
recapitalize(val: string): string {
// 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: unknown, a: string, b: string) {
return a + b.toUpperCase();
}
return str.replace(
// **NOTE: Match to _libfop.php
/([^A-Za-z'.0-9])([a-z])/g,
recap_callback,
);
},
/**
* Replace all the non alphanumerical character by _
*/
sanitizeToAlphaNumeric(input: string): string {
return String(input).replace(/[^\d\w]/g, '_');
},
/**
* Strip out all the non numerical characters
*/
stripNonNumeric(input: string): string {
return String(input).replace(/[^\d]/g, '');
},
/**
* Strips all non ascii characters from a string
* @returns The ascii version of the string.
*/
stripNonASCIICharacters(input: string): string {
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 val The string to reduce in size.
* @param length The maximal length desired.
* @returns The shortened @p text.
*/
shortenText(val: string, length: number): string {
// 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 inputChar You can input more than one character, but it will only return the size of the first
* one.
* @returns Byte size of the character
*/
getRawByteSize(inputChar: string): number {
const onlyChar = String(inputChar);
const c = onlyChar.charCodeAt(0);
// 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
* @returns The number of bytes used by string
*/
getByteLength(input: string): number {
// Force string type
const stringInput = String(input);
const byteLength = Array.from(stringInput).reduce((acc, char) => acc + this.getRawByteSize(char), 0);
return byteLength;
},
/**
* Shortens the input by max byte size instead of by character length
* @param maxSize The max size in bytes, e.g. 256
* @returns Returns a shorted input if the input size exceeds the max
*/
shortenByByte(input: string, maxSize: number): string {
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 haystack The full string to be searched
* @param needle The case-sensitive string to search for
* @returns True if the haystack starts with the needle.
*/
startsWith(haystack: string, needle: string): boolean {
return this.isString(haystack) && this.isString(needle) && haystack.substring(0, needle.length) === needle;
},
/**
* Gets the textual value of the given string.
*
* @param str The string to fetch the text value from.
* @returns The text from within the HTML string.
*/
stripHTML(str: string): string {
if (!this.isString(str)) {
return '';
}
return str.replace(/<[^>]*>?/gm, '');
},
/**
* Modifies the string so the first letter of the string is capitalized
*
* @param str The string to modify.
* @returns The recapitalized string.
*/
UCFirst(str: string): string {
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 str The string to modify.
* @param substr The substring to search for.
* @returns The cut/trimmed string.
*/
cutAfter(str: string, substr: string): string {
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 str The string to modify.
* @param substr The substring to search for.
* @returns The cut/trimmed string.
*/
cutBefore(str: string, substr: string): string {
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 str The string to check for domainnameness.
*
* @returns True if the string is a domain name
*/
isValidDomainName(str: string): boolean {
return !!String(str).match(Constants.CONST.REG_EXP.DOMAIN);
},
/**
* Checks that the string is a valid url
*
* @returns True if the string is a valid hyperlink
*/
isValidURL(str: string): boolean {
return !!String(str).match(Constants.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 str The string to check for email validity.
*
* @returns True if the string is an email
*/
isValidEmail(str: string): boolean {
return !!String(str).match(Constants.CONST.REG_EXP.EMAIL);
},
/**
* Checks if the string is an valid email address formed during comment markdown formation.
*
* @param str The string to check for email validity.
*
* @returns True if the string is an valid email created by comment markdown.
*/
isValidEmailMarkdown(str: string): boolean {
return !!String(str).match(`^${Constants.CONST.REG_EXP.MARKDOWN_EMAIL}$`);
},
/**
* Remove trailing comma from a string.
*
* @param str The string with any trailing comma to be removed.
*
* @returns string with the trailing comma removed
*/
removeTrailingComma(str: string): string {
return str.trim().replace(/(,$)/g, '');
},
/**
* Checks that the string is a list of coma separated email addresss.
*
* @param str The string to check for emails validity.
*
* @returns True if all emails are valid or if input is empty
*/
areValidEmails(str: string): boolean {
const string = this.removeTrailingComma(str);
if (string === '') {
return true;
}
const emails = string.split(',');
const result = emails.every((email) => this.isValidEmail(email.trim()));
return result;
},
/**
* Extract the email addresses from a string
*/
extractEmail(str: string): RegExpMatchArray | null {
return String(str).match(Constants.CONST.REG_EXP.EMAIL_SEARCH);
},
/**
* Extracts the domain name from the given email address
* (e.g. "domain.com" for "joe@domain.com").
*
* @param email The email address.
*
* @returns The domain name in the email address.
*/
extractEmailDomain(email: string): string {
return this.cutBefore(email, '@');
},
/**
* Tries to extract the company name from the given email address
* (e.g. "yelp" for "joe@yelp.co.uk").
*
* @param email The email address.
*
* @returns The company name in the email address or null.
*/
extractCompanyNameFromEmailDomain(email: string): string | null {
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 "joe@domain.com").
*
* @param email The email address.
*
* @returns The local part in the email address.
*/
extractEmailLocalPart(email: string): string {
return this.cutAfter(email, '@');
},
/**
* Sanitize phone number to return only numbers. Return null if non valid phone number.
*/
sanitizePhoneNumber(str: string): string | null {
const string = str.replace(/(?!^\+)\D/g, '');
return string.length <= 15 && string.length >= 10 ? string : null;
},
/**
* Sanitize email. Return null if non valid email.
*/
sanitizeEmail(str: string): string | null {
const string = str.toLowerCase().trim();
return Constants.CONST.REG_EXP.EMAIL.test(string) ? string : null;
},
/**
* Escapes all special RegExp characters from a string
*
* @param str The subject
*
* @returns The escaped string
*/
escapeForRegExp(str: string): string {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
},
/**
* Escapes all special RegExp characters from a string except for the period
*
* @param str The subject
* @returns The escaped string
*/
escapeForExpenseRule(str: string): string {
return str.replace(/[-[\]/{}()*+?\\^$|]/g, '\\$&');
},
/**
* Adds a backslash in front of each of colon
* if they don't already have a backslash in front of them
*
* @param str The subject
* @returns The escaped string
*/
addBackslashBeforeColonsForTagNamesComingFromQBD(str: string): string {
return str.replace(/([^\\]):/g, '$1\\:');
},
/**
* Removes backslashes from string
* eg: myString\[\]\* -> myString[]*
*/
stripBackslashes(str: string): string {
return str.replace(/\\/g, '');
},
/**
* Checks if a string's length is in the specified range
*
* @returns true if the length is in the range, false otherwise
*/
isOfLength(str: string, minimumLength: number, maximumLength: number): boolean {
if (!this.isString(str)) {
return false;
}
if (str.length < minimumLength) {
return false;
}
if (!this.isUndefined(maximumLength) && str.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 haystack The string to look inside of
* @param needle What we're looking for
* @param allowOverlapping Defaults to false
*
* @returns The number of times needle is in haystack.
*/
occurences(haystack: string, needle: string, allowOverlapping: boolean): number {
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 str to uppercase words
* @returns Uppercase worded string
*/
ucwords(str: string): string {
const capitalize = ($1: string) => $1.toUpperCase();
return String(str).replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, capitalize);
},
/**
* Returns true if the haystack contains the needle
*
* @param haystack The full string to be searched
* @param needle The case-sensitive string to search for
*
* @returns Returns true if the haystack contains the needle
*/
contains(haystack: string, needle: string): boolean {
return haystack.indexOf(needle) !== -1;
},
/**
* Returns true if the haystack contains the needle, ignoring case
*
* @param haystack The full string to be searched
* @param needle The case-insensitive string to search for
*
* @returns Returns true if the haystack contains the needle, ignoring case
*/
caseInsensitiveContains(haystack: string, needle: string): boolean {
return this.contains(haystack.toLowerCase(), needle.toLowerCase());
},
/**
* Case insensitive compare function
*
* @param strA string to compare
* @param strB string to compare
*
* @returns -1 if first string < second string
* 1 if first string > second string
* 0 if first string = second string
*/
caseInsensitiveCompare(strA: string, strB: string): 1 | 0 | -1 {
const lowerCaseStrA = strA.toLocaleLowerCase();
const lowerCaseStrB = strB.toLocaleLowerCase();
return this.compare(lowerCaseStrA, lowerCaseStrB);
},
/**
* Case insensitive equals
*
* @param strA string to compare
* @param strB string to compare
* @returns true when first == second except for case
*/
caseInsensitiveEquals(strA: string, strB: string): boolean {
return this.caseInsensitiveCompare(strA, strB) === 0;
},
/**
* Compare function
*
* @param strA string to compare
* @param strB string to compare
*
* @returns -1 if first string < second string
* 1 if first string > second string
* 0 if first string = second string
*/
compare(strA: string, strB: string): 1 | 0 | -1 {
if (strA < strB) {
return -1;
}
if (strA > strB) {
return 1;
}
return 0;
},
/**
* Check if a file extension is supported by SmartReports
*/
isFileExtensionSmartReportsValid(filename: string): boolean {
// 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 !!extension && allowedExtensions.indexOf(extension) > -1;
},
/**
* Mask Permanent Account Number (PAN) the same way Auth does
* @param num account number
* @returns masked account number
*/
maskPAN(num: number | string): string {
// cast to string
const accountNumber = String(num);
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
*/
isString(obj: unknown): obj is string {
return this.isTypeOf(obj, 'String');
},
/**
* Checks if something is a number
* Stolen from underscore
* @param obj
*/
isNumber(obj: unknown): obj is number {
return this.isTypeOf(obj, 'Number');
},
/**
* Checks if something is a certain type
* Stolen from underscore
*/
isTypeOf(obj: unknown, type: 'Arguments' | 'Function' | 'String' | 'Number' | 'Date' | 'RegExp' | 'Error' | 'Symbol' | 'Map' | 'WeakMap' | 'Set' | 'WeakSet'): boolean {
return Object.prototype.toString.call(obj) === `[object ${type}]`;
},
/**
* Checks to see if something is undefined
* Stolen from underscore
*/
isUndefined(obj: unknown): boolean {
// 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 str String to mask
* @param num Number of characters we want to mask from the string
* @param mask String we want replace the first N chars with
* @returns Masked string
*/
maskFirstNCharacters(str: string, num: number, mask: string): string {
// 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(num)) {
return str;
}
return str.substring(0, num).replace(/./g, mask) + str.substring(num);
},
/**
* Trim a string
*/
trim(str: string) {
return str.trim();
},
/**
* Convert a percentage string like '25%' to 25/
* @param percentageString The percentage as a string
*/
percentageStringToNumber(percentageString: string): number {
return Number(this.cutAfter(percentageString, '%'));
},
/**
* Remove all the spaces from a string
*/
removeSpaces(input: string): string {
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 singular form of the phrase
* @param plural form of the phrase
* @param num the count which determines the plurality
*/
pluralize(singular: string, plural: string, num: number): string {
if (!num || num > 1) {
return plural;
}
return singular;
},
/**
* Returns whether or not a string is an encrypted number or not.
*
* @param num that we want to see if its encrypted or not
*
* @returns Whether or not this string is an encrpypted number
*/
isEncryptedCardNumber(num: string): boolean {
// Older encrypted versioning.
if (/^[\da-fA-F]+$/.test(num)) {
return num.length % 32 === 0;
}
// Check with the new versioning.
if (/^[vV][\d]+:[\da-fA-F]+$/.test(num)) {
return num.split(':')[1].length % 32 === 0;
}
return false;
},
/**
* Converts a value to boolean, case-insensitive.
*/
toBool(value: unknown): boolean {
if (this.isString(value)) {
return value.toLowerCase() === 'true';
}
return !!value;
},
/**
* Checks if a string could be the masked version of another one.
*
* @param strA String to compare
* @param strB String to compare
* @param [mask] Defaults to X
* @returns True when first could be the masked version of second
*/
maskedEquals(strA: string, strB: string, mask: string): boolean {
const firsts = strA.match(/.{1,1}/g) ?? [];
const seconds = strB.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.
*/
boldify(text: string, regexp: RegExp): string {
return text.replace(regexp, '<strong>$1</strong>');
},
/**
* Check for whether a phone number is valid.
* @deprecated use isValidE164Phone to validate E.164 phone numbers or isValidPhoneFormat to validate phone numbers in general
*/
isValidPhone(phone: string): boolean {
return Constants.CONST.SMS.E164_REGEX.test(phone);
},
/**
* Check for whether a phone number is valid.
*/
isValidPhoneNumber(phone: string): boolean {
return parsePhoneNumber(phone).possible;
},
/**
* Check for whether a phone number is valid according to E.164 standard.
*/
isValidE164Phone(phone: string): boolean {
return Constants.CONST.SMS.E164_REGEX.test(phone);
},
/**
* Check for whether a phone number is valid in different formats/standards. For example:
* significant: 4404589784
* international: +1 440-458-9784
* e164: +14404589784
* national: (440) 458-9784
* 123.456.7890
*/
isValidPhoneFormat(phone: string): boolean {
return Constants.CONST.REG_EXP.GENERAL_PHONE_PART.test(phone);
},
/**
* We validate mentions by checking if it's first character is an allowed character.
*/
isValidMention(mention: string): boolean {
// Mentions can start @ proceeded by a space, eg "ping @user@domain.tld"
if (/[\s@]/g.test(mention.charAt(0))) {
return true;
}
// Mentions can also start and end with a *, _, ~, ', or " (with no other preceding characters)
// eg "ping *@user@domain.tld*"
const firstChar = mention.charAt(0);
const lastChar = mention.charAt(mention.length - 1);
return /[*~_'"]/g.test(firstChar) && /[*~_'"]/g.test(lastChar) && firstChar === lastChar;
},
/**
* Returns text without our SMS domain
*/
removeSMSDomain(text: string): string {
return text.replace(REMOVE_SMS_DOMAIN_PATTERN, '');
},
/**
* Returns true if the text is a valid E.164 phone number with our SMS domain removed
*/
isSMSLogin(text: string): boolean {
return this.isValidE164Phone(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.
*/
matchAll(str: string, regex: RegExp): Array<RegExpMatchArray & {input: string; index: number}> {
const matches: Array<RegExpMatchArray & {input: string; index: number}> = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const collectMatches = (...args: any[]) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const match: any = Array.prototype.slice.call(args, 0, -2);
match.input = args[args.length - 1];
match.index = args[args.length - 2];
matches.push(match);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
str.replace(regex, collectMatches as any);
return matches;
},
/**
* A simple GUID generator taken from https://stackoverflow.com/a/32760401/9114791
*
* @param [prefix] an optional prefix to put in front of the guid
*/
guid(prefix = ''): string {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return `${prefix}${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
},
/**
* Takes in a URL and returns it with a leading '/'
*
* @param url The URL to be formatted
* @returns The formatted URL
*/
normalizeUrl(url: string): string {
return typeof url === 'string' && url.startsWith('/') ? url : `/${url}`;
},
/**
* Formats a URL by converting the domain name to lowercase and adding the missing 'https://' protocol.
*
* @param url The URL to be formatted
* @param defaultScheme The Scheme to use in the URL
* @returns The formatted URL
*/
sanitizeURL(url: string, defaultScheme = 'https'): string {
const regex = new RegExp(`^${UrlPatterns.URL_REGEX}$`, 'i');
const match = regex.exec(url);
if (!match) {
return url;
}
const website = match[3] ? match[2] : `${defaultScheme}://${match[2]}`;
return website.toLowerCase() + this.cutBefore(match[1], match[2]);
},
/**
* Checks if parameter is a string or function
* if it is a function then we will call it with
* any additional arguments.
*/
result: resultFn,
/**
* Get file extension for a given url with or
* without query parameters
*/
getExtension(url: string): string | undefined {
if (typeof url !== 'string') {
Log.warn('Str.getExtension: url is not a string', {url});