Lovely code of the week – 48/2026
Happy weekend, programmers!
I did a lot of stuff on the ar-at-localhost project this week.
Here’s a lovely piece of code from this week.
TL;DR: Objects make nice, lightweight maps. Paired with sensible fallbacks, they
offer a clean and elegant solution without the overhead of a Map.
Problem
I’ve introduced keys where users may manage a set of keys used in their operations. We need to allow users to update them as necessary. Since most of them are secret, users should see them as passwords—but may turn on view to see them as plain values. The quirk is that we have several keys, not just one.
Solution
At first glance, I thought of a map to store the current state of the field, but a map can be too
noisy with its set, get, has, etc – so I turned to an
object as a map (indeed a Map is an object) and let missing keys be treated as hidden, as it would
be the default. Then I added a function to rotate them.
<script lang="ts">
// ...
const fieldTypes = $state<
{ [key: string]: 'password' | 'text' }
>({}); // notice empty {}
const onview = (key: string) =>
fieldTypes[key] = fieldTypes[key] === 'text'
? 'password'
: 'text';
// ...
</script>
{#each userKeys.values() as key,idx}
<!-- ... -->
<InputGroupButton onclick={() => onview(key)}>
{#if fieldTypes[key] === 'text'}
<EyeOff />
{:else}
<Eye />
{/if}
</InputGroupButton>
<!-- ... -->
{/each}