forked from DebaucheryLibrarian/traxxx
24 lines
509 B
JavaScript
24 lines
509 B
JavaScript
'use strict';
|
|
|
|
function slugify(string, {
|
|
encode = false,
|
|
delimiter = '-',
|
|
limit = 1000,
|
|
} = {}) {
|
|
const slugComponents = string.trim().toLowerCase().match(/\w+/g);
|
|
|
|
const slug = slugComponents.reduce((acc, component, index) => {
|
|
const accSlug = `${acc}${index > 0 ? delimiter : ''}${component}`;
|
|
|
|
if (accSlug.length < limit) {
|
|
return accSlug;
|
|
}
|
|
|
|
return acc;
|
|
}, '');
|
|
|
|
return encode ? encodeURI(slug) : slug;
|
|
}
|
|
|
|
module.exports = slugify;
|