Skip to content

Instantly share code, notes, and snippets.

@kawanet
kawanet / node-assert.shim.ts
Last active May 4, 2026 15:26
a mini subset of node:assert for browser
// Browser-side shim for `node:assert`. Aliased into the test bundle
// by the rollup test config. Tests source-import as
// `import {strict as assert} from "node:assert"`, so this file
// exports `strict` matching that surface.
//
// Canonical: https://gist.github.com/kawanet/5214f8731e1a8ab1f0accd1a2c0538b0
export const strict = {
// Truthy check. Mirrors `assert.ok(value, message?)` in node:assert.
ok(value: unknown, message?: string): void {
@kawanet
kawanet / node-test.shim.ts
Last active May 4, 2026 17:05
a mini subset of node:test for browser
// Browser-side shim for `node:test`. Aliased into the test bundle by
// the rollup test config so the test sources can import the same
// names (`describe`, `it`, `before`, `after`) under both Node (real
// node:test) and the browser (this file).
//
// Canonical: https://gist.github.com/kawanet/5130469aa49b613fb68c8e6781a2ed46
//
// Test results are rendered into `<ul id="results">` on the host
// page (the element is created on demand if absent).
//
@kawanet
kawanet / exec-service.sh
Last active July 26, 2025 13:25
Loop-run systemd’s ExecStart command
#!/bin/bash
#---
## @file exec-service.sh
## @brief Loop-run systemd’s ExecStart command
## @see https://gist.github.com/kawanet/2eadbbf03f3619a28bb7cd6f36985f8d
## @author Yusuke Kawasaki
## @license MIT
#---
die () {
@kawanet
kawanet / svg-to-favicon.sh
Created March 18, 2023 13:35
SVG を favicon.ico に変換する
#!/usr/bin/env bash
convert -background none image.svg -define icon:auto-resize=64,48,32,16 favicon.ico
@kawanet
kawanet / sjis-to-utf8-bom.sh
Created February 2, 2023 00:18
Shift_JIS CP932 を BOM 付き UTF-8 に変換するワンライナー
node -e 'require("fs").writeFileSync(process.argv[1], "\uFEFF" + require("iconv-cp932").decode(require("fs").readFileSync(process.argv[1])))' filename.csv
const getTimezoneOffset = (timeZone, dt) => new Intl.DateTimeFormat("en-US", {timeZoneName: "longOffset", timeZone}).formatToParts(dt).find(v => v.type === "timeZoneName")?.value;
getTimezoneOffset("America/New_York", new Date()); // => "GMT-05:00"
getTimezoneOffset("America/New_York"); // => "GMT-05:00"
getTimezoneOffset("invalid"); // => Uncaught RangeError: Invalid time zone specified:
@kawanet
kawanet / jp-neighboring-pref.csv
Last active June 6, 2023 12:31
jp-neighboring-pref - 隣接都道府県の一覧をCSVで取り出す(MITライセンス) https://qiita.com/kawanet/items/f753df6249c14ef67a04
01 北海道 / /
02 青森県 /03/05/ /岩手県/秋田県/
03 岩手県 /02/04/05/ /青森県/宮城県/秋田県/
04 宮城県 /03/05/06/07/ /岩手県/秋田県/山形県/福島県/
05 秋田県 /02/03/04/06/ /青森県/岩手県/宮城県/山形県/
06 山形県 /04/05/07/15/ /宮城県/秋田県/福島県/新潟県/
07 福島県 /04/06/08/09/10/15/ /宮城県/山形県/茨城県/栃木県/群馬県/新潟県/
08 茨城県 /07/09/11/12/ /福島県/栃木県/埼玉県/千葉県/
09 栃木県 /07/08/10/11/ /福島県/茨城県/群馬県/埼玉県/
10 群馬県 /07/09/11/15/20/ /福島県/栃木県/埼玉県/新潟県/長野県/
@kawanet
kawanet / drawColorFn.html
Last active December 11, 2021 07:23
drawPixel(output, pos, ...color) is slow
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>benchmark</title>
</head>
<body>
Open DevTools
<script>
setTimeout(function() {
@kawanet
kawanet / text-decoder-bench.js
Created September 3, 2021 14:12
Benchmarks for TextDecoder and TextEncoder
#!/usr/bin/env node
/**
* text-decoder-bench.js
* @copyright Kawanet
* @licence MIT
* @see https://gist.github.com/kawanet/a66a0e2657464c57bcff2249286d3a24
*/
const readString = (buffer, offset, length) => {
// オンデマンドに T や Promise<T> を返す関数実装を許容する
type OnDemandType<T> = (T | (() => T) | (() => Promise<T>));
// すべてのプロパティがオンデマンド対応
type LazyType<T> = { [K in keyof T]: OnDemandType<T[K]> };
// T や Promise<T> を返す関数実装なら T を取り出す
type ResolvedType<T> = (T extends () => Promise<infer P> ? P : T extends () => infer P ? P : T);
// T のうち __typename プロパティを無視して、残りのプロパティは必須とする