Skip to content

Instantly share code, notes, and snippets.

View scarf005's full-sized avatar

scarf scarf005

View GitHub Profile
@scarf005
scarf005 / mapEntries.ts
Created June 10, 2023 22:26
unnecesarily complex useform with only string input
export const mapEntries = <T, U>(
obj: Record<string, T>,
fn: (entry: [string, T]) => [string, U],
) => Object.fromEntries(Object.entries(obj).map(fn))
@scarf005
scarf005 / removeIsOpen.ts
Created June 7, 2023 02:13
removes is:open from issues and discussions (only for Q&A).
const getUrl = (elem: Element) => elem.getAttribute("href")!.split("?")[0]
const setQuery = (selector: string, query: string) => () => {
const elem = document.querySelector(selector)
if (!elem) {
console.log(`elem with ${selector} not found`)
return
}
const newSearchParams = new URLSearchParams({ q: query })
elem.setAttribute("href", `${getUrl(elem)}?${newSearchParams}`)
@scarf005
scarf005 / README.md
Last active December 11, 2023 07:30
statistics
@scarf005
scarf005 / initdeno.ts
Last active June 6, 2023 13:43
initialize deno with vscode settings, no semicolon, and line width 100
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run --allow-env --unstable
import { Command } from "https://deno.land/x/cliffy@v0.25.7/command/mod.ts"
import { dirname, join } from "https://deno.land/std@0.190.0/path/mod.ts"
import { c, p } from "https://deno.land/x/copb@v1.0.1/mod.ts"
const mapKeys =
<A>(f: (k: string) => string) => (record: Readonly<Record<string, A>>): Record<string, A> =>
Object.fromEntries(Object.entries(record).map(([k, v]) => [f(k), v]))
import {
bgBrightRed,
bold,
brightGreen,
brightRed,
brightWhite,
brightYellow,
} from "https://deno.land/std@0.186.0/fmt/colors.ts"
import { c, p } from "https://deno.land/x/copb@v1.0.1/mod.ts"
import { match } from "npm:ts-pattern"
declare global {
interface PromiseConstructor {
resolve<const T>(value: T): Promise<Awaited<T>>
}
}
import { z } from "https://deno.land/x/zod@v3.20.5/mod.ts"
import { HasType } from "./type_helper.ts"
export type AnyZodEffect = z.ZodEffects<z.ZodTypeAny, unknown, unknown>
export type ObjectSchema = z.ZodObject<z.ZodRawShape>
/** Recursively checks if schema has a transform effect for either the whole object or a property. */
export type HasZodEffect<T> = T extends ObjectSchema
? HasType<T["shape"], AnyZodEffect> extends true ? true
import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts"
/** @example
* const input = [1, 1, -1, 1, -1, 1]
* assertEquals(span(input, (x) => x > 0), [[1, 1], [-1, 1, -1, 1]])
*/
export const span = <T>(array: T[], predicate: (line: T) => boolean): [T[], T[]] => {
const index = array.findIndex((line) => !predicate(line))
const before = index === -1 ? array : array.slice(0, index)
const after = index === -1 ? [] : array.slice(index)
import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts"
import { outdent } from "npm:outdent"
import { trisection } from "./partition_utils.ts"
import { walk } from "https://deno.land/std@0.182.0/fs/walk.ts"
import { asynciter } from "https://deno.land/x/asynciter@0.0.15/asynciter.ts"
import { fromFileUrl, join, resolve } from "https://deno.land/std@0.177.0/path/mod.ts"
export const SCRIPT_DIR = fromFileUrl(import.meta.url)
export const TOP_DIR = resolve(SCRIPT_DIR, "..", "..", "..")
export const JSON_DIR = join(TOP_DIR, "data")