Skip to content

Instantly share code, notes, and snippets.

View fsubal's full-sized avatar

fsubal fsubal

View GitHub Profile
class Provider
include FindSubclassBySlug
end
module FindSubclassBySlug
extend ActiveSupport::Concern
class SubclassNotFound < StandardError; end
included { load_subclasses! }
type ExtractParams<Template extends string> =
Template extends `${infer A}/${infer B}` ? ExtractParams<A> | ExtractParams<B>
: Template extends `/${infer A}` ? ExtractParams<A>
: Template extends `:${infer A}/${infer B}` ? A | ExtractParams<B>
: Template extends `:${infer A}` ? A
: never
type Params<Template extends string> = ExtractParams<Template> extends string ? Record<ExtractParams<Template>, string> : never
class TrieNode<Template extends `/${string}`, R> {
@fsubal
fsubal / task.rb
Last active June 6, 2024 18:22
Notion ActiveRecord
module Notion
class Task < Database::Page
self.database_id = 'e383bcee-e0d8-4564-9c63-900d307abdb0'
def event_date
property('作成日時') #=> Notion::Database::Property
end
def assignees
property('担当者') #=> Array<Notion::Database::Property>
export class TimeoutError extends Error {}
export class Timeout<T> {
readonly controller: AbortController
readonly error = new TimeoutError()
readonly promise: Promise<T>
constructor(
fn: (signal: AbortSignal) => Promise<T>,
timeout: number
class PlainTime
include Comparable
attr_reader :hour, :minute, :second
HOUR_PER_DAY = 24
MINUTE_PER_HOUR = 60
SECOND_PER_MINUTE = 60
SECOND_PER_HOUR = HOUR_PER_DAY * MINUTE_PER_HOUR
module InheritByEnum
extend ActiveSupport::Concern
class_methods do
def inherit_by_enum(**options)
name, values = options.sole.to_a
plural_name = name.to_s.pluralize
self.inheritance_column = name
@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;