Skip to content

Instantly share code, notes, and snippets.

@bolasblack
bolasblack / test.js
Last active Apr 17, 2020
break lines in html5 canvas
View test.js
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`
View launchd.plist.5.man.txt
我的翻译内容基于 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 -- 系统范围和用户级的守护进程/代理程序配置文件。
View download latest singlefile yarn.sh
# 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
View core.async vs Promise.md

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 >! chan
@bolasblack
bolasblack / bfcache-test.js
Last active Sep 19, 2018
how to prevent bfcache
View bfcache-test.js
// 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')()
View 相对父元素宽度的字体块.html
<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 Jun 6, 2017
[limit concurrency operation with core.async]
View rate_limit.clj
(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
[Wirte once algorithm for sync & async function]
View example.js
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)
}
@bolasblack
bolasblack / cR.js
Last active May 18, 2017
[dot chain syntax for ramda.js]
View cR.js
import R from 'ramda'
class CR {
constructor(fn) {
this.runCR = (fn || R.identity)
}
}
R.keys(R).forEach(method => {
CR.prototype[method] = function() {
var rfn = arguments.length ? R[method].apply(null, arguments) : R[method]
@bolasblack
bolasblack / nodejs unlimited console.log.js
Last active Oct 5, 2018
nodejs unlimited console.log
View nodejs unlimited console.log.js
const util = require('util')
module.exports.global = () => {
util.inspect.defaultOptions.maxArrayLength = null
util.inspect.defaultOptions.depth = null
}
module.exports.consoleJson = () =>
console.json = (...args) =>
console.log.apply(console, args.map(arg =>