39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import _ from 'lodash';
|
|
|
|
export class UtilStringHelper {
|
|
interpolate = function(
|
|
string: string,
|
|
data: any,
|
|
replace?: {
|
|
withEol?: boolean;
|
|
},
|
|
): any {
|
|
let finalString = string;
|
|
|
|
if (!replace || !replace.withEol) finalString = finalString = finalString.replace(/\s{2,}/g, '');
|
|
// finalString.replace(/(?:\r\n|\r|\n)/g, '<br>');
|
|
// // else
|
|
// finalString = finalString.replace(/\s{2,}/g, ''); // finalString.replace(/(?:\r\n|\r|\n)/g, '');
|
|
|
|
try {
|
|
const compiled = _.template(finalString);
|
|
if (finalString.includes('${data.')) return compiled({ data });
|
|
return compiled(data);
|
|
} catch (error) {
|
|
console.error(error);
|
|
|
|
const names = Object.keys(data);
|
|
const vals = Object.values(data) as any;
|
|
for (let index = 0; index < names.length; index++) {
|
|
finalString = finalString.replace('${' + names[index] + '}', vals[index]);
|
|
}
|
|
return finalString;
|
|
// return new Function(...names, "return String.raw`${this}`")(...vals);
|
|
}
|
|
};
|
|
|
|
removeTabAndReturn = function(string: string): string {
|
|
return string.replace(/[\n\t]+/g, '');
|
|
};
|
|
}
|