Created
February 9, 2019 21:25
-
-
Save webgodo/645c7a7bc894f60e0cf386423a457502 to your computer and use it in GitHub Desktop.
Small Useful Helpers For JavaScript (ES6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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