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
/** | |
* a以上b以下の整数の乱数を生成する | |
* @param {number|bigint} a | |
* @param {number|bigint} b | |
* @returns {number|bigint} | |
*/ | |
function getRandomIntBetween(a, b) { | |
if (b < a) { | |
// [a, b] = [b, a] | |
const t = a; a = b; b = t; |
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
// タイムアウト付き setInterval | |
const setIntervalTimeout = (f, interval = 200, timeout = 5000, immediate = false) => { | |
immediate && setTimeout(f, 0) | |
const i = setInterval(f, interval) | |
const t = setTimeout(() => clearInterval(i), timeout) | |
return () => { clearInterval(i); clearTimeout(t) } | |
} | |
// タイムアウト付き setInterval の強化版(タブがバックグラウンドになってもサボらない) | |
const setIntervalTimeoutSuper = (f, interval = 200, timeout = 5000, immediate = false) => { | |
try { |
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
const beep = (duration=200, frequency=440, gain=1.0) => new Promise((resolve, reject) => { | |
try { | |
const audioCtx = new AudioContext() | |
const oscNode = new OscillatorNode(audioCtx, {frequency, type:"square"}) | |
const gainNode = new GainNode(audioCtx, {gain}) | |
oscNode.onended = resolve | |
oscNode.connect(gainNode).connect(audioCtx.destination) | |
oscNode.start(audioCtx.currentTime) | |
oscNode.stop(audioCtx.currentTime + duration / 1000) | |
} catch(err){ |
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
const movingAverageFilter = (ws=3) => { | |
if(!Number.isInteger(ws) || ws < 1) { | |
throw new Error(`Invalid window size. ws must be >=1. You entered: ws=${ws}.`) | |
} | |
let win = [] | |
let sum = 0 | |
let pos = 0 | |
let tail = 0 | |
return (head) => { | |
sum = sum + head - tail |
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
// const q = (s,r)=>[...(r||document).querySelectorAll(s)]; | |
const q = (selector,root) => | |
selector.split(/::shadow\s*/) | |
.reduce((parents,selector,idx) => | |
parents.flatMap(parent => | |
selector=="" ? (idx==0?parent:parent.shadowRoot) : [...(idx==0?parent:parent.shadowRoot).querySelectorAll(selector)] | |
) | |
, [root||document] | |
) |
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
// CloudFront Functions 内で利用可能なBASE64実装 | |
function b64enc(b) { | |
var b64codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".split("").map(s=>s.charCodeAt(0)) | |
var pad = [0, 2, 1][b.length % 3] | |
// 入力バイト列(本来は 0xFF を超える charCode が含まれていたらエラーにすべきだが省略 | |
var bCodes = (b + '\0'.repeat(pad)).split("").map(s=>s.charCodeAt(0)) | |
// 出力用バイト列 | |
var aCodes = new Uint8Array(bCodes.length / 3 * 4) | |
for(var i=0,j=0; i<b.length; i+=3,j+=4){ |
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
#!/usr/bin/swift | |
import AppKit | |
let urls = CommandLine.arguments.dropFirst(1).map { URL(fileURLWithPath: $0) } | |
NSWorkspace.shared.activateFileViewerSelecting(urls) |
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
if "test ! -d ~/.tmux/plugins/tpm" \ | |
"run 'git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm'" |
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
const toCodeUnit = s => s.split("").map(u => `\\u${u.charCodeAt(0).toString(16).toUpperCase().padStart(4, 0)}`).join("") | |
const toCodePoint = s => [...s].map(p => `\\u{${p.codePointAt(0).toString(16).toUpperCase()}}`).join("") | |
const splitToVisualChars = s => [...s].reduce((c, p, i, a) => { | |
let description = p | |
if (p == "\u200D") { | |
// ZERO WITH JOINER | |
description = "ZWJ" | |
} else if ("\u{180B}" <= p && p <= "\u{180D}") { | |
// モンゴル文字専用のモンゴル自由字形選択子(3個) | |
description = `FVS${p.codePointAt(0) - 0x180B + 1}` |
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
// おまじない | |
ᡸ=ᡸᡸ=>ᡸ | |
// SyntaxErrorはもちろん、ランタイムエラーすら出さずに実行可能 | |
ᡸ``` | |
ᡸhi | |
ᡸ``` |