From 32a1351e7e04d468e9960f85604be4c017eaa22d Mon Sep 17 00:00:00 2001 From: Jonathan LELIEVRE Date: Wed, 28 Aug 2024 17:25:48 +0200 Subject: [PATCH] Add slugify into core utils --- src/utils/core.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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}