From 70c3b51b0b3c89ebd4f615ab117e59911457474a Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 23 Apr 2021 13:00:18 +1000 Subject: [PATCH] remove nested terneries and enable lint @hgiesel would you mind quickly checking I've correctly captured the logic here? --- ts/.eslintrc.js | 1 + ts/editor-toolbar/helpers.ts | 12 +++++------- ts/editor-toolbar/identifiable.ts | 13 +++++-------- ts/graphs/calendar.ts | 23 ++++++++++++++--------- ts/html-filter/index.ts | 17 +++++++++++------ ts/lib/shortcuts.ts | 16 +++++++++------- 6 files changed, 45 insertions(+), 37 deletions(-) diff --git a/ts/.eslintrc.js b/ts/.eslintrc.js index f79067ef8..dd3dc4c4b 100644 --- a/ts/.eslintrc.js +++ b/ts/.eslintrc.js @@ -8,6 +8,7 @@ module.exports = { plugins: ["@typescript-eslint"], rules: { "prefer-const": "warn", + "no-nested-ternary": "warn", "@typescript-eslint/ban-ts-comment": "warn", "@typescript-eslint/no-unused-vars": [ "warn", diff --git a/ts/editor-toolbar/helpers.ts b/ts/editor-toolbar/helpers.ts index b75213c28..cb33d3c7f 100644 --- a/ts/editor-toolbar/helpers.ts +++ b/ts/editor-toolbar/helpers.ts @@ -4,11 +4,9 @@ export function mergeTooltipAndShortcut( tooltip: string | undefined, shortcutLabel: string | undefined ): string | undefined { - return tooltip - ? shortcutLabel - ? `${tooltip} (${shortcutLabel})` - : tooltip - : shortcutLabel - ? `(${shortcutLabel})` - : undefined; + let buf = tooltip ?? ""; + if (shortcutLabel) { + buf = `${buf} (${shortcutLabel})`; + } + return buf; } diff --git a/ts/editor-toolbar/identifiable.ts b/ts/editor-toolbar/identifiable.ts index 41030f094..154895bfc 100644 --- a/ts/editor-toolbar/identifiable.ts +++ b/ts/editor-toolbar/identifiable.ts @@ -8,14 +8,11 @@ function normalize( values: T[], idOrIndex: string | number ): number { - const normalizedIndex = - typeof idOrIndex === "string" - ? values.findIndex((value) => value.id === idOrIndex) - : idOrIndex >= 0 - ? idOrIndex - : values.length + idOrIndex; - - return normalizedIndex >= values.length ? -1 : normalizedIndex; + if (typeof idOrIndex === "string") { + return values.findIndex((value) => value.id === idOrIndex); + } else { + return idOrIndex >= values.length ? -1 : idOrIndex; + } } export function search( diff --git a/ts/graphs/calendar.ts b/ts/graphs/calendar.ts index 8243e9357..d86e42bdb 100644 --- a/ts/graphs/calendar.ts +++ b/ts/graphs/calendar.ts @@ -69,15 +69,7 @@ export function gatherData( reviewCount.set(day, count + 1); } - const timeFunction = - firstDayOfWeek === Weekday.MONDAY - ? timeMonday - : firstDayOfWeek === Weekday.FRIDAY - ? timeFriday - : firstDayOfWeek === Weekday.SATURDAY - ? timeSaturday - : timeSunday; - + const timeFunction = timeFunctionForDay(firstDayOfWeek); const weekdayLabels: number[] = []; for (let i = 0; i < 7; i++) { weekdayLabels.push((firstDayOfWeek + i) % 7); @@ -224,3 +216,16 @@ export function renderCalendar( .duration(800) .attr("fill", (d: DayDatum) => (d.count === 0 ? emptyColour : blues(d.count)!)); } + +function timeFunctionForDay(firstDayOfWeek: WeekdayType): CountableTimeInterval { + switch (firstDayOfWeek) { + case Weekday.MONDAY: + return timeMonday; + case Weekday.FRIDAY: + return timeFriday; + case Weekday.SATURDAY: + return timeSaturday; + default: + return timeSunday; + } +} diff --git a/ts/html-filter/index.ts b/ts/html-filter/index.ts index f3ef0df31..2d1a3152c 100644 --- a/ts/html-filter/index.ts +++ b/ts/html-filter/index.ts @@ -41,12 +41,7 @@ export function filterHTML(html: string, internal: boolean, extended: boolean): const template = document.createElement("template"); template.innerHTML = html; - const mode = internal - ? FilterMode.Internal - : extended - ? FilterMode.Extended - : FilterMode.Basic; - + const mode = getFilterMode(internal, extended); const content = template.content; const filter = filterNode(filters[mode]); @@ -54,3 +49,13 @@ export function filterHTML(html: string, internal: boolean, extended: boolean): return outputHTMLProcessors[mode](template.innerHTML); } + +function getFilterMode(internal: boolean, extended: boolean): FilterMode { + if (internal) { + return FilterMode.Internal; + } else if (extended) { + return FilterMode.Extended; + } else { + return FilterMode.Basic; + } +} diff --git a/ts/lib/shortcuts.ts b/ts/lib/shortcuts.ts index 5b3816496..00ee2df7a 100644 --- a/ts/lib/shortcuts.ts +++ b/ts/lib/shortcuts.ts @@ -53,13 +53,15 @@ const keyToCharacterMap = { }; function keyToPlatformString(key: string): string { - return key.startsWith(alphabeticPrefix) - ? key.slice(alphabeticPrefix.length) - : key.startsWith(numericPrefix) - ? key.slice(numericPrefix.length) - : Object.prototype.hasOwnProperty.call(keyToCharacterMap, key) - ? keyToCharacterMap[key] - : key; + if (key.startsWith(alphabeticPrefix)) { + return key.slice(alphabeticPrefix.length); + } else if (key.startsWith(numericPrefix)) { + return key.slice(numericPrefix.length); + } else if (Object.prototype.hasOwnProperty.call(keyToCharacterMap, key)) { + return keyToCharacterMap[key]; + } else { + return key; + } } function toPlatformString(modifiersAndKey: string[]): string {