Skip to content

Instantly share code, notes, and snippets.

View genki's full-sized avatar

Genki Takiuchi genki

View GitHub Profile
@genki
genki / keep-mtime.ts
Created February 17, 2025 08:37
A vite-plugin for bringing mtime/atime of files in ./public to ./dist
import type {Plugin} from 'vite';
import path from 'path';
import fs from 'fs';
const publicDir = path.resolve(__dirname, '../../public');
const distDir = path.resolve(__dirname, '../../dist');
const copyMtime = (srcDir:string, dstDir:string):void => {
console.log('keep-mtime: copying stat', srcDir, '->', dstDir);
for (const file of fs.readdirSync(srcDir)) {
@genki
genki / dist.rb
Created December 16, 2024 17:28
./distの中身を./dist.dataにまとめる
#!/usr/bin/env ruby
# ./distの中身を./dist.dataにまとめる
DIST_DIR = './dist'
OUT_PATH = "./dist.data"
paths = []
Dir["#{DIST_DIR}/**/*"].each do |path|
next if File.directory?(path)
pathO = path.gsub("#{DIST_DIR}", '')
paths << [path, pathO]
@genki
genki / translator.ts
Created October 16, 2024 23:48
Type alias definition of overloaded functions
const translator = <Result extends string|Promise<string>>() => {
type A = any[];
function t(s?:TranslateOptions):
(s:TemplateStringsArray, ...values:A) => Result;
function t(s:string,...values:A):(s:string, ...values:A) => Result;
function t(n:number|Signal<number>, s?:TranslateOptions):
(s:TemplateStringsArray, ...values:A) => Result;
function t(n:number|Signal<number>, s?:TranslateOptions):
(s:string, ...values:A) => Result;
function t(s:TemplateStringsArray, ...values:A):Result;
@genki
genki / invade
Last active July 21, 2024 22:13
Invader: A simple preprocessor for terraform
#!/usr/bin/env ruby
# A simple preprocessor for terraform.
#
# USAGE:
#
# invade <template dir> <modules dir>
#
# This script will copy all tf files in the template dir to the modules dir
# and replace the `//invade <invader name> <args>` with the content of the
# invader file located at `./invader/<invader name>.tf`.
@genki
genki / bitmask.ts
Last active July 2, 2024 12:37
マスクビット値のUnionからビットマスク型を生成する型関数
const Flag = {
foo: 1,
bar: 2,
baz: 4,
} as const;
type MaskType = MaskOf<typeof Flag[keyof typeof Flag]>;
// ^? 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
type SparseMaskType = MaskOf<1|2|8>;
@genki
genki / uint8array_serializer.ts
Last active February 28, 2024 07:11
The Uint8Array serializer over valid UTF-16 string.
// pack bytes into valid UTF-16 string
//
// strategy:
//
// * using ESC as the escape character
// * if there is ESC in the bytes, double it
// * if there is unmatched surrogate pair, mark it by the escape character
//
// 0x007f: escape, because it's rare but still only one utf-8 byte.
// To escape itself, use 0x007f 0x08ff (two bytes utf-8)
@genki
genki / outof.ts
Last active January 21, 2024 21:16
`outof`, the valibot utility method, that is a type guard for the `Output` type of the schema as like as the `is` does for `Input` type. In addition, you can use the parsed value if you specify `then` arg.
const outof = <T extends BaseSchema, R,
O = T extends BaseSchema<any, infer O> ? O : never,
>(schema:T, value:unknown, then?:(value:O) => R): value is O => {
const {issues, output} = safeParse(schema, value);
if (issues && issues.length > 0) return false;
if (then) then(output as O);
return true;
};
// USAGE example
export const waiter: {
<T, R, A extends any[]>(
w:Waitable<T>, done:(x:T, ...args:A) => Promise<R>|R
): (...args:A) => Promise<R>;
<R, A extends any[], T=void|undefined>(
w:Waitable<T>, done:(...args:A) => Promise<R>|R
): (...args:A) => Promise<R>;
} = <T, R, A extends any[]>(w:Waitable<T>, done:{
(x:T, ...args:A):Promise<R>|R;
(...args:A):Promise<R>|R;
@genki
genki / submit.ts
Created August 31, 2023 10:17
`submit(formStore, onSubmit$)` method that enables the `modular-forms/qwik` to submit the form equivalent to simulate the manual submit.
import type {
FormStore, FieldValues, ResponseData, FormErrors, FieldPath, FieldArrayPath,
SetResponseOptions,
} from '@modular-forms/qwik';
import {
getValues, validate, setResponse, FormError, setError,
} from '@modular-forms/qwik';
import { type Maybe } from '~/utils';
type ErrorResponseOptions = SetResponseOptions &
@genki
genki / mr
Last active January 25, 2023 01:25
mr: Move files relatively
#!/usr/bin/ruby
opts, files = ARGV.partition {|a| a =~ /^-/}
first, *middle, last = files
base = File.expand_path File.dirname(first)
to = File.expand_path File.join base, last
system ['mv', *opts, first, *middle, to].join ' '