Skip to content

Instantly share code, notes, and snippets.

import fs from 'fs';
import { inspect } from 'util';
import path from 'path';
import { fileURLToPath } from 'url';
let __dirname = path.dirname(fileURLToPath(import.meta.url));
let usersToUpgrade = ['@justingrant:matrix.org'];
let targetPowerLevel = 50;
let roomId = '!nSLHQtIRJQxUJbEuGt:matrix.org'; // editor channel; this ID can be obtained from room settings -> Advanced
@bakkot
bakkot / parseargs-subcommands.js
Last active September 23, 2022 00:32
how to implement subcommands with node's parseArgs argument parser
'use strict';
let { parseArgs } = require('util');
let mainOptions = { help: { type: 'boolean', short: 'h' } };
let subCommands = {
__proto__: null,
foo: fooCommand,
bar: barCommand,
};

Optionally async functions

Or: two-color functions.

The problem: async is infectious.

There's a famous essay about this, called What Color is your Function, which I recommend reading.

For example, say you're writing a bundler. It needs to fetch resources, but is agnostic to how those resources are fetched, so it takes a readResource function from its caller. In some contexts the caller might have those resources synchronously available; in others not. That is, caller might have a synchronous readResource which returns a resource immediately, or an async readResources which returns a promise for a resource, which will need to be unwrapped with await.

@bakkot
bakkot / globals.md
Last active November 29, 2018 20:55

globalThis

  • PRO: almost shipping in Chrome, maybe used elsewhere
  • PRO: is actually correct
  • CON: might confuse JS beginners

Global

  • PRO: maybe less likely to confuse beginners
  • PRO/CON: looks like a namespace
  • CON: requires even more churn on this proposal
@bakkot
bakkot / es2016..es2017.md
Last active May 4, 2017 21:03
normative es2017 changes
  1. d28ebe6d Normative: Specify Date.UTC when called with fewer than two arguments (#642)

  2. 0916d813 Normative: Use explicit comparison to compare two lists in Valid Chosen Reads

  3. 90ed6ce2 Normative: Call NumberToRawBytes instead of the conversion operation to get the byte values list in Atomics.compareExchange

  4. 7949fae4 Normative: Only check for undefined count parameter in Atomics.wake, because it's not marked as optional

  5. a4d0e490 Normative: Unconditionally call ToNumber on the timeout parameter in Atomics.wait, because it's not marked as optional

let frame = document.body.appendChild(document.createElement('iframe'));
frame.addEventListener('load', ()=>{
let w = frame.contentWindow;
let evt;
w.addEventListener('error', e=>{evt = e;});
let scr = w.document.createElement('script');
scr.text = 'throw 42;';
var p = () => console.log(f);
{
p(); // undefined
console.log(f); // function f(){}
f = 1;
p(); // undefined
console.log(f); // 1

Keybase proof

I hereby claim:

  • I am bakkot on github.
  • I am bakkot (https://keybase.io/bakkot) on keybase.
  • I have a public key whose fingerprint is FD44 B8F2 65AC E177 48C5 23C8 A07A 119B 504C 8D8F

To claim this, I am signing this object:

https://www.googleapis.com/download/storage/v1/b/chromium-browser-continuous/o/Win%2F100056%2Fchrome-win32.zip?generation=1&alt=media
(function () {
var g, h;
function x() { f = ""; }
function y() { h = f; }
{
// var-scoped f is undefined, let-scoped f is a function
f = 1; // let-scoped f gets value 1
x(); // var-scoped f gets value ""
y(); // h gets value of var-scoped f
function f() {} // var-scoped f gets value of let-scoped f