This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { AuthErrorType } from '~/auth/types' | |
export class AuthError extends Error { | |
readonly type: AuthErrorType | |
constructor( | |
type: AuthErrorType, | |
{ message, cause }: { message?: string } & ErrorOptions = {}, | |
) { | |
super(message ?? AuthError.defaultMessage[type], { cause }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { debounce } from 'https://deno.land/std@0.224.0/async/debounce.ts' | |
await Array.fromAsync( | |
Deno.watchFs('file.txt'), | |
debounce((event) => console.debug(`[${event.kind}] ${event.paths[0]}`), 50), | |
) | |
// Credits: https://github.com/denoland/deno/issues/12874#issuecomment-1935198354 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from 'zod' | |
const privateKeySchema = z.object({ | |
MY_PRIVATE_KEY: z | |
.string() | |
.regex( | |
/-----BEGIN PRIVATE KEY-----\s*\S[\s\S]*?\s*-----END PRIVATE KEY-----/, | |
'MY_PRIVATE_KEY must be a valid private key.', | |
) | |
.describe('Full contents of the private key used to sign/encrypt the thing.'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { setTimeout } from 'node:timers/promises' | |
export async function* fibonacci(frequencyMs = 200) { | |
let a = 0n | |
let b = 1n | |
while (true) { | |
yield String(b) | |
b = a + b | |
a = b - a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parametrize = (url: string, params: ConstructorParameters<typeof URLSearchParams>[0]) => { | |
return `${url}?${new URLSearchParams(params)}` | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type RegExpMatchArrayWithGroups<G extends string> = RegExpMatchArray & { | |
groups: Record<G, string> & Record<string, never> | |
} | |
const isRegExpMatchArrayWithGroups = <G extends string>( | |
match: RegExpMatchArray | null, | |
...groups: G[] | |
): match is RegExpMatchArrayWithGroups<G> => groups.every((g) => match?.groups !== undefined && g in match.groups) | |
// usage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import resolve from './resolve' // https://gist.github.com/xzec/fac8ca82ce0e5cee41cf3d9769f9c9d0 | |
const resolveFetch = (promise: Promise<Response>) => | |
resolve( | |
promise.then((response) => { | |
if (response.ok) return response | |
throw new Error(`${response.status} ${response.statusText}`) | |
}) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Sensor = { | |
lastMeasuredValue?: number | null | |
} | |
const sensors: Sensor[] = [] | |
// type of `values` is narrowed down to `number[]` | |
const values = sensors | |
.map((sensor) => sensor.lastMeasuredValue) | |
.filter((value): value is number => typeof value === 'number') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Resolved<Data> = [undefined, Data] | [Error, undefined] | |
export const resolve = <Data>(promise: Promise<Data>): Promise<Resolved<Data>> => | |
promise | |
.then((data): [undefined, Data] => [undefined, data]) | |
.catch((error): [Error, undefined] => [error, undefined]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const resolveAllSettled = <T>(results: PromiseSettledResult<T>[]): [errors: string[], data: T[]] => { | |
const errors = results | |
.filter((result): result is PromiseRejectedResult => result.status === 'rejected') | |
.map(({ reason }) => { | |
if (reason instanceof Error) return reason.message | |
if (typeof reason === 'string') return reason | |
return 'undetected reason' | |
}) | |
const data = results |
NewerOlder