Para a próxima parte do meu Sistema de gerenciamento de conteúdo, eu estava terminando o sistema de formulários. Como o sistema CMS deve ser traduzível, comecei a examinar os problemas que alguém pode encontrar ao lidar com diacríticos (isto é, caracteres acentuados para nós, pessoas comuns). Meu problema é que, se tocar no sistema de arquivos, torne-o ASCII ou o mais ASCII possível. Eu encontrei esta resposta na comunidade stack. Eu modifiquei para ficar um pouco mais eficiente, então copie, cole e aproveite
/**
* Remove diacritics (accents) from a string
* @param {string} str The input string from which we will remove strings with diacritics
* @returns {string}
* @see http://goo.gl/zCBxkM
*/
function removeDiacritics (str) {
var diacriticsMap = {
A: /[u0041u24B6uFF21u00C0u00C1u00C2u1EA6u1EA4u1EAAu1EA8u00C3u0100u0102u1EB0u1EAEu1EB4u1EB2u0226u01E0u00C4u01DEu1EA2u00C5u01FAu01CDu0200u0202u1EA0u1EACu1EB6u1E00u0104u023Au2C6F]/g,
AA: /[uA732]/g,
AE: /[u00C6u01FCu01E2]/g,
AO: /[uA734]/g,
AU: /[uA736]/g,
AV: /[uA738uA73A]/g,
AY: /[uA73C]/g,
B: /[u0042u24B7uFF22u1E02u1E04u1E06u0243u0182u0181]/g,
C: /[u0043u24B8uFF23u0106u0108u010Au010Cu00C7u1E08u0187u023BuA73E]/g,
D: /[u0044u24B9uFF24u1E0Au010Eu1E0Cu1E10u1E12u1E0Eu0110u018Bu018Au0189uA779]/g,
DZ: /[u01F1u01C4]/g,
Dz: /[u01F2u01C5]/g,
E: /[u0045u24BAuFF25u00C8u00C9u00CAu1EC0u1EBEu1EC4u1EC2u1EBCu0112u1E14u1E16u0114u0116u00CBu1EBAu011Au0204u0206u1EB8u1EC6u0228u1E1Cu0118u1E18u1E1Au0190u018E]/g,
F: /[u0046u24BBuFF26u1E1Eu0191uA77B]/g,
G: /[u0047u24BCuFF27u01F4u011Cu1E20u011Eu0120u01E6u0122u01E4u0193uA7A0uA77DuA77E]/g,
H: /[u0048u24BDuFF28u0124u1E22u1E26u021Eu1E24u1E28u1E2Au0126u2C67u2C75uA78D]/g,
I: /[u0049u24BEuFF29u00CCu00CDu00CEu0128u012Au012Cu0130u00CFu1E2Eu1EC8u01CFu0208u020Au1ECAu012Eu1E2Cu0197]/g,
J: /[u004Au24BFuFF2Au0134u0248]/g,
K: /[u004Bu24C0uFF2Bu1E30u01E8u1E32u0136u1E34u0198u2C69uA740uA742uA744uA7A2]/g,
L: /[u004Cu24C1uFF2Cu013Fu0139u013Du1E36u1E38u013Bu1E3Cu1E3Au0141u023Du2C62u2C60uA748uA746uA780]/g,
LJ: /[u01C7]/g,
Lj: /[u01C8]/g,
M: /[u004Du24C2uFF2Du1E3Eu1E40u1E42u2C6Eu019C]/g,
N: /[u004Eu24C3uFF2Eu01F8u0143u00D1u1E44u0147u1E46u0145u1E4Au1E48u0220u019DuA790uA7A4]/g,
NJ: /[u01CA]/g,
Nj: /[u01CB]/g,
O: /[u004Fu24C4uFF2Fu00D2u00D3u00D4u1ED2u1ED0u1ED6u1ED4u00D5u1E4Cu022Cu1E4Eu014Cu1E50u1E52u014Eu022Eu0230u00D6u022Au1ECEu0150u01D1u020Cu020Eu01A0u1EDCu1EDAu1EE0u1EDEu1EE2u1ECCu1ED8u01EAu01ECu00D8u01FEu0186u019FuA74AuA74C]/g,
OI: /[u01A2]/g,
OO: /[uA74E]/g,
OU: /[u0222]/g,
P: /[u0050u24C5uFF30u1E54u1E56u01A4u2C63uA750uA752uA754]/g,
Q: /[u0051u24C6uFF31uA756uA758u024A]/g,
R: /[u0052u24C7uFF32u0154u1E58u0158u0210u0212u1E5Au1E5Cu0156u1E5Eu024Cu2C64uA75AuA7A6uA782]/g,
S: /[u0053u24C8uFF33u1E9Eu015Au1E64u015Cu1E60u0160u1E66u1E62u1E68u0218u015Eu2C7EuA7A8uA784]/g,
T: /[u0054u24C9uFF34u1E6Au0164u1E6Cu021Au0162u1E70u1E6Eu0166u01ACu01AEu023EuA786]/g,
TZ: /[uA728]/g,
U: /[u0055u24CAuFF35u00D9u00DAu00DBu0168u1E78u016Au1E7Au016Cu00DCu01DBu01D7u01D5u01D9u1EE6u016Eu0170u01D3u0214u0216u01AFu1EEAu1EE8u1EEEu1EECu1EF0u1EE4u1E72u0172u1E76u1E74u0244]/g,
V: /[u0056u24CBuFF36u1E7Cu1E7Eu01B2uA75Eu0245]/g,
VY: /[uA760]/g,
W: /[u0057u24CCuFF37u1E80u1E82u0174u1E86u1E84u1E88u2C72]/g,
X: /[u0058u24CDuFF38u1E8Au1E8C]/g,
Y: /[u0059u24CEuFF39u1EF2u00DDu0176u1EF8u0232u1E8Eu0178u1EF6u1EF4u01B3u024Eu1EFE]/g,
Z: /[u005Au24CFuFF3Au0179u1E90u017Bu017Du1E92u1E94u01B5u0224u2C7Fu2C6BuA762]/g,
a: /[u0061u24D0uFF41u1E9Au00E0u00E1u00E2u1EA7u1EA5u1EABu1EA9u00E3u0101u0103u1EB1u1EAFu1EB5u1EB3u0227u01E1u00E4u01DFu1EA3u00E5u01FBu01CEu0201u0203u1EA1u1EADu1EB7u1E01u0105u2C65u0250]/g,
aa: /[uA733]/g,
ae: /[u00E6u01FDu01E3]/g,
ao: /[uA735]/g,
au: /[uA737]/g,
av: /[uA739uA73B]/g,
ay: /[uA73D]/g,
b: /[u0062u24D1uFF42u1E03u1E05u1E07u0180u0183u0253]/g,
c: /[u0063u24D2uFF43u0107u0109u010Bu010Du00E7u1E09u0188u023CuA73Fu2184]/g,
d: /[u0064u24D3uFF44u1E0Bu010Fu1E0Du1E11u1E13u1E0Fu0111u018Cu0256u0257uA77A]/g,
dz: /[u01F3u01C6]/g,
e: /[u0065u24D4uFF45u00E8u00E9u00EAu1EC1u1EBFu1EC5u1EC3u1EBDu0113u1E15u1E17u0115u0117u00EBu1EBBu011Bu0205u0207u1EB9u1EC7u0229u1E1Du0119u1E19u1E1Bu0247u025Bu01DD]/g,
f: /[u0066u24D5uFF46u1E1Fu0192uA77C]/g,
g: /[u0067u24D6uFF47u01F5u011Du1E21u011Fu0121u01E7u0123u01E5u0260uA7A1u1D79uA77F]/g,
h: /[u0068u24D7uFF48u0125u1E23u1E27u021Fu1E25u1E29u1E2Bu1E96u0127u2C68u2C76u0265]/g,
hv: /[u0195]/g,
i: /[u0069u24D8uFF49u00ECu00EDu00EEu0129u012Bu012Du00EFu1E2Fu1EC9u01D0u0209u020Bu1ECBu012Fu1E2Du0268u0131]/g,
j: /[u006Au24D9uFF4Au0135u01F0u0249]/g,
k: /[u006Bu24DAuFF4Bu1E31u01E9u1E33u0137u1E35u0199u2C6AuA741uA743uA745uA7A3]/g,
l: /[u006Cu24DBuFF4Cu0140u013Au013Eu1E37u1E39u013Cu1E3Du1E3Bu017Fu0142u019Au026Bu2C61uA749uA781uA747]/g,
lj: /[u01C9]/g,
m: /[u006Du24DCuFF4Du1E3Fu1E41u1E43u0271u026F]/g,
n: /[u006Eu24DDuFF4Eu01F9u0144u00F1u1E45u0148u1E47u0146u1E4Bu1E49u019Eu0272u0149uA791uA7A5]/g,
nj: /[u01CC]/g,
o: /[u006Fu24DEuFF4Fu00F2u00F3u00F4u1ED3u1ED1u1ED7u1ED5u00F5u1E4Du022Du1E4Fu014Du1E51u1E53u014Fu022Fu0231u00F6u022Bu1ECFu0151u01D2u020Du020Fu01A1u1EDDu1EDBu1EE1u1EDFu1EE3u1ECDu1ED9u01EBu01EDu00F8u01FFu0254uA74BuA74Du0275]/g,
oi: /[u01A3]/g,
ou: /[u0223]/g,
oo: /[uA74F]/g,
p: /[u0070u24DFuFF50u1E55u1E57u01A5u1D7DuA751uA753uA755]/g,
q: /[u0071u24E0uFF51u024BuA757uA759]/g,
r: /[u0072u24E1uFF52u0155u1E59u0159u0211u0213u1E5Bu1E5Du0157u1E5Fu024Du027DuA75BuA7A7uA783]/g,
s: /[u0073u24E2uFF53u015Bu1E65u015Du1E61u0161u1E67u1E63u1E69u0219u015Fu023FuA7A9uA785u1E9B]/g,
ss: /[u00DF]/g,
t: /[u0074u24E3uFF54u1E6Bu1E97u0165u1E6Du021Bu0163u1E71u1E6Fu0167u01ADu0288u2C66uA787]/g,
tz: /[uA729]/g,
u: /[u0075u24E4uFF55u00F9u00FAu00FBu0169u1E79u016Bu1E7Bu016Du00FCu01DCu01D8u01D6u01DAu1EE7u016Fu0171u01D4u0215u0217u01B0u1EEBu1EE9u1EEFu1EEDu1EF1u1EE5u1E73u0173u1E77u1E75u0289]/g,
v: /[u0076u24E5uFF56u1E7Du1E7Fu028BuA75Fu028C]/g,
vy: /[uA761]/g,
w: /[u0077u24E6uFF57u1E81u1E83u0175u1E87u1E85u1E98u1E89u2C73]/g,
x: /[u0078u24E7uFF58u1E8Bu1E8D]/g,
y: /[u0079u24E8uFF59u1EF3u00FDu0177u1EF9u0233u1E8Fu00FFu1EF7u1E99u1EF5u01B4u024Fu1EFF]/g,
z: /[u007Au24E9uFF5Au017Au1E91u017Cu017Eu1E93u1E95u01B6u0225u0240u2C6CuA763]/g
};
for (var x in diacriticsMap) {
// Iterate through each keys in the above object and perform a replace
str = str.replace(diacriticsMap[x], x);
}
return str;
}