-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.ts
96 lines (85 loc) · 3.38 KB
/
text.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
import { ObjectUtils } from "./object";
export interface ITextUtils {
formatText(template: string, ...values: string[]): string;
formatTextByKey(template: string, values: Record<string, any>): string;
isString: (value: any) => boolean;
stripeHTML: (value: string) => string;
escapeRegExp: (value: string) => string;
}
export const TextUtils: ITextUtils = {
formatText(template: string, ...params: Array<string | number>): string {
let result = template;
const placeholders = template.match(/\{\d\}/g) as string[];
if (placeholders) {
placeholders.map(placeholder => {
const index = parseInt(placeholder.replace("{", "").replace("}", ""));
result = result.replace(placeholder, `${params[index]}`);
});
}
return result;
},
formatTextByKey(template: string, params: Record<string, any>): string {
let result = template;
let placeholders = template.match(/\:\w+/g) as string[];
if (placeholders) {
placeholders.map(placeholder => {
const key = placeholder.replace(":", "");
result = result.replace(placeholder, ObjectUtils.get(params, key, placeholder));
});
} else {
placeholders = template.match(/{\w+}/g) as string[];
placeholders.map(placeholder => {
const key = placeholder.replace("{", "").replace("}", "");
result = result.replace(placeholder, `${ObjectUtils.get(params, key, `{${key}}`)}`);
});
}
return result;
},
isString: (value: any) => {
return typeof value === "string" || value instanceof String;
},
stripeHTML(value) {
if (!this.isString(value)) {
return value;
}
const htmlUnescapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
};
const reEscapedHtml = /&(?:amp|lt|gt|quot|#(0+)?39);/g;
const reHasEscapedHtml = RegExp(reEscapedHtml.source);
value = reHasEscapedHtml.test(value)
? value.replace(reEscapedHtml, (entity) => htmlUnescapes[entity] || "'")
: value || '';
return value.replace(/<(\/)?\w+(\s[\w-]+(=\".*")?)?>/gi, '');
},
escapeRegExp(value) {
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
const reHasRegExpChar = RegExp(reRegExpChar.source);
return value && reHasRegExpChar.test(value)
? value.replace(reRegExpChar, '\\$&')
: value || '';
}
};
String.prototype.splice = function (idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
String.prototype.replaceJSX = function (find, replace) {
return this.split(find).flatMap((item) => [item, replace]).slice(0, -1);
};
String.prototype.removeNewline = function () {
return this.replace(/(\r\n|\n|\r)/gm, "");
};
// return true if string is "1", "true", "TRUE" else if string is "0", "false", "FALSE" return false
String.prototype.toBoolean = function () {
if (!TextUtils.isString(this)) {
return false;
}
return this.toLowerCase() === "1" || this.toLowerCase() === "true";
};
String.prototype.toNumber = function () {
return Number(this);
};