All files / src/string stringf.ts

0% Statements 0/13
0% Branches 0/6
0% Functions 0/7
0% Lines 0/13

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                                                                                                                 
/**
 * Slugifies a string.
 * https://gist.github.com/hagemann/382adfc57adbd5af078dc93feef01fe1
 */
export function slugify(str: string) {
  const a =
    'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;';
  const b =
    'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------';
  const p = new RegExp(a.split('').join('|'), 'g');
 
  return str
    .toString()
    .toLowerCase()
    .replace(/\s+/g, '-')
    .replace(p, (c) => b.charAt(a.indexOf(c)))
    .replace(/[^\w-]+/g, '')
    .replace(/-+/g, '-')
    .replace(/^-+/, '')
    .replace(/-+$/, '');
}
 
/**
 * A basic zero padding.
 *
 *
 * ```
 * stringf.zeroPad('1') ---> 01
 * stringf.zeroPad('0') ---> 00
 * stringf.zeroPad('9') ---> 09
 * stringf.zeroPad('10') ---> 10
 * stringf.zeroPad('22') ---> 10
 * ```
 *
 * @param str
 */
export function zeroPad(str: string) {
  if (str.toString().length === 1) {
    str = '0' + str;
  }
  return str;
}
 
// https://stackoverflow.com/questions/105034/how-to-create-guid-uuid
export function uuid(): string {
  return (
    Math.random().toString(36).substring(2, 15) +
    Math.random().toString(36).substring(2, 15)
  );
}
 
/**
 * Given a number, outputs it with commas.
 * @param x
 */
export function numberWithCommas(x: number): string {
  return x.toLocaleString();
}
 
/**
 * Only returns alpha numberic values.  Handy for quick sanitization
 * in selext cases.
 * @param x
 */
export function alphaNumeric(x: string): string {
  return x && x.replace(/[^A-Za-z0-9.]/gi, '');
}
 
export function numeric(x: string): string {
  return x && x.replace(/[^0-9.]/gi, '');
}
 
export const stringf = {
  slugify,
  zeroPad,
  uuid,
  numberWithCommas,
  alphaNumeric,
  numeric,
};