All files / src/utils slug.ts

65% Statements 13/20
41.66% Branches 5/12
50% Functions 2/4
63.15% Lines 12/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 36151x   92x 81x 81x   81x 25x   81x     81x     81x 35x     81x     151x                          
export const removeSpecialChars = (str: string, number?: number) => {
 
  if (!str) return ""
  const sanitizedString = str.replace(/[^a-zA-Z0-9- ]/g, "");
  const slug = sanitizedString.replaceAll(/\s+/g, "-");
 
  const cleanSlug = slug.replace(/[-.]+/g, (match) => {
    return match.length === 1 ? match : "";
  });
  let lowercaseSlug = cleanSlug.toLowerCase();
 
  // If the string starts or ends with a special character, remove it
  lowercaseSlug = lowercaseSlug.replace(/^[^a-zA-Z0-9]|[^a-zA-Z0-9]$/g, "");
 
  // If number is defined, truncate the string
  if (number) {
    lowercaseSlug = lowercaseSlug.substring(0, number);
  }
 
  return lowercaseSlug;
};
 
export const getFullApiURL = (endpoint: string) => {
  return `${process.env.VITE_API_ENDPOINT}/${endpoint}`;
};
 
//Get the width of the string based on string and font
export function getStringWidth(text?: string, font?: string) {
  const canvas = document.createElement("canvas");
  const context = canvas.getContext("2d");
  if (context && text) {
    context.font = font || "10px Give Whyte";
    return context.measureText(text).width;
  } else return 0;
}