From 969cdc938d01a7f48999524e518785fd24009e41 Mon Sep 17 00:00:00 2001 From: rhiannon morris Date: Sun, 18 Aug 2024 02:43:41 +0200 Subject: [PATCH] refactor fragment aliases & add field lookup checking why is that not the default --- Makefile | 2 +- script/gallery.ts | 27 +++++++++++++++++---------- tsconfig.json | 1 + 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index d2bd1c3..b27b287 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ $(BUILDDIR)/%: $(TMPDIR)/% $(TMPDIR)/%.js: %.ts echo "[tsc] "$@ - tsc --strict --noEmitOnError \ + tsc --strict --noUncheckedIndexedAccess --noEmitOnError \ --lib dom,es2021 --target es2015 \ --outDir $(dir $@) $^ diff --git a/script/gallery.ts b/script/gallery.ts index 1213ef8..6c562cd 100644 --- a/script/gallery.ts +++ b/script/gallery.ts @@ -106,23 +106,30 @@ function makeFragment() { } } +type Shortcuts = { [short: string]: Set }; +const shortcuts: Shortcuts = { + 'summary': new Set('require_artsummary'), + 'colourexamples': new Set('require_colourexample'), + 'flatexamples': new Set('require_flatexample'), + 'sketchexamples': new Set('require_sketchexample'), + 'iconexamples': new Set('require_iconexample'), + 'curated': new Set('require_curated') +}; + function useFragment() { const frag = decodeURIComponent(location.hash).replace(/^#/, ''); const details = document.getElementById('filters-details') as HTMLDetailsElement; + const fromShortcut = shortcuts[frag]; if (!frag) { clearForm(); } else if (frag == 'all') { allBoxes.forEach(b => b.checked = false); details.open = false; + } else if (fromShortcut) { + allBoxes.forEach(b => b.checked = fromShortcut.has(b.id)); } else { - const pieces = - frag == 'summary' ? ['require_artsummary'] : - frag == 'colourexamples' ? ['require_colourexample'] : - frag == 'flatexamples' ? ['require_flatexample'] : - frag == 'sketchexamples' ? ['require_sketchexample'] : - frag == 'iconexamples' ? ['require_iconexample'] : - frag == 'curated' ? ['require_curated'] : frag.split(';'); + const pieces = frag.split(';'); const set = new Set(pieces); const re = /^(require|exclude)_|hide_filters/; if (pieces.every(x => re.test(x))) { @@ -154,7 +161,7 @@ function sortFilters(cmp: (a: Node, b: Node) => number) { function sortFiltersAlpha(e: Event) { function getName(node: Node): string { if (node instanceof Element) { - return node.getElementsByTagName('input')[0].value; + return node.getElementsByTagName('input')[0]?.value ?? ''; } else { return ''; } @@ -164,9 +171,9 @@ function sortFiltersAlpha(e: Event) { } function sortFiltersUses(e: Event) { - function getUses(node: Node) { + function getUses(node: Node): number { if (node instanceof Element) { - const countStr = node.getElementsByTagName('label')[0].dataset.count; + const countStr = node.getElementsByTagName('label')[0]?.dataset.count; return countStr ? +countStr : 0; } else { return 0; diff --git a/tsconfig.json b/tsconfig.json index 4c1a853..309f42d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "strict": true, + "noUncheckedIndexedAccess": true, "noEmitOnError": true, "lib": ["ES2021", "dom"], "target": "ES2015"