Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use absolute equal for string comparison #1251

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ ctx.body = await db.find('something');

### request.secure

Shorthand for `ctx.protocol == "https"` to check if a request was
Shorthand for `ctx.protocol === "https"` to check if a request was
issued via TLS.

### request.ip
Expand Down
6 changes: 3 additions & 3 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,23 @@ function logger(format) {
const compose = require('koa-compose');

async function random(ctx, next) {
if ('/random' == ctx.path) {
if ('/random' === ctx.path) {
ctx.body = Math.floor(Math.random() * 10);
} else {
await next();
}
};

async function backwards(ctx, next) {
if ('/backwards' == ctx.path) {
if ('/backwards' === ctx.path) {
ctx.body = 'sdrawkcab';
} else {
await next();
}
}

async function pi(ctx, next) {
if ('/pi' == ctx.path) {
if ('/pi' === ctx.path) {
ctx.body = String(Math.PI);
} else {
await next();
Expand Down
6 changes: 3 additions & 3 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ module.exports = class Application extends Emitter {
onerror(err) {
if (!(err instanceof Error)) throw new TypeError(util.format('non-error thrown: %j', err));

if (404 == err.status || err.expose) return;
if (404 === err.status || err.expose) return;
if (this.silent) return;

const msg = err.stack || err.toString();
Expand Down Expand Up @@ -213,7 +213,7 @@ function respond(ctx) {
return res.end();
}

if ('HEAD' == ctx.method) {
if ('HEAD' === ctx.method) {
if (!res.headersSent && isJSON(body)) {
ctx.length = Buffer.byteLength(JSON.stringify(body));
}
Expand All @@ -232,7 +232,7 @@ function respond(ctx) {

// responses
if (Buffer.isBuffer(body)) return res.end(body);
if ('string' == typeof body) return res.end(body);
if ('string' === typeof body) return res.end(body);
if (body instanceof Stream) return body.pipe(res);

// body: json
Expand Down
2 changes: 1 addition & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const proto = module.exports = {
this.type = 'text';

// ENOENT support
if ('ENOENT' == err.code) err.status = 404;
if ('ENOENT' === err.code) err.status = 404;

// default to 500
if ('number' != typeof err.status || !statuses[err.status]) err.status = 500;
Expand Down
10 changes: 5 additions & 5 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ module.exports = {
get hostname() {
const host = this.host;
if (!host) return '';
if ('[' == host[0]) return this.URL.hostname || ''; // IPv6
if ('[' === host[0]) return this.URL.hostname || ''; // IPv6
return host.split(':')[0];
},

Expand Down Expand Up @@ -313,7 +313,7 @@ module.exports = {
if ('GET' != method && 'HEAD' != method) return false;

// 2xx or 304 as per rfc2616 14.26
if ((s >= 200 && s < 300) || 304 == s) {
if ((s >= 200 && s < 300) || 304 === s) {
return fresh(this.header, this.response.header);
}

Expand Down Expand Up @@ -385,7 +385,7 @@ module.exports = {

get length() {
const len = this.get('Content-Length');
if (len == '') return;
if (len === '') return;
return ~~len;
},

Expand All @@ -411,14 +411,14 @@ module.exports = {
/**
* Short-hand for:
*
* this.protocol == 'https'
* this.protocol === 'https'
*
* @return {Boolean}
* @api public
*/

get secure() {
return 'https' == this.protocol;
return 'https' === this.protocol;
},

/**
Expand Down
14 changes: 7 additions & 7 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = {
set status(code) {
if (this.headerSent) return;

assert('number' == typeof code, 'status code must be a number');
assert('number' === typeof code, 'status code must be a number');
assert(statuses[code], `invalid status code: ${code}`);
this._explicitStatus = true;
this.res.statusCode = code;
Expand Down Expand Up @@ -151,7 +151,7 @@ module.exports = {
const setType = !this.header['content-type'];

// string
if ('string' == typeof val) {
if ('string' === typeof val) {
if (setType) this.type = /^\s*</.test(val) ? 'html' : 'text';
this.length = Buffer.byteLength(val);
return;
Expand All @@ -165,7 +165,7 @@ module.exports = {
}

// stream
if ('function' == typeof val.pipe) {
if ('function' === typeof val.pipe) {
onFinish(this.res, destroy.bind(null, val));
ensureErrorHandler(val, err => this.ctx.onerror(err));

Expand Down Expand Up @@ -205,7 +205,7 @@ module.exports = {

if (null == len) {
if (!body) return;
if ('string' == typeof body) return Buffer.byteLength(body);
if ('string' === typeof body) return Buffer.byteLength(body);
if (Buffer.isBuffer(body)) return body.length;
if (isJSON(body)) return Buffer.byteLength(JSON.stringify(body));
return;
Expand Down Expand Up @@ -259,7 +259,7 @@ module.exports = {

redirect(url, alt) {
// location
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
if ('back' === url) url = this.ctx.get('Referrer') || alt || '/';
this.set('Location', url);

// status
Expand Down Expand Up @@ -326,7 +326,7 @@ module.exports = {
*/

set lastModified(val) {
if ('string' == typeof val) val = new Date(val);
if ('string' === typeof val) val = new Date(val);
this.set('Last-Modified', val.toUTCString());
},

Expand Down Expand Up @@ -438,7 +438,7 @@ module.exports = {
set(field, val) {
if (this.headerSent) return;

if (2 == arguments.length) {
if (2 === arguments.length) {
if (Array.isArray(val)) val = val.map(v => typeof v === 'string' ? v : String(v));
else if (typeof val !== 'string') val = String(val);
this.res.setHeader(field, val);
Expand Down
2 changes: 1 addition & 1 deletion test/application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('app', () => {
const app = new Koa();

app.use((ctx, next) => {
// triggers ctx.socket.writable == false
// triggers ctx.socket.writable === false
ctx.socket.emit('error', new Error('boom'));
});

Expand Down