Skip to content

Instantly share code, notes, and snippets.

@jbarnat
jbarnat / debounce.ts
Created April 15, 2019 14:10
ignore click event that occurs shortly after touchstart
export default function debounce(func: Function, sleep?: number): EventListener {
let timerMobile: number | undefined
let timer: number | undefined
return function debounced(e: Event): void {
if (!!timer || (!!timerMobile && e.type === "click")) {
return
}
func()
if (typeof sleep !== "undefined" && sleep > 0) {
clearTimeout(timer)
@jbarnat
jbarnat / assertExactKeys.ts
Created June 3, 2025 16:52
assertExactKeys.ts
/**
* ✅ Use case:
* Validate that a list of string keys matches a TypeScript interface *exactly*:
* - No missing keys
* - No extra keys
*
* 💡 Great for:
* - DTO serialization (`toDBObject`)
* - Form field definitions
* - Schema-driven rendering
<script lang="ts">
import { SvelteSet } from 'svelte/reactivity';
import type { FlattenedIssues } from '$lib/components/forms/types.js';
import Form from '$lib/components/forms/Form.svelte';
import ValidationIssues from '$lib/components/forms/ValidationIssues.svelte';
import { commentForm } from './commentForm.remote.js';
import { commentSchema } from './schema.js';
let liveIssues: FlattenedIssues<typeof commentSchema> = $state({});