refactor fragment aliases & add field lookup checking

why is that not the default
This commit is contained in:
rhiannon morris 2024-08-18 02:43:41 +02:00
parent 43af72aa4d
commit 969cdc938d
3 changed files with 19 additions and 11 deletions

View file

@ -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 $@) $^

View file

@ -106,23 +106,30 @@ function makeFragment() {
}
}
type Shortcuts = { [short: string]: Set<string> };
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;

View file

@ -1,6 +1,7 @@
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noEmitOnError": true,
"lib": ["ES2021", "dom"],
"target": "ES2015"