Skip to content

Instantly share code, notes, and snippets.

@kra3
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kra3/7809abdce3ee747d1948 to your computer and use it in GitHub Desktop.
Save kra3/7809abdce3ee747d1948 to your computer and use it in GitHub Desktop.
urllib.unquote in JavaScript
// LICENCE: BSD 3 Clause
var _hexDig = "0123456789ABCDEFabcdef";
var _hexToChar = {};
var i, j;
// generate convertion map
for(i in _hexDig){
for(j in _hexDig){
var key = _hexDig[i] + _hexDig[j] ;
_hexToChar[key] = String.fromCharCode(parseInt(key, 16));
}
}
function unquote(encoded_string){
var bits = encoded_string.split('%');
if(bits.length === 1){
return encoded_string;
}
var res = [bits[0]];
bits.slice(1).map(function(data){
var f = data.slice(0,2);
var val = _hexToChar[f];
if(val === undefined){
res.push('%');
res.push(data);
}else{
res.push(val);
res.push(data.slice(2));
}
});
return res.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment