Skip to content

Commit

Permalink
Simplify various null comparisons (#1108)
Browse files Browse the repository at this point in the history
* remove `!= null` where unnecessary
* replace `x != null && x.y ===` with `x?.y ===`
* replace `x != null ? x :` with `x ??`

Co-authored-by: alxndrsn <alxndrsn>
  • Loading branch information
alxndrsn authored Mar 20, 2024
1 parent 85a0e3c commit bec0f96
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/data/briefcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const processRow = (xml, instanceId, fields, header, selectValues) => new Promis
},
ontext: (text) => {
const field = schemaStack.head();
if ((field != null) && (field.idx != null)) {
if (field?.idx != null) {
// we have a real schema field for this text value and a place to put the
// value, so inject it into the appropriate spot in the row.

Expand Down Expand Up @@ -151,7 +151,7 @@ const processRow = (xml, instanceId, fields, header, selectValues) => new Promis
const row = dataStack.pop();

// if we popped a repeat, we need to write the subrow we just created.
if ((field != null) && (field.type === 'repeat'))
if (field?.type === 'repeat')
field.stream.write(row);

// if we popped everything, we've hit the close tag. write out a few special
Expand Down
2 changes: 1 addition & 1 deletion lib/formats/odata.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ const rowStreamToOData = (fields, table, domain, originalUrl, query, inStream, t

// if we were given an explicit count, use it from here out, to create
// @odata.count and nextUrl.
const totalCount = (tableCount != null) ? tableCount : counted;
const totalCount = tableCount ?? counted;

// How many items are remaining for the next page?
// if there aren't any then we don't need nextUrl
Expand Down
2 changes: 1 addition & 1 deletion lib/http/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const isWriteRequest = (request) => !phony(request) &&
(request.method === 'PATCH') || (request.method === 'DELETE'));

// quick and dirty function to wrap plain values in a promise.
const ensurePromise = (x) => (((x != null) && (typeof x.then === 'function')) ? x : resolve(x));
const ensurePromise = (x) => ((typeof x?.then === 'function') ? x : resolve(x));

// allows Streams, response-mutating functions, and Problems to be directly returned
// by a resource handler, and does sane thinsg with each.
Expand Down
2 changes: 1 addition & 1 deletion lib/http/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module.exports = (container) => {
if (response.headersSent === true) {
// In this case, we'll just let Express fail the request out.
next(error);
} else if ((error != null) && (error.type === 'entity.parse.failed')) {
} else if (error?.type === 'entity.parse.failed') {
// catch body-parser middleware problems. we only ask it to parse JSON, which
// isn't part of OpenRosa, so we can assume a plain JSON response.
defaultErrorWriter(Problem.user.unparseable({ format: 'json', rawLength: error.body.length }), request, response);
Expand Down
2 changes: 1 addition & 1 deletion lib/model/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Container {
const injector = (base, queries) => {
// The container is the object that contains all of our (inter-)dependent objects.
const container = new Container(base);
if ((base != null) && (base.db != null)) queryFuncs(base.db, container);
if (base?.db != null) queryFuncs(base.db, container);

// Inject container context to query modules.
for (const queryModule of Object.keys(queries))
Expand Down
2 changes: 1 addition & 1 deletion lib/model/frames/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Config extends Frame.define(
'setAt', readable,
species('config')
) {
static forKey(key) { return frames[key] != null ? frames[key] : this; }
static forKey(key) { return frames[key] ?? this; }
}

/*
Expand Down
4 changes: 2 additions & 2 deletions lib/model/frames/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Form extends Frame.define(
) {
get def() { return this.aux.def; }
get xls() { return this.aux.xls || {}; } // TODO/SL sort of a hack but let's see how it goes.
get xml() { return (this.aux.xml != null) ? this.aux.xml.xml : undefined; }
get xml() { return this.aux.xml?.xml; }

acceptsSubmissions() { return (this.state === 'open') || (this.state === 'closing'); }

Expand All @@ -85,7 +85,7 @@ class Form extends Frame.define(

forApi() {
const enketoId = this._enketoIdForApi();
const enketoOnceId = this.def != null && this.def.id === this.currentDefId
const enketoOnceId = this.def?.id === this.currentDefId
? this.enketoOnceId
: null;

Expand Down

0 comments on commit bec0f96

Please sign in to comment.