traxxx/src/utils/slugify.js

27 lines
558 B
JavaScript
Raw Normal View History

'use strict';
function slugify(string, delimiter = '-', {
encode = false,
limit = 1000,
} = {}) {
const slugComponents = string.trim().toLowerCase().match(/\w+/g);
if (!slugComponents) {
return '';
}
const slug = slugComponents.reduce((acc, component, index) => {
const accSlug = `${acc}${index > 0 ? delimiter : ''}${component}`;
if (accSlug.length < limit) {
return accSlug;
}
return acc;
}, '');
2020-01-10 21:10:11 +00:00
return encode ? encodeURI(slug) : slug;
}
module.exports = slugify;