2020-01-07 03:23:28 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-05-26 02:11:29 +00:00
|
|
|
const substitutes = {
|
|
|
|
à: 'a',
|
|
|
|
á: 'a',
|
|
|
|
ä: 'a',
|
|
|
|
å: 'a',
|
2020-05-26 23:40:10 +00:00
|
|
|
ã: 'a',
|
2020-05-26 02:11:29 +00:00
|
|
|
æ: 'ae',
|
|
|
|
ç: 'c',
|
|
|
|
è: 'e',
|
|
|
|
é: 'e',
|
|
|
|
ë: 'e',
|
2020-05-26 23:40:10 +00:00
|
|
|
ẽ: 'e',
|
2020-05-26 02:11:29 +00:00
|
|
|
ì: 'i',
|
|
|
|
í: 'i',
|
|
|
|
ï: 'i',
|
2020-05-26 23:40:10 +00:00
|
|
|
ĩ: 'i',
|
2020-05-26 02:11:29 +00:00
|
|
|
ǹ: 'n',
|
|
|
|
ń: 'n',
|
|
|
|
ñ: 'n',
|
|
|
|
ò: 'o',
|
|
|
|
ó: 'o',
|
|
|
|
ö: 'o',
|
2020-05-26 23:40:10 +00:00
|
|
|
õ: 'o',
|
2020-05-26 02:11:29 +00:00
|
|
|
ø: 'o',
|
|
|
|
œ: 'oe',
|
|
|
|
ß: 'ss',
|
|
|
|
ù: 'u',
|
|
|
|
ú: 'u',
|
|
|
|
ü: 'u',
|
2020-05-26 23:40:10 +00:00
|
|
|
ũ: 'u',
|
2020-05-26 02:11:29 +00:00
|
|
|
ỳ: 'y',
|
|
|
|
ý: 'y',
|
|
|
|
ÿ: 'y',
|
2020-05-26 23:40:10 +00:00
|
|
|
ỹ: 'y',
|
2020-05-26 02:11:29 +00:00
|
|
|
};
|
|
|
|
|
2020-03-16 03:10:52 +00:00
|
|
|
function slugify(string, delimiter = '-', {
|
2020-05-14 02:26:05 +00:00
|
|
|
encode = false,
|
2020-05-26 02:11:29 +00:00
|
|
|
removeAccents = true,
|
2020-09-04 23:56:54 +00:00
|
|
|
removePunctuation = false,
|
2020-05-14 02:26:05 +00:00
|
|
|
limit = 1000,
|
2020-02-04 02:12:09 +00:00
|
|
|
} = {}) {
|
2020-05-18 23:10:32 +00:00
|
|
|
if (!string || typeof string !== 'string') {
|
2020-05-14 02:26:05 +00:00
|
|
|
return string;
|
|
|
|
}
|
2020-03-22 02:50:24 +00:00
|
|
|
|
2020-09-04 23:56:54 +00:00
|
|
|
const slugComponents = string
|
|
|
|
.trim()
|
|
|
|
.toLowerCase()
|
2021-03-29 20:22:56 +00:00
|
|
|
.replace(removePunctuation && /[.,:;'"_-]/g, '')
|
2020-09-04 23:56:54 +00:00
|
|
|
.match(/[A-Za-zÀ-ÖØ-öø-ÿ0-9]+/g);
|
2020-02-04 02:12:09 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (!slugComponents) {
|
|
|
|
return '';
|
|
|
|
}
|
2020-02-20 01:35:23 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const slug = slugComponents.reduce((acc, component, index) => {
|
|
|
|
const accSlug = `${acc}${index > 0 ? delimiter : ''}${component}`;
|
2020-02-04 02:12:09 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (accSlug.length < limit) {
|
2020-05-26 02:11:29 +00:00
|
|
|
if (removeAccents) {
|
2021-11-20 22:59:15 +00:00
|
|
|
return accSlug.replace(/[à-ÿ]/g, (match) => substitutes[match] || '');
|
2020-05-26 02:11:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return accSlug;
|
|
|
|
}
|
2020-02-04 02:12:09 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return acc;
|
|
|
|
}, '');
|
2020-01-10 21:10:11 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return encode ? encodeURI(slug) : slug;
|
2020-01-07 03:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = slugify;
|