Skip to content

Instantly share code, notes, and snippets.

@webgodo
Created February 9, 2019 21:25
Show Gist options
  • Save webgodo/645c7a7bc894f60e0cf386423a457502 to your computer and use it in GitHub Desktop.
Save webgodo/645c7a7bc894f60e0cf386423a457502 to your computer and use it in GitHub Desktop.
Small Useful Helpers For JavaScript (ES6)
// turn a string into a URL friendly serie of characters concatenated via '-'
export const slugify = (str) => str && typeof str === 'string' ? str
.toLowerCase()
// replace persian/arabic digits with numbers
.replace(/([٠١٢٣٤٥٦٧٨٩])|([۰۱۲۳۴۵۶۷۸۹])/g, (m, $1) => m.charCodeAt(0) - ($1 ? 1632 : 1776))
// replace characters other than [a-z] [ا-ی], [0-9], [آ,ئ] with a dash (-)
.replace(/[^آئa-z0-9ا-ی]+/g, '-')
.replace(/^-|-$/g, '')
// limit the string to 100 characters
.slice(0, 100) : ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment