Skip to content

Instantly share code, notes, and snippets.

@fawazahmed0
Last active November 18, 2023 15:11
Show Gist options
  • Save fawazahmed0/13476b3a0ada0ebdc3016a6372559870 to your computer and use it in GitHub Desktop.
Save fawazahmed0/13476b3a0ada0ebdc3016a6372559870 to your computer and use it in GitHub Desktop.
Converts numbers from one language to another in a given string, it takes zero of source language and zero of destination language
// In below example I am converting numbers from Hindi to Latin for a given string
val = LangNum2Num("जिनसे आप कुछ ९० सीख सकते ३४ हैं हम लाये हैं ७ आपके लिये कुछ", "०", 0)
console.log(val)
// Prints जिनसे आप कुछ 90 सीख सकते 34 हैं हम लाये हैं 7 आपके लिये कुछ
// Coverts numbers insides a string to other language number
// str is the string
// zeroFrom is the zero number from the language you want to convert
// zeroTo is the zero number of the languague you want to convert to
function LangNum2Num(str, zeroFrom, zeroTo) {
// Get the UTF-16 code points of zeros
codezeroFrom = ("" + zeroFrom).codePointAt(0)
codezeroTo = ("" + zeroTo).codePointAt(0)
// Make array containing 0 to 10 for the language
var FromArr = [...Array(10).keys()].map(e => codezeroFrom + e).map(e => String.fromCodePoint(e))
var ToArr = [...Array(10).keys()].map(e => codezeroTo + e).map(e => String.fromCodePoint(e))
for(let i=0;i<FromArr.length;i++)
str=str.replaceAll(FromArr[i], ToArr[i])
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment