@followupus/common 0.9.200 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/format.d.ts +4 -0
- package/dist/utils/format.js +60 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function filterHtmlTags(htmlSource: string): string;
|
|
2
|
+
export declare function getTextValue(variable: Record<string, string>): string;
|
|
3
|
+
export declare function truncateString(str: string, maxLength: number): string;
|
|
4
|
+
export declare function formatTemplate(text: string, rawVariables: Record<string, Record<string, string> | string>, maxLength?: number): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export function filterHtmlTags(htmlSource) {
|
|
2
|
+
return htmlSource.replace(/<[^>]*>/g, "").replace(/ /g, " ");
|
|
3
|
+
}
|
|
4
|
+
export function getTextValue(variable) {
|
|
5
|
+
if (typeof variable !== "object") {
|
|
6
|
+
return filterHtmlTags(variable);
|
|
7
|
+
}
|
|
8
|
+
return getNotNullValue([
|
|
9
|
+
"displayName",
|
|
10
|
+
"displayname",
|
|
11
|
+
"userName",
|
|
12
|
+
"targetName",
|
|
13
|
+
"userName",
|
|
14
|
+
"name",
|
|
15
|
+
"email",
|
|
16
|
+
], variable);
|
|
17
|
+
}
|
|
18
|
+
function getNotNullValue(varNames, variables) {
|
|
19
|
+
for (const varName of varNames) {
|
|
20
|
+
if (varName in variables && variables[varName]) {
|
|
21
|
+
return variables[varName];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
function keysToLowerCase(obj) {
|
|
27
|
+
const newObj = {};
|
|
28
|
+
for (const key in obj) {
|
|
29
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
30
|
+
newObj[key.toLowerCase()] = obj[key];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return newObj;
|
|
34
|
+
}
|
|
35
|
+
export function truncateString(str, maxLength) {
|
|
36
|
+
if (str?.length > maxLength) {
|
|
37
|
+
return str.slice(0, maxLength) + "...";
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return str;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//** case insensitive
|
|
44
|
+
//** {Who} can be replaced by variable {who:{name:ttt}}
|
|
45
|
+
export function formatTemplate(text, rawVariables, maxLength = Number.MAX_SAFE_INTEGER) {
|
|
46
|
+
const matches = text?.match(/{([^}]+)}/g);
|
|
47
|
+
if (matches) {
|
|
48
|
+
const formatVariables = keysToLowerCase(rawVariables);
|
|
49
|
+
let finalStr = text || "";
|
|
50
|
+
matches.forEach((match) => {
|
|
51
|
+
const variableKey = match.slice(1, match.length - 1)?.toLowerCase();
|
|
52
|
+
if (Object.keys(formatVariables).includes(variableKey)) {
|
|
53
|
+
const variable = formatVariables[variableKey] || {};
|
|
54
|
+
finalStr = finalStr.replace(match, typeof variable === "string" ? variable : getTextValue(variable));
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return truncateString(finalStr, maxLength);
|
|
58
|
+
}
|
|
59
|
+
return truncateString(text, maxLength);
|
|
60
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED