Use optional chaining operator ?.

https://caniuse.com/mdn-javascript_operators_optional_chaining
This commit is contained in:
Simon Legner 2024-01-06 16:53:51 +01:00
parent eea754d19a
commit f364659d97
9 changed files with 11 additions and 21 deletions

View file

@ -66,8 +66,7 @@ app.Router = class Router extends Events {
type(context, next) {
const doc = app.docs.findBySlug(context.params.doc);
const type =
doc != null ? doc.types.findBy("slug", context.params.type) : undefined;
const type = doc?.types?.findBy("slug", context.params.type);
if (type) {
context.doc = doc;

View file

@ -312,10 +312,7 @@ app.Searcher = class Searcher extends Events {
}
scoredEnough() {
return (
(this.scoreMap[100] != null ? this.scoreMap[100].length : undefined) >=
this.options.max_results
);
return this.scoreMap[100]?.length >= this.options.max_results;
}
foundEnough() {
@ -388,7 +385,7 @@ app.SynchronousSearcher = class SynchronousSearcher extends app.Searcher {
}
sendResults(end) {
if (end && (this.allResults != null ? this.allResults.length : undefined)) {
if (end && this.allResults?.length) {
return this.triggerResults(this.allResults);
}
}

View file

@ -198,10 +198,7 @@ app.Settings = class Settings {
toggleLayout(layout, enable) {
const { classList } = document.body;
// sidebar is always shown for settings; its state is updated in app.views.Settings
if (
layout !== "_sidebar-hidden" ||
!(app.router != null ? app.router.isSettings : undefined)
) {
if (layout !== "_sidebar-hidden" || !app.router?.isSettings) {
classList.toggle(layout, enable);
}
classList.toggle("_overlay-scrollbars", $.overlayScrollbarsEnabled());

View file

@ -43,7 +43,7 @@ page.stop = function () {
page.show = function (path, state) {
let res;
if (path === (currentState != null ? currentState.path : undefined)) {
if (path === currentState?.path) {
return;
}
const context = new Context(path, state);

View file

@ -453,8 +453,7 @@ $.arrayDelete = function (array, object) {
// Returns true if the object is an array or a collection of DOM elements.
$.isCollection = (object) =>
Array.isArray(object) ||
typeof (object != null ? object.item : undefined) === "function";
Array.isArray(object) || typeof object?.item === "function";
const ESCAPE_HTML_MAP = {
"&": "&",

View file

@ -144,9 +144,7 @@ app.views.EntryPage = class EntryPage extends app.View {
}
onRoute(context) {
const isSameFile =
context.entry.filePath() ===
(this.entry != null ? this.entry.filePath() : undefined);
const isSameFile = context.entry.filePath() === this.entry?.filePath?.();
this.entry = context.entry;
if (!isSameFile) {
this.restore() || this.load();

View file

@ -35,7 +35,7 @@ app.views.SearchScope = class SearchScope extends app.View {
}
name() {
return this.doc != null ? this.doc.name : undefined;
return this.doc?.name;
}
search(value, searchDisabled) {

View file

@ -83,7 +83,7 @@ app.views.DocList = class DocList extends app.View {
var versions = "";
while (true) {
versions += this.tmpl("sidebarDoc", doc, { disabled: true });
if ((docs[0] != null ? docs[0].name : undefined) !== doc.name) {
if (docs[0]?.name !== doc.name) {
break;
}
doc = docs.shift();
@ -144,7 +144,7 @@ app.views.DocList = class DocList extends app.View {
}
select(model) {
this.listSelect.selectByHref(model != null ? model.fullPath() : undefined);
this.listSelect.selectByHref(model?.fullPath());
}
reveal(model) {

View file

@ -79,7 +79,7 @@ app.views.DocPicker = class DocPicker extends app.View {
getSelectedDocs() {
return this.findAllByTag("input")
.filter((input) => (input != null ? input.checked : undefined))
.filter((input) => input?.checked)
.map((input) => input.name);
}