Last active
February 1, 2017 19:37
-
-
Save alukinykh/176aa5d68ba25b37a7f74b09d95694fc to your computer and use it in GitHub Desktop.
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 express = require('express'); | |
var app = express(); | |
app.get('/', function (req, res) { | |
res.json({sum: parseInt(req.query.a) + parseInt(req.query.b)}); | |
}); | |
app.listen(3000, function () { | |
console.log('Example app listening on port 3000!') | |
}); | |
//encode url | |
function encodeSymbols (url) { | |
var new_url = []; | |
for (var i = 0; i < url.length; i++) { | |
if(!(url[i].match(/[a-z]|[A-Z]|[0-9]|[!*'();:@&=+$,/?#]/gi))) { | |
new_url.push(encode_utf8(url[i])); | |
// new_url.push(encodeURIComponent(url[i].charAt(0))); | |
} | |
else { | |
new_url.push(url[i]); | |
} | |
} | |
return new_url.join(''); | |
} | |
console.log(encodeSymbols('https://ru.wikipedia.org/wiki/Микрокредит')); | |
//Final result of encoding function | |
function encodeSymbols (url) { | |
var new_url = []; | |
for (var i = 0; i < url.length; i++) { | |
if(!(url[i].match(/[a-z]|[A-Z]|[0-9]|[!*'();:@&=+$,./?#]/gi))) { | |
new_url.push(toUnicode(url[i])); | |
} | |
else { | |
new_url.push(url[i]); | |
} | |
} | |
return new_url.join(''); | |
} | |
function toUnicode(str){ | |
var result = ""; | |
var partial = str.charCodeAt(0).toString(16); | |
while(partial.length !== 4) partial = "0" + partial; | |
partial = partial.substr(0, 2) + '%' + partial.substr(2, 2) + '%'; | |
return partial; | |
}; | |
function encode_utf8 (str) { | |
var result = ''; | |
var symbol = str.charCodeAt(0); | |
if(symbol < 128) { | |
result = String.fromCharCode(symbol); | |
} | |
else if ((symbol > 127) && (symbol < 2048)) { | |
console.log(String.fromCharCode((symbol >> 6) | 192)); | |
result += String.fromCharCode((symbol >> 6) | 192) + '%'; | |
result += String.fromCharCode((symbol & 63) | 128) + '%'; | |
} | |
return result; | |
} | |
console.log(encodeSymbols("https://ru.wikipedia.org/wiki/Микрокредит")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment