From 808c43377e309323b1cf463d4f5002d90ab3874f Mon Sep 17 00:00:00 2001 From: Johann150 Date: Mon, 10 Oct 2022 23:03:24 +0200 Subject: [PATCH] refactor: use nullish coalescing & optional chaining Changes several places to use the afforementioned constructs in favour of ternary expressions. --- .../backend/src/server/api/endpoints/drive/folders/create.ts | 2 +- packages/backend/src/services/drive/add-file.ts | 2 +- packages/client/src/components/drive.nav-folder.vue | 2 +- packages/client/src/components/launch-pad.vue | 2 +- packages/client/src/filters/number.ts | 2 +- packages/client/src/pages/settings/sounds.vue | 2 +- packages/client/src/scripts/scroll.ts | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/drive/folders/create.ts b/packages/backend/src/server/api/endpoints/drive/folders/create.ts index ebbc23289..6cb161a7f 100644 --- a/packages/backend/src/server/api/endpoints/drive/folders/create.ts +++ b/packages/backend/src/server/api/endpoints/drive/folders/create.ts @@ -56,7 +56,7 @@ export default define(meta, paramDef, async (ps, user) => { id: genId(), createdAt: new Date(), name: ps.name, - parentId: parent !== null ? parent.id : null, + parentId: parent?.id, userId: user.id, }).then(x => DriveFolders.findOneByOrFail(x.identifiers[0])); diff --git a/packages/backend/src/services/drive/add-file.ts b/packages/backend/src/services/drive/add-file.ts index 45d53492d..ee49f6ae5 100644 --- a/packages/backend/src/services/drive/add-file.ts +++ b/packages/backend/src/services/drive/add-file.ts @@ -425,7 +425,7 @@ export async function addFile({ file.createdAt = new Date(); file.userId = user ? user.id : null; file.userHost = user ? user.host : null; - file.folderId = folder !== null ? folder.id : null; + file.folderId = folder?.id; file.comment = comment; file.properties = properties; file.blurhash = info.blurhash || null; diff --git a/packages/client/src/components/drive.nav-folder.vue b/packages/client/src/components/drive.nav-folder.vue index 304d7b008..71174ea4d 100644 --- a/packages/client/src/components/drive.nav-folder.vue +++ b/packages/client/src/components/drive.nav-folder.vue @@ -9,7 +9,7 @@ @drop.stop="onDrop" > - {{ folder == null ? i18n.ts.drive : folder.name }} + {{ folder?.name ?? i18n.ts.drive }} diff --git a/packages/client/src/components/launch-pad.vue b/packages/client/src/components/launch-pad.vue index bd5494739..2733903cb 100644 --- a/packages/client/src/components/launch-pad.vue +++ b/packages/client/src/components/launch-pad.vue @@ -60,7 +60,7 @@ const modal = $ref>(); const menu = defaultStore.state.menu; -const items = Object.keys(menuDef).filter(k => !menu.includes(k)).map(k => menuDef[k]).filter(def => def.show == null ? true : def.show).map(def => ({ +const items = Object.keys(menuDef).filter(k => !menu.includes(k)).map(k => menuDef[k]).filter(def => def.show ?? true).map(def => ({ type: def.to ? 'link' : 'button', text: i18n.ts[def.title], icon: def.icon, diff --git a/packages/client/src/filters/number.ts b/packages/client/src/filters/number.ts index 880a848ca..11bb71bc5 100644 --- a/packages/client/src/filters/number.ts +++ b/packages/client/src/filters/number.ts @@ -1 +1 @@ -export default n => n == null ? 'N/A' : n.toLocaleString(); +export default n => n?.toLocaleString() ?? 'N/A'; diff --git a/packages/client/src/pages/settings/sounds.vue b/packages/client/src/pages/settings/sounds.vue index 0e686d0f2..4e2ee6588 100644 --- a/packages/client/src/pages/settings/sounds.vue +++ b/packages/client/src/pages/settings/sounds.vue @@ -85,7 +85,7 @@ async function edit(type) { type: 'enum', enum: soundsTypes.map(x => ({ value: x, - label: x == null ? i18n.ts.none : x, + label: x ?? i18n.ts.none, })), label: i18n.ts.sound, default: sounds.value[type].type, diff --git a/packages/client/src/scripts/scroll.ts b/packages/client/src/scripts/scroll.ts index f5bc6bf9c..2414c2ca6 100644 --- a/packages/client/src/scripts/scroll.ts +++ b/packages/client/src/scripts/scroll.ts @@ -12,7 +12,7 @@ export function getScrollContainer(el: HTMLElement | null): HTMLElement | null { export function getScrollPosition(el: Element | null): number { const container = getScrollContainer(el); - return container == null ? window.scrollY : container.scrollTop; + return container?.scrollTop ?? window.scrollY; } export function isTopVisible(el: Element | null): boolean {