Fixed characters — Returns whole words if possible and doesnt cut

This commit is contained in:
Mahdi Dibaiee 2014-09-07 12:34:23 +04:30
parent 8be73ae624
commit 80ff9fd1b7

View File

@ -6,8 +6,8 @@ app.use(express.static(__dirname + '/ipsum', {maxAge: 60*60*24*7}));
module.exports.app = app; module.exports.app = app;
app.get(/.*\/.*\/.*/, function(req, res) { app.get(/.*\/.*\/.*/, function(req, res) {
res.charset = 'utf-8';
res.end(go(req.url)); res.end('<head><meta charset="utf-8"></head>' + go(req.url));
}); });
if(!String.prototype.repeat) { if(!String.prototype.repeat) {
@ -59,7 +59,15 @@ function loremipsum(data) {
switch(unit) { switch(unit) {
case 'c': case 'c':
r = stretch(r, amount/2); r = stretch(r, amount/2);
return r.slice(0, amount/2).join(' ').slice(0, amount + (amount/4-1)); var c = 0;
r = r.filter(function(a) {
if(c < amount && c + a.length <= amount) {
c += a.length;
return true;
}
return false;
});
return r.slice(0, Math.round(amount/2)).join(' ');
case 'w': case 'w':
r = stretch(r, amount); r = stretch(r, amount);
return r.slice(0, amount).join(' '); return r.slice(0, amount).join(' ');
@ -79,3 +87,5 @@ function go(url) {
var req = url.split('/'); var req = url.split('/');
return loremipsum(req.slice(1)); return loremipsum(req.slice(1));
} }
app.listen(8008);