'use strict';

function slugify(string, {
    encode = false,
    delimiter = '-',
    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;
    }, '');

    return encode ? encodeURI(slug) : slug;
}

module.exports = slugify;
