Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ts-node
/**
* Usage:
*
* pnpm ts-node scripts/generate-btc-ordinal-addresses.ts 'mainnet | testnet' 'mnemonic' 0 1000 | jq -r 'map(.address) | join("\n")' > /save/to/some/file
*/
import { BIP32Factory } from 'bip32';
import * as bip39 from 'bip39';
type UnknownObject = Record<string, unknown>
/**
* 一个 EventEmitter ,当注册事件的对象(`gcTarget`)被 GC 时,会自动取消注册事件
*
* WARNING: 使用这个 EventEmitter 时需要注意,为了避免 `callback` 里可能会引用
* `gcTarget` 导致 `gcTarget` 本身不被回收,所以代码实现中对 `callback` 是持有的弱引用,
* 因此 `gcTarget` 本身需要持有 `callback` 的强引用
*
* @example
@bolasblack
bolasblack / test.js
Last active April 17, 2020 03:57
break lines in html5 canvas
import { textBreakLinesInCanvas } from "./textBreakLinesInCanvas"
const maxWidth = 300
const lineHeight = 24
document.querySelectorAll('canvas').forEach((e) => e.remove())
const canvas = document.createElement('canvas')
canvas.width = maxWidth
canvas.height = 300
canvas.style.lineHeight = `${lineHeight}px`
我的翻译内容基于 CC BY-NC-SA 3.0 协议
https://creativecommons.org/licenses/by-nc-sa/3.0/cn/deed.zh
launchd.plist(5) BSD File Formats Manual launchd.plist(5)
NAME
launchd.plist -- System wide and per-user daemon/agent configuration
files
launchd.plist -- 系统范围和用户级的守护进程/代理程序配置文件。
# 1
curl -o yarn -L `curl -s 'https://api.github.com/repos/yarnpkg/yarn/releases/latest' | jq -r '.assets | map(select(.name | test("legacy.*js$")) | .browser_download_url) | .[0] | tostring'` && chmod u+x yarn
# 2
VERSION=$(curl https://yarnpkg.com/latest-version)
curl -L -o yarn https://github.com/yarnpkg/yarn/releases/download/v$VERSION/yarn-legacy-$VERSION.js

Promise.race

const result = await Promise.race([promise1, promise2])
(go (let [[result chan] ;; result the value and while chan won
          (<! (alts! [chan1
 [value chan2] ;; even can wait a value &gt;! chan
@bolasblack
bolasblack / bfcache-test.js
Last active September 19, 2018 08:24
how to prevent bfcache
// modified from https://mixmax.com/blog/chrome-back-button-cache-no-store
/**
* Run this by downloading this script to your computer, then:
* 1. $ npm install express
* 2. $ node thisscript.js
* 3. Open localhost:8030/nocache and then localhost:8030/nostore
*/
const app = require('express')()
<html>
<div style="width: 100px">
<img src='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 24 24"><text x="0" y="12" font-size="12">你好</text></svg>' style="width: 50%" />
</div>
</html>
@bolasblack
bolasblack / rate_limit.clj
Created June 6, 2017 04:52
[limit concurrency operation with core.async]
(def RATE_LIMIT 5)
(defn operate [n]
(println "test-operate" n (.now js/Date))
(async/timeout 1000))
(defn -test []
(let [limit-chan (async/chan (- RATE_LIMIT 1))]
(go (doseq [n (range 20)]
(let [operate-chan (operate n)]
@bolasblack
bolasblack / example.js
Last active May 18, 2017 08:02
[Wirte once algorithm for sync & async function]
import co from 'co' // or whatever like co
import fetch from './fetch_data_synchronously'
import fetchAsync from './fetch_data_asynchronously'
export function process() {
const generator = processAlgorithmGenerator(fetch)
let next = { done: false }
while(!next.done) {
next = generator.next(next.value)
}