Skip to content

Commit 8bec6ea

Browse files
author
Chris Sidebottom
committed
Reduce dependencies and use more ES6ness
1 parent da24c9f commit 8bec6ea

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
var _ = require('lodash');
2-
var util = require('util');
3-
41
module.exports = function (defaults) {
52
return function cacheControl(ctx, next) {
63
return next().then(function () {
7-
var options = _.defaults(ctx.cacheControl || {}, defaults);
4+
var options = ctx.cacheControl || defaults || {};
85
var cacheControl = [];
96

107
if (options.private) {
@@ -36,20 +33,20 @@ module.exports = function (defaults) {
3633
cacheControl.push('must-revalidate');
3734
} else if (!options.noCache) {
3835
if (options.staleIfError) {
39-
cacheControl.push(util.format('stale-if-error=%d', options.staleIfError));
36+
cacheControl.push(`stale-if-error=${options.staleIfError}`);
4037
}
4138

4239
if (options.staleWhileRevalidate) {
43-
cacheControl.push(util.format('stale-while-revalidate=%d', options.staleWhileRevalidate));
40+
cacheControl.push(`stale-while-revalidate=${options.staleWhileRevalidate}`);
4441
}
4542
}
4643

47-
if (_.isNumber(options.maxAge)) {
48-
cacheControl.push(util.format('max-age=%d', options.maxAge));
44+
if (Number.isInteger(options.maxAge)) {
45+
cacheControl.push(`max-age=${options.maxAge}`);
4946
}
5047

51-
if (_.isNumber(options.sMaxAge)) {
52-
cacheControl.push(util.format('s-maxage=%d', options.sMaxAge));
48+
if (Number.isInteger(options.sMaxAge)) {
49+
cacheControl.push(`s-maxage=${options.sMaxAge}`);
5350
}
5451

5552
if (cacheControl.length) {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@
3131
"supertest": "^2.0.0"
3232
},
3333
"dependencies": {
34-
"lodash": "^4.12.0"
3534
}
3635
}

test/index.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var request = require('supertest');
2-
var koa = require('koa');
3-
var cacheControl = require('..');
4-
var fs = require('fs');
1+
const request = require('supertest');
2+
const koa = require('koa');
3+
const cacheControl = require('..');
4+
const fs = require('fs');
55

66
describe('cacheControl()', function () {
77
describe('default configuration', function () {
88
it('uses defaults if nothing defined on request', function (done) {
9-
var app = new koa();
9+
const app = new koa();
1010

1111
app.use(cacheControl({
1212
maxAge: 4
@@ -25,7 +25,7 @@ describe('cacheControl()', function () {
2525

2626
describe('override default configuration', function () {
2727
it('allows middleware to override options in incoming requests', function (done) {
28-
var app = new koa();
28+
const app = new koa();
2929

3030
app.use(cacheControl({
3131
maxAge: 4
@@ -46,7 +46,7 @@ describe('cacheControl()', function () {
4646
})
4747

4848
it('allows middleware to override options on outgoing requests', function (done) {
49-
var app = new koa();
49+
const app = new koa();
5050

5151
app.use(cacheControl({
5252
maxAge: 300
@@ -73,7 +73,7 @@ describe('cacheControl()', function () {
7373

7474
describe('public is set', function () {
7575
it('adds public flag to cache-control header', function (done) {
76-
var app = new koa();
76+
const app = new koa();
7777

7878
app.use(cacheControl({
7979
public: true
@@ -92,7 +92,7 @@ describe('cacheControl()', function () {
9292

9393
describe('private is set', function () {
9494
it('adds private flag to cache-control header', function (done) {
95-
var app = new koa();
95+
const app = new koa();
9696

9797
app.use(cacheControl({
9898
private: true
@@ -109,7 +109,7 @@ describe('cacheControl()', function () {
109109
});
110110

111111
it('discards public flag in cache-control header', function (done) {
112-
var app = new koa();
112+
const app = new koa();
113113

114114
app.use(cacheControl({
115115
private: true,
@@ -129,7 +129,7 @@ describe('cacheControl()', function () {
129129

130130
describe('maxAge is set', function () {
131131
it('sets cache-control max-age section', function (done) {
132-
var app = new koa();
132+
const app = new koa();
133133

134134
app.use(cacheControl({
135135
maxAge: 4
@@ -148,7 +148,7 @@ describe('cacheControl()', function () {
148148

149149
describe('staleIfError is set', function () {
150150
it('sets cache-control header with stale-if-error', function (done) {
151-
var app = new koa();
151+
const app = new koa();
152152

153153
app.use(cacheControl({
154154
staleIfError: 320
@@ -167,7 +167,7 @@ describe('cacheControl()', function () {
167167

168168
describe('staleWhileRevalidate is set', function () {
169169
it('sets cache-control header with stale-while-revalidate', function (done) {
170-
var app = new koa();
170+
const app = new koa();
171171

172172
app.use(cacheControl({
173173
staleWhileRevalidate: 320
@@ -186,7 +186,7 @@ describe('cacheControl()', function () {
186186

187187
describe('mustRevalidate is set', function () {
188188
it('sets cache-control header with must-revalidate', function (done) {
189-
var app = new koa();
189+
const app = new koa();
190190

191191
app.use(cacheControl({
192192
mustRevalidate: true
@@ -203,7 +203,7 @@ describe('cacheControl()', function () {
203203
});
204204

205205
it('overthrows stale-while-revalidate and stale-if-error', function (done) {
206-
var app = new koa();
206+
const app = new koa();
207207

208208
app.use(cacheControl({
209209
mustRevalidate: true,
@@ -224,7 +224,7 @@ describe('cacheControl()', function () {
224224

225225
describe('when noCache is true', function () {
226226
it('adds no-cache to Cache-Control header', function (done) {
227-
var app = new koa();
227+
const app = new koa();
228228

229229
app.use(cacheControl({
230230
noCache: true
@@ -241,7 +241,7 @@ describe('cacheControl()', function () {
241241
});
242242

243243
it('sets maxAge to 0', function (done) {
244-
var app = new koa();
244+
const app = new koa();
245245

246246
app.use(cacheControl({
247247
noCache: true,
@@ -259,7 +259,7 @@ describe('cacheControl()', function () {
259259
});
260260

261261
it('removes sMaxAge', function (done) {
262-
var app = new koa();
262+
const app = new koa();
263263

264264
app.use(cacheControl({
265265
noCache: true,
@@ -277,7 +277,7 @@ describe('cacheControl()', function () {
277277
});
278278

279279
it('ignores stale settings', function (done) {
280-
var app = new koa();
280+
const app = new koa();
281281

282282
app.use(cacheControl({
283283
noCache: true,
@@ -298,7 +298,7 @@ describe('cacheControl()', function () {
298298

299299
describe('when noStore is true', function () {
300300
it('sets Cache-Control no-store and no-cache', function (done) {
301-
var app = new koa();
301+
const app = new koa();
302302

303303
app.use(cacheControl({
304304
noStore: true
@@ -315,7 +315,7 @@ describe('cacheControl()', function () {
315315
});
316316

317317
it('sets maxAge to 0', function (done) {
318-
var app = new koa();
318+
const app = new koa();
319319

320320
app.use(cacheControl({
321321
noStore: true,
@@ -335,7 +335,7 @@ describe('cacheControl()', function () {
335335

336336
describe('when noTransform is set', function () {
337337
it('sets Cache-Control no-transform', function (done) {
338-
var app = new koa();
338+
const app = new koa();
339339

340340
app.use(cacheControl({
341341
noTransform: true
@@ -354,7 +354,7 @@ describe('cacheControl()', function () {
354354

355355
describe('when proxyRevalidate', function () {
356356
it('sets Cache-Control proxy-revalidate', function (done) {
357-
var app = new koa();
357+
const app = new koa();
358358

359359
app.use(cacheControl({
360360
proxyRevalidate: true
@@ -374,7 +374,7 @@ describe('cacheControl()', function () {
374374

375375
describe('when sMaxAge', function () {
376376
it('sets Cache-Control s-maxage property', function (done) {
377-
var app = new koa();
377+
const app = new koa();
378378

379379
app.use(cacheControl({
380380
sMaxAge: 10
@@ -393,7 +393,7 @@ describe('cacheControl()', function () {
393393

394394
describe('when no cache properties set', function () {
395395
it('does not set a cache-control header', function (done) {
396-
var app = new koa();
396+
const app = new koa();
397397

398398
app.use(cacheControl());
399399

0 commit comments

Comments
 (0)