Skip to content

Instantly share code, notes, and snippets.

View key-container.ts
/**
* This module contains two data structures:
*
* - `KeyContainerMap` lets us use “key containers” as Map keys.
* - `KeyContainerSet` lets us use “key containers” as Set elements.
*
* A “key container” is an object that has a method `.getMapKey()`. Two key
* containers are considered equal if `.getMapKey()` returns the same value
* (often a string).
*
View gist-toc.mjs
#!/usr/bin/env node
// Usage:
// cat my-file.md | ./gist-toc.mjs
// pbpaste | ./gist-toc.mjs | pbcopy # clipboard -> clipboard (macOS)
function createSlug(str) {
str = str.replace(/[?%:`]/g, '');
str = str.replace(/ +/g, '-');
return str.toLowerCase();
View fork-join.mjs
import * as assert from 'assert/strict';
async function forkJoin(objOrArrOfPromises) {
let promises;
if (Array.isArray(objOrArrOfPromises)) {
promises = objOrArrOfPromises;
} else {
promises = Object.values(objOrArrOfPromises);
}
const settled = await Promise.allSettled(promises);
View caused-error.mjs
// Code moved here: https://2ality.com/2021/06/error-cause.html#a-custom-error-class
View array-cheat-sheet.md

Cheat sheet: JavaScript Array methods

Deriving a new Array from an existing Array:

['■','●','▲'].slice(1, 3)           ['●','▲']
['■','●','■'].filter(x => x==='■')  ['■','■']
    ['▲','●'].map(x => x+x)         ['▲▲','●●']
    ['▲','●'].flatMap(x => [x,x])   ['▲','▲','●','●']
View brief-an-die-politik.md

Brief an die Politik bzgl. COVID-19

Problem:

  • Die deutsche Politik scheint nicht zu verstehen, dass eine Mehrheit der Deutschen härtere Maßnahmen gegen COVID-19 mittragen würde, wenn dahinter eine Strategie stünde.
  • Um das zu kommunizieren, sind die Abgeordneten des eigenen Wahlkreises wahrscheinlich die besten Ansprechparter*innen. Hier kann man sie finden: https://www.bundestag.de/abgeordnete/wahlkreise/
  • Den folgen Text werde ich ihnen per E-Mail schicken.

( Lizenz: https://creativecommons.org/publicdomain/zero/1.0/deed.de )

View dual-mode-code.md

Dual mode code (that works both synchronously and asynchronously)

My use case: a Markdown parser that should work both synchronously and asynchronously. The latter forces the invoking code to switch to async, which is overkill when parsing simple strings.

Currently the parser is based on async generators.

Potential solution 1: CSP (communicating sequential processes) and generators

I did some experiments with CSP.

@rauschma
rauschma / impatient-js-es2021.md
Last active May 17, 2021
ES2021 edition of “JavaScript for impatient programmers”
View impatient-js-es2021.md

What is new in the ES2021 edition of “JavaScript for impatient programmers”?

Free to read online: exploringjs.com/impatient-js/

  • The exercises now run as native ESM modules on Node.js. Previously, they were run via the esm package.
  • Material on new ES2021 features:
    • String.prototype.replaceAll()
    • Promise.any()
    • Logical assignment operators
    • Underscores (_) as separators in number literals and bigint literals
View README.md

Using the “JS for impatient programmers” exercise setup with latest language features

Instructions:

  • Replace the existing package.json with the file shown here.
  • Execute in the shell:
    cd impatient-js-code*
    rm -rf node_modules
    npm install
    
@rauschma
rauschma / README.md
Last active Jan 25, 2021
Table: RegExp flags `/g` and `/y`
View README.md

Table: RegExp flags /g and /y

The following table was generated by the Node.js script:

/ /g /y /yg
r.exec(s) {i:0} {i:1} {i:1} {i:1}
.lI unch .lI upd .lI upd .lI upd
r.test(s) true true true true
.lI unch .lI upd .lI upd .lI upd