Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to translate accented characters to ASCII equivalents. Stripped out from SugarJS. (https://github.com/andrewplummer/Sugar/)
/**
* Returns the string with accented and non-standard Latin-based characters converted into ASCII approximate equivalents.
*
* @returns {String}
*/
String.prototype.normalize = function() {
var NormalizeSource = {
'A': /[AⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ]/g,
'B': /[BⒷBḂḄḆɃƂƁ]/g,
'C': /[CⒸCĆĈĊČÇḈƇȻꜾ]/g,
'D': /[DⒹDḊĎḌḐḒḎĐƋƊƉꝹ]/g,
'E': /[EⒺEÈÉÊỀẾỄỂẼĒḔḖĔĖËẺĚȄȆẸỆȨḜĘḘḚƐƎ]/g,
'F': /[FⒻFḞƑꝻ]/g,
'G': /[GⒼGǴĜḠĞĠǦĢǤƓꞠꝽꝾ]/g,
'H': /[HⒽHĤḢḦȞḤḨḪĦⱧⱵꞍ]/g,
'I': /[IⒾIÌÍÎĨĪĬİÏḮỈǏȈȊỊĮḬƗ]/g,
'J': /[JⒿJĴɈ]/g,
'K': /[KⓀKḰǨḲĶḴƘⱩꝀꝂꝄꞢ]/g,
'L': /[LⓁLĿĹĽḶḸĻḼḺŁȽⱢⱠꝈꝆꞀ]/g,
'M': /[MⓂMḾṀṂⱮƜ]/g,
'N': /[NⓃNǸŃÑṄŇṆŅṊṈȠƝꞐꞤ]/g,
'O': /[OⓄOÒÓÔỒỐỖỔÕṌȬṎŌṐṒŎȮȰÖȪỎŐǑȌȎƠỜỚỠỞỢỌỘǪǬØǾƆƟꝊꝌ]/g,
'P': /[PⓅPṔṖƤⱣꝐꝒꝔ]/g,
'Q': /[QⓆQꝖꝘɊ]/g,
'R': /[RⓇRŔṘŘȐȒṚṜŖṞɌⱤꝚꞦꞂ]/g,
'S': /[SⓈSẞŚṤŜṠŠṦṢṨȘŞⱾꞨꞄ]/g,
'T': /[TⓉTṪŤṬȚŢṰṮŦƬƮȾꞆ]/g,
'U': /[UⓊUÙÚÛŨṸŪṺŬÜǛǗǕǙỦŮŰǓȔȖƯỪỨỮỬỰỤṲŲṶṴɄ]/g,
'V': /[VⓋVṼṾƲꝞɅ]/g,
'W': /[WⓌWẀẂŴẆẄẈⱲ]/g,
'X': /[XⓍXẊẌ]/g,
'Y': /[YⓎYỲÝŶỸȲẎŸỶỴƳɎỾ]/g,
'Z': /[ZⓏZŹẐŻŽẒẔƵȤⱿⱫꝢ]/g,
'a': /[aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐ]/g,
'b': /[bⓑbḃḅḇƀƃɓ]/g,
'c': /[cⓒcćĉċčçḉƈȼꜿↄ]/g,
'd': /[dⓓdḋďḍḑḓḏđƌɖɗꝺ]/g,
'e': /[eⓔeèéêềếễểẽēḕḗĕėëẻěȅȇẹệȩḝęḙḛɇɛǝ]/g,
'f': /[fⓕfḟƒꝼ]/g,
'g': /[gⓖgǵĝḡğġǧģǥɠꞡᵹꝿ]/g,
'h': /[hⓗhĥḣḧȟḥḩḫẖħⱨⱶɥ]/g,
'i': /[iⓘiìíîĩīĭïḯỉǐȉȋịįḭɨı]/g,
'j': /[jⓙjĵǰɉ]/g,
'k': /[kⓚkḱǩḳķḵƙⱪꝁꝃꝅꞣ]/g,
'l': /[lⓛlŀĺľḷḹļḽḻſłƚɫⱡꝉꞁꝇ]/g,
'm': /[mⓜmḿṁṃɱɯ]/g,
'n': /[nⓝnǹńñṅňṇņṋṉƞɲʼnꞑꞥ]/g,
'o': /[oⓞoòóôồốỗổõṍȭṏōṑṓŏȯȱöȫỏőǒȍȏơờớỡởợọộǫǭøǿɔꝋꝍɵ]/g,
'p': /[pⓟpṕṗƥᵽꝑꝓꝕ]/g,
'q': /[qⓠqɋꝗꝙ]/g,
'r': /[rⓡrŕṙřȑȓṛṝŗṟɍɽꝛꞧꞃ]/g,
's': /[sⓢsśṥŝṡšṧṣṩșşȿꞩꞅẛ]/g,
't': /[tⓣtṫẗťṭțţṱṯŧƭʈⱦꞇ]/g,
'u': /[uⓤuùúûũṹūṻŭüǜǘǖǚủůűǔȕȗưừứữửựụṳųṷṵʉ]/g,
'v': /[vⓥvṽṿʋꝟʌ]/g,
'w': /[wⓦwẁẃŵẇẅẘẉⱳ]/g,
'x': /[xⓧxẋẍ]/g,
'y': /[yⓨyỳýŷỹȳẏÿỷẙỵƴɏỿ]/g,
'z': /[zⓩzźẑżžẓẕƶȥɀⱬꝣ]/g,
'AA': /[]/g,
'AE': /[ÆǼǢ]/g,
'AO': /[]/g,
'AU': /[]/g,
'AV': /[ꜸꜺ]/g,
'AY': /[]/g,
'DZ': /[DZDŽ]/g,
'Dz': /[DzDž]/g,
'LJ': /[LJ]/g,
'Lj': /[Lj]/g,
'NJ': /[NJ]/g,
'Nj': /[Nj]/g,
'OI': /[Ƣ]/g,
'OO': /[]/g,
'OU': /[Ȣ]/g,
'TZ': /[]/g,
'VY': /[]/g,
'aa': /[]/g,
'ae': /[æǽǣ]/g,
'ao': /[]/g,
'au': /[]/g,
'av': /[ꜹꜻ]/g,
'ay': /[]/g,
'dz': /[dzdž]/g,
'hv': /[ƕ]/g,
'lj': /[lj]/g,
'nj': /[nj]/g,
'oi': /[ƣ]/g,
'ou': /[ȣ]/g,
'oo': /[]/g,
'ss': /[ß]/g,
'tz': /[]/g,
'vy': /[]/g
};
var value = this;
for (var normalized in NormalizeSource) {
var regexp = NormalizeSource[normalized];
value = value.replace(regexp, normalized);
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.