Skip to content

Instantly share code, notes, and snippets.

@bellbind
bellbind / async-array.js
Created Jun 22, 2020
[ECMAScript] array with async iterator
View async-array.js
const Notifier = class {
constructor() {
this.waits = new Set();
}
get wait() {
let d = {};
const p = new Promise((f, r) => {
d.r = r; d.f = f;
@bellbind
bellbind / cuckoo-filter.js
Last active Jun 18, 2020
[JavaScript]CuckooHash/Cuckoo Filter implementations
View cuckoo-filter.js
import {sha1} from "./sha1.js";
const te = new TextEncoder();
const eq = (a, b) => a && b && a.length === b.length &&
a.every((v, i) => v === b[i]);
// cuckoo filter: https://en.wikipedia.org/wiki/Cuckoo_filter
export const CuckooFilter = class {
constructor({
fingerprintSize = 4, tableSize = 17, pushLimit = 128, hash = sha1
@bellbind
bellbind / web-streams-examples.js
Created Jun 12, 2020
[JavaScript][deno]Web Streams API basic examples with `highWaterMark`
View web-streams-examples.js
// Web Streams API basic example for highWaterMark option
// 1. raw ReadableStream pattern
const rawReader = async () => {
const blob = new Blob(["Hello ", "World"]);
const rstream = blob.stream();
const reader = rstream.getReader();
try {
for (let {value, done} = await reader.read(); !done;
@bellbind
bellbind / collator-ja.js
Created Jun 10, 2020
[JavaScript] comparing order with Intl.Collator("ja-JP")
View collator-ja.js
const col = new Intl.Collator("ja-JP");
console.log("[category]");
// Category order: symbol < number < kana < kanji < Greek < Cyrillic
console.log(col.compare("-", "←")); //=> -1 (A < B)
console.log(col.compare("←", "♪")); //=> -1 (A < B)
console.log(col.compare("♪", "0")); //=> -1 (A < B)
console.log(col.compare("9", "A")); //=> -1 (A < B)
console.log(col.compare("Z", "あ")); //=> -1 (A < B)
console.log(col.compare("ん", "亜")); //=> -1 (A < B)
@bellbind
bellbind / index.html
Created Jun 5, 2020
[browser]SharedWorker example
View index.html
<!doctype html>
<html>
<head>
<script type="module">
const worker = new SharedWorker("./worker.js");
worker.port.addEventListener("message", ev => {
document.body.textContent = `connected: ${ev.data}`;
});
worker.port.start(); // spawm connect event to a shared worker
</script>
@bellbind
bellbind / index.html
Created Jun 4, 2020
[DOM][browser] simple AbortController example
View index.html
<!doctype html>
<html>
<head>
<script type="module">
// https://dom.spec.whatwg.org/#interface-AbortController
const ac = new AbortController();
document.querySelector("#aborted").textContent = ac.signal.aborted;
ac.signal.addEventListener("abort", ev => {
console.log("aborted"); // onabort spawned only once
document.querySelector("#aborted").textContent = ac.signal.aborted;
@bellbind
bellbind / index-40.html
Created May 25, 2020
[js-ipfs][browser] Pubsub chatroom with js-libp2p in js-ipfs node
View index-40.html
<!doctype html>
<html>
<head>
<title>Pubsub on libp2p (js-ipfs-0.40)</title>
<script type="module" src="./main-40.js"></script>
</head>
<body>
<h3 id="myid"></h3>
<div>
id: <input id="id" type="text" size="40"/>
@bellbind
bellbind / index-40.html
Created May 25, 2020
[js-ipfs][browser] Chat with js-libp2p in js-ipfs node
View index-40.html
<!doctype html>
<html>
<head>
<title>Chat on libp2p (js-ipfs-0.40)</title>
<script type="module" src="./main-40.js"></script>
</head>
<body>
<h3 id="myid"></h3>
<div>
to: <input id="to" type="text" size="40"/>
@bellbind
bellbind / index.html
Last active Apr 22, 2020
[chrome][android] BarcodeDetector example
View index.html
<!doctype html>
<html>
<head>
<script type="module">
// WICG Shape Detection API
// - https://wicg.github.io/shape-detection-api/
try {
const start = document.getElementById("start");
const video = document.getElementById("video");
const result = document.getElementById("result");
@bellbind
bellbind / bitstream.js
Last active Jun 1, 2020
[JavaScript] QR code generator from scratch
View bitstream.js
// big endian bit stream
export class BitWriter {
constructor(buf = [], byte = 0, pos = 7) {
this.byte = byte;
this.pos = pos;
this.buf = Array.from(buf);
}
writeBit(b) {
this.byte |= (b & 1) << this.pos;
this.pos--;
You can’t perform that action at this time.