Browse Source

Use optional chaining operator ?.

https://caniuse.com/mdn-javascript_operators_optional_chaining
Simon Legner 1 year ago
parent
commit
f364659d97

+ 1 - 2
assets/javascripts/app/router.js

@@ -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;

+ 2 - 5
assets/javascripts/app/searcher.js

@@ -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);
     }
   }

+ 1 - 4
assets/javascripts/app/settings.js

@@ -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());

+ 1 - 1
assets/javascripts/lib/page.js

@@ -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);

+ 1 - 2
assets/javascripts/lib/util.js

@@ -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 = {
   "&": "&",

+ 1 - 3
assets/javascripts/views/content/entry_page.js

@@ -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();

+ 1 - 1
assets/javascripts/views/search/search_scope.js

@@ -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) {

+ 2 - 2
assets/javascripts/views/sidebar/doc_list.js

@@ -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) {

+ 1 - 1
assets/javascripts/views/sidebar/doc_picker.js

@@ -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);
   }