This file contains 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 toraViewer, { type Viewer } from '@toralab/tora-viewer'; | |
function finiteInteger<R extends number | null | undefined>(value: string | null, defaultValue: R = 0): R { | |
if (!value) { | |
return defaultValue | |
} | |
const parsed = parseInt(value) | |
if (!Number.isFinite(parsed)) { | |
throw new RangeError(`${value} cannot be parsed to finite number`) |
This file contains 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
export class Byte { | |
static readonly units = { | |
terabyte: 2 ** 40, | |
gigabyte: 2 ** 30, | |
megabyte: 2 ** 20, | |
kilobyte: 2 ** 10, | |
}; | |
private unit = "byte"; | |
private divisor = 1; |
This file contains 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
export function memo<T extends (...args: any[]) => any>(fn: T): T { | |
const call = Object.assign(fn, { cache: null as ReturnType<T> | null }) | |
return ((...args: Parameters<T>) => call.cache ??= call(...args)) as T | |
} | |
export class Foo { | |
bar = memo(() => 1) | |
} |
This file contains 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 hljs from 'highlight.js' | |
import typescript from 'highlight.js/lib/languages/typescript' | |
const css = new CSSStyleSheet() | |
css.replaceSync(` | |
@import 'highlight.js/styles/github-dark.css'; | |
overflow-x: auto; | |
font-size: 12px; | |
border-radius: 8px; |
This file contains 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
export const connect = (db: D1Database) => function sql( | |
str: TemplateStringsArray, | |
...args: (Date | string | null | number | boolean)[] | |
) { | |
const sql = str.join('?') | |
const statement = db.prepare(sql) | |
for (const arg of args) { | |
if (arg instanceof Date) { | |
const formatted = arg.toISOString().slice(0, 19).replace('T', ' '); |
This file contains 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
export const MyElement = withEventHandlers('my-element', { | |
'onClick': 'click', | |
'onMouseover': 'mouseOver', | |
'onMousedown': 'mouseDown', | |
}) | |
function withEventHandlers< | |
Name extends keyof JSX.IntrinsicElements, | |
Map extends Record<string, string> | |
>(ElementName: Name, eventHandlers: Map) { |
This file contains 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
module Jbreaker::TreatErrorAsRequestVariant | |
included do | |
rescue_from StandardError do |exception| | |
ActionDispatch::ExceptionWrapper.rescue_responses[exception.to_s]&.tap do |response| | |
request.variant = response if response | |
end | |
raise exception | |
end | |
end |
This file contains 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 enum AspectRatio { | |
Scale = 0, | |
ForceScale = 1, | |
Crop = 2, | |
Pad = 3 | |
} | |
// prettier-ignore-start | |
const enum Gravity { | |
TopLeft = 1, Top = 2, TopRight = 3, |
This file contains 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
export class Validatable<T> { | |
#validators: Array<(subject: T) => void> = [] | |
constructor() {} | |
mustBe(fn: (subject: T) => boolean, message: string) { | |
this.#validators.push((subject) => { | |
if (!fn(subject)) { | |
throw new Error(message) | |
} |
This file contains 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
module Clsx | |
# https://github.com/JedWatson/classnames/blob/main/index.js | |
def clsx(*args) | |
args.flat_map(&method(:to_class)).join(' ') | |
end | |
private | |
def to_class(value) | |
case value |
NewerOlder