question 213 Modules & Packages · easy
A slugs module for URLs
Product pages need URL-friendly names ("slugs"), and the shop, the blog and the sitemap all make them. Write this file as the shared slugs module with one function, slugify(title). Check calls it directly and also imports the file as slugs.
Modulethis file, imported as
slugsFunction
slugify(title): lower case; every run of characters that aren't letters or digits (spaces, punctuation, underscores) becomes one -; no - at either endLettersaccented and non-English letters count as letters:
Crème → crèmeExample
slugify('Tea set, "Royal" edition') → 'tea-set-royal-edition'Examples
callslugify('Deluxe Pillow')returns'deluxe-pillow'
callslugify('Tea set, "Royal" edition')returns'tea-set-royal-edition'
callslugify(' Crème brûlée kit! ')returns'crème-brûlée-kit'