Skip to content

Instantly share code, notes, and snippets.

View fsubal's full-sized avatar

fsubal fsubal

View GitHub Profile
@fsubal
fsubal / index.ts
Last active April 21, 2024 12:14
<tora-viewer> Web Components
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`)
@fsubal
fsubal / Byte.ts
Last active March 25, 2024 14:07
export class Byte {
static readonly units = {
terabyte: 2 ** 40,
gigabyte: 2 ** 30,
megabyte: 2 ** 20,
kilobyte: 2 ** 10,
};
private unit = "byte";
private divisor = 1;
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)
}
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;
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', ' ');
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) {
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
const enum AspectRatio {
Scale = 0,
ForceScale = 1,
Crop = 2,
Pad = 3
}
// prettier-ignore-start
const enum Gravity {
TopLeft = 1, Top = 2, TopRight = 3,
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)
}
@fsubal
fsubal / clsx.rb
Last active October 13, 2023 13:00
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