forked from svrjs/svrjs
Optimize fixNodeMojibakeURL function
This commit is contained in:
parent
486820e1da
commit
a4ca117020
1 changed files with 11 additions and 16 deletions
|
@ -1,21 +1,16 @@
|
||||||
// Node.JS mojibake URL fixing function
|
// Node.JS mojibake URL fixing function
|
||||||
function fixNodeMojibakeURL(string) {
|
function fixNodeMojibakeURL(string) {
|
||||||
var encoded = "";
|
return Buffer.from(string, "latin1")
|
||||||
|
.reduce((result, value) => {
|
||||||
//Encode URLs
|
if (value > 127) {
|
||||||
Buffer.from(string, "latin1").forEach((value) => {
|
result +=
|
||||||
if (value > 127) {
|
"%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase();
|
||||||
encoded +=
|
} else {
|
||||||
"%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase();
|
result += String.fromCharCode(value);
|
||||||
} else {
|
}
|
||||||
encoded += String.fromCodePoint(value);
|
return result;
|
||||||
}
|
}, "")
|
||||||
});
|
.replace(/%[0-9a-f]{2}/gi, (match) => match.toUpperCase());
|
||||||
|
|
||||||
//Upper case the URL encodings
|
|
||||||
return encoded.replace(/%[0-9a-f-A-F]{2}/g, (match) => {
|
|
||||||
return match.toUpperCase();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = fixNodeMojibakeURL;
|
module.exports = fixNodeMojibakeURL;
|
||||||
|
|
Reference in a new issue