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 { compile } from 'path-to-regexp' | |
type UrlParams = Record<string, string | number> | |
function normarizeUrlParams(input: UrlParams): Record<string, string> { | |
return Object.fromEntries( | |
Object.entries(input).map(([key, value]) => [key, value.toString()]) | |
) | |
} |
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 AttrReaderGuard | |
extend ActiveSupport::Concern | |
class InvalidError < ArgumentError | |
PREFIX = '[attr_reader_guard!]:' | |
def initialize(subject, predicate) | |
super [PREFIX, subject, predicate].join(' ') | |
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
export class Unreachable extends TypeError { | |
static assert(value: never): never { | |
throw new this(value) | |
} | |
static silent(value: never): void { | |
void value | |
} | |
constructor(value: never) { |
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
type ClassNameValue = string | number | false | undefined | null | ClassNameValue[] | { [className: string]: boolean } | |
function cx(...values: ClassNameValue[]) { | |
const classNames: string[] = [] | |
for (const value of values) { | |
if (!value) { | |
continue | |
} | |
if (Array.isArray(value)) { |
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 Papa from 'papaparse' | |
class Csv { | |
constructor(readonly header: string[], readonly rows: string[][]) {} | |
static parse(csv: string) { | |
const [header, ...rows] = Papa.parse(csv.trim(), { header: false }) | |
return new this(header, rows) | |
} |
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 AnswerEvent extends CustomEvent<FormData> { | |
static readonly type = 'async-dialog:answer' | |
constructor(detail: FormData) { | |
super(AnswerEvent.type, { detail }) | |
} | |
get answer() { | |
return this.detail.get('answer')?.toString() | |
} |
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 Ajv, { JSONSchemaType, ErrorObject } from 'ajv' | |
import { FromSchema } from 'json-schema-to-ts' | |
interface AggregatedValidationError extends AggregateError { | |
errors: ErrorObject[] | |
} | |
/** | |
* ajvは全スキーマをキャッシュするので、インスタンスごとにnewせず、使い回す | |
*/ |
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
class SlackNotification | |
class UnknownSlackChannel < StandardError; end | |
WEBHOOK_URLS = Rails.configuration.x.slack_webhook_urls | |
def initialize(webhook_url:) | |
@notifier = Slack::Notifier.new(webhook_url) | |
end | |
def self.for_channel(channel_name) |
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
/** | |
* `try ... catch`で`any`や`unknown`が来たときに、こいつに渡すとすべてをいい感じにしてくれる。 | |
* | |
* ただし、あくまで単一のエラーに対して使う想定。 | |
* 複数のエラーをまとめる用途には、標準の`AggregateError`を使うこと。 | |
* | |
* @see https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/AggregateError | |
* | |
* @example | |
* ```ts |
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 { useMemo } from 'react' | |
interface Props { | |
value?: unknown | |
indent?: number | |
} | |
export function useVarDump(value: unknown, indent = 2) { | |
const json = useMemo(() => { | |
try { |
NewerOlder