All files / utils common.ts

100% Statements 22/22
100% Branches 0/0
100% Functions 2/2
100% Lines 19/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 36 37 38 39 40 41 42 43 44 45 46 47 48 49          4x   1x     1x             1x     1x 1x 1x 1x     1x 1x 1x               4x 4x 4x 4x   4x 15x 15x     4x    
/**
 * Use this to generate slug from string value
 * @param value
 * @returns
 */
export const generateSlug = (value: string) => {
  // convert to lower case
  let slug = value.toLowerCase();
 
  // remove special characters
  slug = slug.replace(
    /\`|\~|\!|\@|\#|\||\$|\%|\^|\&|\*|\(|\)|\+|\=|\,|\.|\/|\\|\?|\>|\<|\'|\"|\:|\;|_/gi,
    ""
  );
  // The /gi modifier is used to do a case insensitive search of all occurrences of a regular expression in a string
 
  // replace spaces with dash symbols
  slug = slug.replace(/ /gi, "-");
 
  // remove consecutive dash symbols
  slug = slug.replace(/\-\-\-\-\-/gi, "-");
  slug = slug.replace(/\-\-\-\-/gi, "-");
  slug = slug.replace(/\-\-\-/gi, "-");
  slug = slug.replace(/\-\-/gi, "-");
 
  // remove the unwanted dash symbols at the beginning and the end of the slug
  slug = "@" + slug + "@";
  slug = slug.replace(/\@\-|\-\@|\@/gi, "");
  return slug;
};
 
/**
 * Use this to get random string of given length
 * @param len length of string
 * @returns
 */
export const randomStringKey = (len: number) => {
  const buf = [];
  const chars = "0123456789abcdefghijklmnopqrstuvwxyz";
  const charLength = chars.length;
 
  for (let i = 0; i < len; ++i) {
    const randomInt = Math.floor(Math.random() * (charLength - 1 - 0 + 1)) + 0;
    buf.push(chars[randomInt]);
  }
 
  return buf.join("");
};