performance improvements

This commit is contained in:
2023-04-27 13:23:53 +10:00
parent b36ad8042f
commit bef4c3440b

View File

@@ -5,11 +5,8 @@
* @returns {string} A string transformed to title case. * @returns {string} A string transformed to title case.
*/ */
export const toTitleCase = (str: string): string => { export const toTitleCase = (str: string): string => {
return str.replace(/\w\S*/g, function (txt) { return str.replace(/\b\w+\b/g, function (txt) {
return ( return txt.charAt(0).toUpperCase() + txt.slice(1).toLowerCase();
txt.charAt(0).toUpperCase() +
txt.substring(1).replace(/_/g, " ").toLowerCase()
);
}); });
}; };
@@ -22,31 +19,34 @@ export const toTitleCase = (str: string): string => {
* @param stripHtml * @param stripHtml
* @returns {string} The excerpt. * @returns {string} The excerpt.
*/ */
export const excerpt = ( export function excerpt(
txt: string, txt: string,
maxLen: number = 150, maxLen: number = 150,
stripHtml: boolean = true stripHtml: boolean = true
): string => { ): string {
if (stripHtml) { if (stripHtml) {
txt = stripHtmlTags(replaceHtmlEntites(txt)); txt = txt.replace(/<[^>]+>/g, "").replace(/&nbsp;/g, " ");
} }
const txtPieces = txt.split(" "); const words = txt.trim().split(/\s+/);
const excerptPieces: string[] = [];
let curLen = 0; let curLen = 0;
const excerptWords: string[] = [];
txtPieces.every((itm) => { for (const word of words) {
if (curLen + itm.length >= maxLen) { if (curLen + word.length + 1 > maxLen) {
return false; break;
} }
curLen += word.length + 1;
excerptWords.push(word);
}
excerptPieces.push(itm); let excerpt = excerptWords.join(" ");
curLen += itm.length + 1; if (curLen < txt.length) {
return true; excerpt += "...";
}); }
return excerptPieces.join(" ") + (curLen < txt.length ? "..." : ""); return excerpt;
}; }
/** /**
* String HTML tags from text. * String HTML tags from text.
@@ -55,8 +55,7 @@ export const excerpt = (
* @returns {string} The stripped text. * @returns {string} The stripped text.
*/ */
export const stripHtmlTags = (txt: string): string => { export const stripHtmlTags = (txt: string): string => {
txt = txt.replace(/<(p|br)([ /]*?>|[ /]+.*?>)/g, " "); return txt.replace(/<(p|br)([ /]*?>|[ /]+.*?>)|<[a-zA-Z/][^>]+(>|$)/g, " ");
return txt.replace(/<[a-zA-Z/][^>]+(>|$)/g, "");
}; };
/** /**
@@ -65,18 +64,24 @@ export const stripHtmlTags = (txt: string): string => {
* @param {string} txt The text to transform. * @param {string} txt The text to transform.
* @returns {string} Transformed text * @returns {string} Transformed text
*/ */
export const replaceHtmlEntites = (txt: string): string => { export const replaceHtmlEntities = (txt: string): string => {
const translate_re = /&(nbsp|amp|quot|lt|gt);/g; const translate_re = /&(nbsp|amp|quot|lt|gt);/g;
const translate = {
nbsp: " ",
amp: "&",
quot: '"',
lt: "<",
gt: ">",
};
return txt.replace(translate_re, function (match, entity) { return txt.replace(translate_re, function (match, entity) {
return translate[entity]; switch (entity) {
case "nbsp":
return " ";
case "amp":
return "&";
case "quot":
return '"';
case "lt":
return "<";
case "gt":
return ">";
default:
return match;
}
}); });
}; };
@@ -88,8 +93,8 @@ export const replaceHtmlEntites = (txt: string): string => {
*/ */
export const stringToNumber = (str: string): number => { export const stringToNumber = (str: string): number => {
str = str.replace(/[^\d.-]/g, ""); str = str.replace(/[^\d.-]/g, "");
const num = Number.parseFloat(str); const num = parseFloat(str);
return isNaN(num) ? 0 : parseFloat(num.toFixed(2)); return isNaN(num) ? 0 : Number(num.toFixed(2));
}; };
/** /**
@@ -99,19 +104,9 @@ export const stringToNumber = (str: string): number => {
* @returns {string} The converted result. * @returns {string} The converted result.
*/ */
export const toPrice = (numOrString: number | string): string => { export const toPrice = (numOrString: number | string): string => {
let num = 0; const num =
typeof numOrString === "string"
if (typeof numOrString == "string") { ? stringToNumber(numOrString)
num = stringToNumber(numOrString); : numOrString;
} else { return num.toFixed(num % 1 === 0 ? 0 : 2);
num = numOrString;
}
if (num % 1 === 0) {
// Number has no decimal places
return num.toFixed(0);
} else {
// Number has decimal places
return num.toFixed(2);
}
}; };