Skip to content

Instantly share code, notes, and snippets.

View LorisSigrist's full-sized avatar
🇨🇭

Loris Sigrist LorisSigrist

🇨🇭
View GitHub Profile
@LorisSigrist
LorisSigrist / index.md
Created February 5, 2025 22:10
Vite-Plugin-SQLC Proof of Concept

Vite-Plugin-SQLC Concept

SQLC can generate typesafe code for interacting with your database. This is done via code-generation.

I thought it would be nice to use vite to hide the code generation and instead import the .sql files where the queries are defined directly.

Thanks to Typescript's typeRoots option this is still entirely typesafe, but offers a better editing experience IMO.

@LorisSigrist
LorisSigrist / index.ts
Last active July 3, 2024 13:50
VsCode WebView to Comlink Endpoint
import * as vscode from "vscode"
import * as Comlink from "comlink"
type MessageEvent = Parameters<typeof listener>[0]
function webviewEndpoint(webview: vscode.Webview): Comlink.Endpoint {
const listeners = new Map<any, vscode.Disposable>()
return {
// NO OWNERSHIPT TRANSFERS POSSIBLE
@LorisSigrist
LorisSigrist / app.js
Created June 12, 2024 15:41
Using ParaglideJS with Hono
import { Hono } from "hono"
import { ParaglideHono } from "./paraglide-hono.js"
import * as runtime from "./paraglide/runtime.js"
import * as m from "./paraglide/messages.js"
// custom function that determines which language should be used based on the Context
function detecLanguage(c) {
const pathname = new URL(c.req.url).pathname;
const parts = pathname.split("/");
return isAvailableLanguageTag(parts[1]) ? parts[1] : sourceLanguageTag;
@LorisSigrist
LorisSigrist / reroute-sequence.ts
Created January 15, 2024 16:37
A sequence utility for SvelteKit's reroute hook
const rerouteSequence(...reroutes) {
return ({ url }) => {
for(const reroute of reroutes) {
url.pathname = reroute({ url: new URL(url) }) ?? url.pathname
}
return url.pathname
}
}
@LorisSigrist
LorisSigrist / enforcer-hook.js
Created January 9, 2024 18:57
A SvelteKit hook for enforcing rules on a route-level
import { dev } from "$app/environment";
import { sequence } from "@sveltejs/kit/hooks";
// A rule segment has the form ($ruleName)
const ruleSegmentRegex = /^\(\$[a-zA-Z_]+\)$/;
/**
* Checks if a segment is a rule segment.
*
* @param {string} segment
* @returns {segment is `($${string})`}
@LorisSigrist
LorisSigrist / birthday.js
Created December 20, 2023 10:20
Birthday Problem simulation
const num_days = 16_000_000;
const num_participants = 100_000;
const samples = 100_000;
let collisions = 0;
const percentageFormatter = new Intl.NumberFormat('en-US', {
style: 'percent',
maximumFractionDigits: 3,
});
@LorisSigrist
LorisSigrist / diff-urls.js
Created December 14, 2023 08:45
A function to get the shortest href that get's you from one URL to another
/**
* Get's the shortest href that gets from `from` to `to`
*
* @param {URL} from
* @param {URL} to
* @returns {string} The shortest href that gets from `from` to `to`
*/
export function getHrefBetween(from, to) {
//check if they use the same protocol - If not, we can't do anything
@LorisSigrist
LorisSigrist / result.ts
Last active November 18, 2023 23:47
Result Type in Typescript
type TypeMap = {
[key: string]: any
}
type Entries<T extends TypeMap> = {
[K in keyof T]: [K, T[K]];
}[Extract<keyof T, string>];
export type OkResult<OkVal> = {
ok: true,
@LorisSigrist
LorisSigrist / reply.md
Last active August 16, 2024 07:40
Reply to Inlang API Simpilarity

This is in response to This comment I got on the t18s announcement. This was too long, so reddit wouldn't allow me to post it there.


Hi. I saw inlang's tweet yesterday and am very excited about what you are building!

I looked at the repo you linked and agree that the API is very similar. However, I did arrive at this on my own.

Let me walk you through how I arrived at the design I'm currently at. I want to write a blog post about this anyway, so I'll take this opportunity to write a draft. Sorry if this gets long.

@LorisSigrist
LorisSigrist / ResultMatcher.js
Last active October 12, 2023 08:29
Declarative Error Handling in JS
/**
* The configuration for a ResultMatcher Strategy.
*
* @template Prototype
* @template ReturnType
* @typedef {{
* prototype: { new (): Prototype; } | { prototype: Prototype; },
* handler: (instance: Prototype) => ReturnType;
* }} Strategy
*/