Skip to content

Instantly share code, notes, and snippets.

@AliMilani
Last active September 5, 2022 12:51
Show Gist options
  • Save AliMilani/d1b594179ed5c25d262dc7737d53594a to your computer and use it in GitHub Desktop.
Save AliMilani/d1b594179ed5c25d262dc7737d53594a to your computer and use it in GitHub Desktop.
// const sample = " -گچپژ '۴', '۵', '۶', '۷', '۸', '۹AWSD@RWAD#t336--------23res شصیشصی 3ق131ق صشیشٌٌَََِِِص(_َ(ٌٌَِ__)ٌ(ّ)_]#«ّ['١', '٢', '٣', '٤', '٥', '٦', '٧', '٨«ً#)+]!«(]«#ريالٌ[ٍ«ّ:ٌَ»ٌءِ:»یصچش"
// const sample = '٠۶۶٠ ۶۶٠6 ٨ ٩List_of_Un icعبدلode۹_۹ch٠ ara c۹ ters'
const makeSlug = (text) => {
let slug = text
// select only persian, arabic, english letters and numbers
.replace(
/[^\w\s\-|\u0621-\u064A\u0660-\u0669\u06F0-\u06F9|\u06Af|\u067E|\u0698|\u0686|\u06CC|\u06A9|\u200C]/g,
''
)
// remove extra hyphens and spaces
.replace(/^-+|-+$|^\s+|\s+$/g, '')
// replace multiple spaces with single hyphen
.replace(/\s+/g, '-')
// replace zero-width non-joiner with a single hyphen
.replace(/[\u200C]/g, '-')
// replace multiple underscores with a single underscore for simplicity to read
.replace(/_+/g, '_')
// replace multiple hyphens with a single hyphen
.replace(/-+/g, '-')
return _convertNumbers(slug)
}
// console.log(makeSlug(sample))
const _convertNumbers = (number) =>
number
.split('')
.map((char) => _convertNumber(char))
.join('')
const _convertNumber = (char) => {
const arabicNumbers = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']
const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
const englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
// const cleanedNumber = number.replace(/[^۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩1234567890]/g, '')
if (arabicNumbers.includes(char)) {
return englishNumbers[arabicNumbers.indexOf(char)]
} else if (persianNumbers.includes(char)) {
return englishNumbers[persianNumbers.indexOf(char)]
} else {
return char
}
}
console.log(makeSlug(sample))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment