UNPKG

701 BJavaScriptView Raw
1'use strict';
2const deburr = require('lodash.deburr');
3
4module.exports = string => {
5 const separator = '-';
6
7 string = deburr(string);
8
9 // Decamelize
10 string = string
11 .replace(/([a-z\d])([A-Z])/g, `$1 $2`)
12 .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, `$1 $2`);
13
14 // Custom replacements
15 string = string
16 .replace(/&/, ' and ')
17 .replace(/🦄/, ' unicorn ')
18 .replace(/♥/, ' love ');
19
20 string = string.toLowerCase();
21
22 string = string.replace(/[^a-z\d]+/g, '-');
23
24 string = string
25 // Remove duplicate separators
26 .replace(new RegExp(`${separator}{2,}`, 'g'), separator)
27 // Remove separator from start and end
28 .replace(new RegExp(`^${separator}|${separator}$`, 'g'), '');
29
30 return string;
31};