All files slugify.ts

100% Statements 14/14
100% Branches 2/2
100% Functions 2/2
100% Lines 14/14

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 448x                     8x 3x   7x   3x     3x       3x   3x 3x 294x       3x             3x 1x   2x    
import { normalizeString } from './normalizeString';
 
/**
 * Converts any string to slug
 * @param str the string
 * @signature
 *    P.slugify(str);
 * @example
 *    P.slugify("Super ball cup") // => super-ball-cup
 * @category String
 */
export function slugify(str: string) {
  const qw = str
    .split(' ')
    .map(q => normalizeString(q))
    .join('-');
  str = qw.replace(/^\s+|\s+$/g, '');
 
  // Make the string lowercase
  str = str.toLowerCase();
 
  // Remove accents, swap ñ for n, etc
  var from =
    'ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđßÆa·/_,:;';
  var to =
    'AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------';
  for (var i = 0, l = from.length; i < l; i++) {
    str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
  }
 
  // Remove invalid chars
  str = str
    .replace(/[^a-z0-9 -]/g, '')
    // Collapse whitespace and replace by -
    .replace(/\s+/g, '-')
    // Collapse dashes
    .replace(/-+/g, '-');
 
  if (str[str.length - 1] === '-') {
    return str.slice(0, str.length - 1);
  }
  return str;
}