From 0c15ff3059aac0dcbc1d556328e6bc91d3284767 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 26 Jul 2026 20:12:27 +0300 Subject: [PATCH] feat(utils): Added slug function for generate url for products --- utils/slugify.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 utils/slugify.py diff --git a/utils/slugify.py b/utils/slugify.py new file mode 100644 index 0000000..0c510db --- /dev/null +++ b/utils/slugify.py @@ -0,0 +1,19 @@ +import re + + +TRANSLIT_TABLE = { + 'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e', 'ё': 'e', + 'ж': 'zh', 'з': 'z', 'и': 'i', 'й': 'y', 'к': 'k', 'л': 'l', 'м': 'm', + 'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u', + 'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch', 'ш': 'sh', 'щ': 'sch', 'ъ': '', + 'ы': 'y', 'ь': '', 'э': 'e', 'ю': 'yu', 'я': 'ya', +} + + +def slugify(text: str) -> str: + text = text.lower() + for ru, en in TRANSLIT_TABLE.items(): + text = text.replace(ru, en) + text = re.sub(r'[^a-z0-9]+', '-', text) + text = re.sub(r'^-+|-+$', '', text) + return text