UNPKG

1.4 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const escape_diacritic_1 = __importDefault(require("./escape_diacritic"));
6const escape_regexp_1 = __importDefault(require("./escape_regexp"));
7// eslint-disable-next-line no-control-regex
8const rControl = /[\u0000-\u001f]/g;
9const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g;
10function slugize(str, options = {}) {
11 if (typeof str !== 'string')
12 throw new TypeError('str must be a string!');
13 const separator = options.separator || '-';
14 const escapedSep = (0, escape_regexp_1.default)(separator);
15 const result = (0, escape_diacritic_1.default)(str)
16 // Remove control characters
17 .replace(rControl, '')
18 // Replace special characters
19 .replace(rSpecial, separator)
20 // Remove continous separators
21 .replace(new RegExp(`${escapedSep}{2,}`, 'g'), separator)
22 // Remove prefixing and trailing separtors
23 .replace(new RegExp(`^${escapedSep}+|${escapedSep}+$`, 'g'), '');
24 switch (options.transform) {
25 case 1:
26 return result.toLowerCase();
27 case 2:
28 return result.toUpperCase();
29 default:
30 return result;
31 }
32}
33module.exports = slugize;
34//# sourceMappingURL=slugize.js.map
\No newline at end of file