Created
May 7, 2013 09:16
-
-
Save qborreda/5531348 to your computer and use it in GitHub Desktop.
JavaScript Normalize strings
This file contains 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
var normalize = (function() { | |
var from = "ÃÀÁÄÂÈÉËÊÌÍÏÎÒÓÖÔÙÚÜÛãàáäâèéëêìíïîòóöôùúüûÑñÇç", | |
to = "AAAAAEEEEIIIIOOOOUUUUaaaaaeeeeiiiioooouuuunncc", | |
mapping = {}; | |
for(var i = 0, j = from.length; i < j; i++ ) | |
mapping[ from.charAt( i ) ] = to.charAt( i ); | |
return function( str ) { | |
var ret = []; | |
for( var i = 0, j = str.length; i < j; i++ ) { | |
var c = str.charAt( i ); | |
if( mapping.hasOwnProperty( str.charAt( i ) ) ) | |
ret.push( mapping[ c ] ); | |
else | |
ret.push( c ); | |
} | |
return ret.join( '' ); | |
} | |
})(); | |
console.log( normalize('Fábula de la cigüeña y el murciélago') ); | |
// Fabula de la ciguena y el murcielago |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment