Created
October 14, 2013 03:12
-
-
Save soomtong/6970113 to your computer and use it in GitHub Desktop.
convert UTF-8 from URIEncoded EUC-KR
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
// 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