From 80ff9fd1b72ffb5818635f7bf1760484d2e9d9bc Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Sun, 7 Sep 2014 12:34:23 +0430 Subject: [PATCH] =?UTF-8?q?Fixed=20characters=20=E2=80=94=20Returns=20whol?= =?UTF-8?q?e=20words=20if=20possible=20and=20doesnt=20cut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ipsum.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ipsum.js b/ipsum.js index 6339afb..97c4633 100644 --- a/ipsum.js +++ b/ipsum.js @@ -6,8 +6,8 @@ app.use(express.static(__dirname + '/ipsum', {maxAge: 60*60*24*7})); module.exports.app = app; app.get(/.*\/.*\/.*/, function(req, res) { - - res.end(go(req.url)); + res.charset = 'utf-8'; + res.end('' + go(req.url)); }); if(!String.prototype.repeat) { @@ -59,7 +59,15 @@ function loremipsum(data) { switch(unit) { case 'c': 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': r = stretch(r, amount); return r.slice(0, amount).join(' '); @@ -79,3 +87,5 @@ function go(url) { var req = url.split('/'); return loremipsum(req.slice(1)); } + +app.listen(8008);