diff --git a/src/utils/core.ts b/src/utils/core.ts index 6c5a61362..c18c0a3fe 100644 --- a/src/utils/core.ts +++ b/src/utils/core.ts @@ -67,6 +67,30 @@ export default { return value.charAt(0).toUpperCase() + value.slice(1); }, + /** + * Make a string into a slug (all lower case, remove accent, replace space and the rest by -) + * Ex: Hello it's beautiful here => hello-it-s-beautiful-here + * + * @param value {string} + * @returns {string} + */ + slugify(value: string): string { + if (!value) { + return ''; + } + + return value + // make lower case and trim + .toLowerCase().trim() + // remove accents from characters + .normalize('NFD').replace(/[\u0300-\u036f]/g, '') + // replace invalid chars with spaces + .replace(/[^a-z0-9\s-]/g, ' ') + .trim() + // replace multiple spaces or hyphens with a single hyphen + .replace(/[\s-]+/g, '-'); + }, + /** * Search occurrence of a value in text * @param text {string}