Skip to content

Instantly share code, notes, and snippets.

@soomtong
Created October 14, 2013 03:12
Show Gist options
  • Save soomtong/6970113 to your computer and use it in GitHub Desktop.
Save soomtong/6970113 to your computer and use it in GitHub Desktop.
convert UTF-8 from URIEncoded EUC-KR
// original code from https://github.com/guersam/freezhal/blob/master/lib/utils.coffee#L91-L94
//
// from utility.js
var Iconv = require('iconv').Iconv;
var toUTF8 = new Iconv('EUC-KR', 'UTF-8');
exports.decodeEuckrUrlToUTF8 = function(str) {
var toHex = function(n) {
return parseInt('0x' + n);
};
if (!str) str = '';
return str.replace(/(%([^%]{2}))+/g, function(chars) {
var b;
b = new Buffer(chars.split('%').slice(1).map(toHex));
return toUTF8.convert(b).toString();
});
};
// from outside
var convertEUCKR = require('./utility').decodeEuckrUrlToUTF8;
params.list = params.list.map(function (item) {
item.name = convertEUCKR(item.name);
item.job = convertEUCKR(item.job);
item.quiz1 = convertEUCKR(item.quiz1);
item.quiz2 = convertEUCKR(item.quiz2);
item.gender = item.gender == 1 ? '남' : '여';
});
console.log(params.list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment