From ed19e670559b4096bbe01b8d7013dd6dcca51e19 Mon Sep 17 00:00:00 2001 From: Tejas Manohar Date: Thu, 8 Oct 2015 23:01:32 -0500 Subject: [PATCH] refactor to use ES6 template strings replace string interp w/ templates in core use string templating es6 in benchmarks template strings in tests dir --- benchmarks/experimental/async.js | 2 +- benchmarks/middleware.js | 2 +- lib/application.js | 2 +- lib/context.js | 2 +- lib/request.js | 4 ++-- lib/response.js | 8 ++++---- test/response/redirect.js | 12 ++++++------ 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/benchmarks/experimental/async.js b/benchmarks/experimental/async.js index d7ea4a2..ab2e19e 100644 --- a/benchmarks/experimental/async.js +++ b/benchmarks/experimental/async.js @@ -10,7 +10,7 @@ app.experimental = true; // number of middleware const n = parseInt(process.env.MW || '1', 10); -console.log(' %s async middleware', n); +console.log(` ${n} async middleware`); while (n--) { app.use(async function (next){ diff --git a/benchmarks/middleware.js b/benchmarks/middleware.js index 75d5f4a..b60beb6 100644 --- a/benchmarks/middleware.js +++ b/benchmarks/middleware.js @@ -8,7 +8,7 @@ const app = koa(); // number of middleware const n = parseInt(process.env.MW || '1', 10); -console.log(' %s middleware', n); +console.log(` ${n} middleware`); while (n--) { app.use(function *(next){ diff --git a/lib/application.js b/lib/application.js index fc01a6c..6ef0600 100644 --- a/lib/application.js +++ b/lib/application.js @@ -165,7 +165,7 @@ app.createContext = function(req, res){ */ app.onerror = function(err){ - assert(err instanceof Error, 'non-error thrown: ' + err); + assert(err instanceof Error, `non-error thrown: ${err}`); if (404 == err.status || err.expose) return; if (this.silent) return; diff --git a/lib/context.js b/lib/context.js index 5f25161..47f71bd 100644 --- a/lib/context.js +++ b/lib/context.js @@ -104,7 +104,7 @@ const proto = module.exports = { // to node-style callbacks. if (null == err) return; - if (!(err instanceof Error)) err = new Error('non-error thrown: ' + err); + if (!(err instanceof Error)) err = new Error(`non-error thrown: ${err}`); // delegate this.app.emit('error', err, this); diff --git a/lib/request.js b/lib/request.js index 93715a4..4d80318 100644 --- a/lib/request.js +++ b/lib/request.js @@ -69,7 +69,7 @@ module.exports = { */ get origin() { - return this.protocol + '://' + this.host; + return `${this.protocol}://${this.host}`; }, /** @@ -196,7 +196,7 @@ module.exports = { get search() { if (!this.querystring) return ''; - return '?' + this.querystring; + return `?${this.querystring}`; }, /** diff --git a/lib/response.js b/lib/response.js index 195aa41..1b27bed 100644 --- a/lib/response.js +++ b/lib/response.js @@ -80,7 +80,7 @@ module.exports = { set status(code) { assert('number' == typeof code, 'status code must be a number'); - assert(statuses[code], 'invalid status code: ' + code); + assert(statuses[code], `invalid status code: ${code}`); this._explicitStatus = true; this.res.statusCode = code; this.res.statusMessage = statuses[code]; @@ -263,13 +263,13 @@ module.exports = { if (this.ctx.accepts('html')) { url = escape(url); this.type = 'text/html; charset=utf-8'; - this.body = 'Redirecting to ' + url + '.'; + this.body = `Redirecting to ${url}.`; return; } // text this.type = 'text/plain; charset=utf-8'; - this.body = 'Redirecting to ' + url + '.'; + this.body = `Redirecting to ${url}.`; }, /** @@ -344,7 +344,7 @@ module.exports = { */ set etag(val) { - if (!/^(W\/)?"/.test(val)) val = '"' + val + '"'; + if (!/^(W\/)?"/.test(val)) val = `"${val}"`; this.set('ETag', val); }, diff --git a/test/response/redirect.js b/test/response/redirect.js index 8eba957..68ed47b 100644 --- a/test/response/redirect.js +++ b/test/response/redirect.js @@ -46,7 +46,7 @@ describe('ctx.redirect(url)', function(){ ctx.header.accept = 'text/html'; ctx.redirect(url); ctx.response.header['content-type'].should.equal('text/html; charset=utf-8'); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }) it('should escape the url', function(){ @@ -56,7 +56,7 @@ describe('ctx.redirect(url)', function(){ ctx.redirect(url); url = escape(url); ctx.response.header['content-type'].should.equal('text/html; charset=utf-8'); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }) }) @@ -66,7 +66,7 @@ describe('ctx.redirect(url)', function(){ const url = 'http://google.com'; ctx.header.accept = 'text/plain'; ctx.redirect(url); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }) }) @@ -78,7 +78,7 @@ describe('ctx.redirect(url)', function(){ ctx.header.accept = 'text/plain'; ctx.redirect('http://google.com'); ctx.status.should.equal(301); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }) }) @@ -90,7 +90,7 @@ describe('ctx.redirect(url)', function(){ ctx.header.accept = 'text/plain'; ctx.redirect('http://google.com'); ctx.status.should.equal(302); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }) }) @@ -102,7 +102,7 @@ describe('ctx.redirect(url)', function(){ ctx.header.accept = 'text/plain'; ctx.redirect('http://google.com'); ctx.status.should.equal(302); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); ctx.type.should.equal('text/plain'); }) })