Last active
March 19, 2019 15:12
-
-
Save zeeshan1112/11963f1068f885744e7d9e8b9da358fe to your computer and use it in GitHub Desktop.
#Add Unicode value of strings in JSON array #ConvertStringsToUnicode #ConvertUnicodeToStrings
This file contains hidden or 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
/** | |
* This file takes a Javascript Object array of iso Languages as input | |
* and appends unicode to the array. | |
* input array : [{"code":"af","nativeName":"Afrikaans"}] | |
* Output array : [{"code":"af","nativeName":"Afrikaans"}, "unicodeName": "\\u0041\\u0066\\u0072\\u0069\\u006B\\u0061\\u0061\\u006E\\u0073"] | |
*/ | |
function StringManipulation() { | |
this.languageCode = [{ | |
"code": "zz", | |
"nativeName": "Default" | |
}, { | |
"code": "af", | |
"nativeName": "Afrikaans" | |
}, { | |
"code": "ar", | |
"nativeName": "العربية" | |
}, { | |
"code": "bg", | |
"nativeName": "Български" | |
}, { | |
"code": "ca", | |
"nativeName": "Català" | |
}, { | |
"code": "zh", | |
"nativeName": "简体中文" | |
}, { | |
"code": "zf", | |
"nativeName": "繁體中文" | |
}, { | |
"code": "hr", | |
"nativeName": "Hrvatski" | |
}, { | |
"code": "cs", | |
"nativeName": "čeština" | |
}, { | |
"code": "cy", | |
"nativeName": "Cymraeg" | |
}, { | |
"code": "da", | |
"nativeName": "Dansk" | |
}, { | |
"code": "nl", | |
"nativeName": "Nederlands" | |
}, { | |
"code": "en_UK", | |
"nativeName": "English (United Kingdom)" | |
}, { | |
"code": "en", | |
"nativeName": "English (United States)" | |
}, { | |
"code": "et", | |
"nativeName": "Eesti" | |
}, { | |
"code": "fa", | |
"nativeName": "فارسی" | |
}, { | |
"code": "fi", | |
"nativeName": "Suomi" | |
}, { | |
"code": "fr_CA", | |
"nativeName": "Français (Canada)" | |
}, { | |
"code": "fr", | |
"nativeName": "Français (France)" | |
}, { | |
"code": "de", | |
"nativeName": "Deutsch" | |
}, { | |
"code": "el", | |
"nativeName": "Ελληνικά" | |
}, { | |
"code": "he", | |
"nativeName": "עברית" | |
}, { | |
"code": "hi", | |
"nativeName": "हिन्दी" | |
}, { | |
"code": "hu", | |
"nativeName": "Magyar" | |
}, { | |
"code": "is", | |
"nativeName": "Íslenska" | |
}, { | |
"code": "id", | |
"nativeName": "Bahasa Indonesia" | |
}, { | |
"code": "it", | |
"nativeName": "Italiano" | |
}, { | |
"code": "ja", | |
"nativeName": "日本語" | |
}, { | |
"code": "kk", | |
"nativeName": "Қазақша" | |
}, { | |
"code": "ko", | |
"nativeName": "한국어" | |
}, { | |
"code": "lv", | |
"nativeName": "Latviešu" | |
}, { | |
"code": "lt", | |
"nativeName": "Lietuvių" | |
}, { | |
"code": "ms", | |
"nativeName": "Bahasa Melayu" | |
}, { | |
"code": "no", | |
"nativeName": "Norsk" | |
}, { | |
"code": "pl", | |
"nativeName": "Polski" | |
}, { | |
"code": "pt", | |
"nativeName": "Português (Brasil)" | |
}, { | |
"code": "pt_PT", | |
"nativeName": "Português (Portugal)" | |
}, { | |
"code": "z1", | |
"nativeName": "Reserved" | |
}, { | |
"code": "ro", | |
"nativeName": "Română" | |
}, { | |
"code": "ru", | |
"nativeName": "Русский" | |
}, { | |
"code": "sr", | |
"nativeName": "Српски" | |
}, { | |
"code": "sh", | |
"nativeName": "Српскохрватски" | |
}, { | |
"code": "sk", | |
"nativeName": "Slovenčina" | |
}, { | |
"code": "sl", | |
"nativeName": "Slovenščina" | |
}, { | |
"code": "es", | |
"nativeName": "Español (España)" | |
}, { | |
"code": "es_MX", | |
"nativeName": "Español (México)" | |
}, { | |
"code": "sv", | |
"nativeName": "Svenska" | |
}, { | |
"code": "th", | |
"nativeName": "ภาษาไทย" | |
}, { | |
"code": "tr", | |
"nativeName": "Türkçe" | |
}, { | |
"code": "uk", | |
"nativeName": "Українська" | |
}, { | |
"code": "vi", | |
"nativeName": "Việtnam" | |
}, { | |
"code": "zh_TW", | |
"nativeName": "繁體中文" | |
}, { | |
"code": "zh_CN", | |
"nativeName": "简体中文" | |
}]; | |
}; | |
/** | |
* Converts string to its corresponding unicode by taking string as input | |
* and returning unicode string as output. | |
*/ | |
StringManipulation.prototype.toUnicode = function(sName) { | |
var unicodeString = ''; | |
for (var i = 0; i < sName.length; i++) { | |
var unicode = sName.charCodeAt(i).toString(16).toUpperCase(); | |
while (unicode.length < 4) { | |
unicode = '0' + unicode; | |
} | |
unicode = '\\u' + unicode; | |
unicodeString += unicode; | |
} | |
return unicodeString; | |
}; | |
/** | |
* This function can convert the unicode back to string | |
*/ | |
StringManipulation.prototype.fromUnicodeToString = function (sUnicode) { | |
return sUnicode.replace(/\\u[\dA-F]{4}/gi, | |
function (match) { | |
return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)); | |
}); | |
}; | |
/** | |
* Creates a new array of objects with unicode of the iso Languages | |
*/ | |
StringManipulation.prototype.createJsonLanguageArray = function () { | |
var aLanguage = []; | |
Object.entries(this.languageCode).forEach(function(value, index) { | |
var unicode = this.toUnicode(value[1].nativeName); | |
var oLanguage = { | |
"code": value[1].code, | |
"nativeName": value[1].nativeName, | |
"unicodeName": unicode | |
}; | |
aLanguage.push(oLanguage); | |
}.bind(this)); | |
return aLanguage; | |
}; | |
var oStringManipulation = new StringManipulation(); | |
console.log(oStringManipulation.createJsonLanguageArray()); //Outputs objects array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment