Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile
#!/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();
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);
// Code moved here: https://2ality.com/2021/06/error-cause.html#a-custom-error-class

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])   ['▲','▲','●','●']

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 )

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 August 31, 2023 07:02
ES2021 edition of “JavaScript for impatient programmers”

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

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 January 25, 2021 08:57
Table: RegExp flags `/g` and `/y`

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

for-of, early termination, iterators, and generators

If we terminate a loop early, for-of invokes method .return() of the iterator. Generators return generator objects (that implement both the Iterable interface and the Iterator interface). If we invoke .return() on such an object, the generator is terminated. We can prevent that by overriding .return().

More information: https://exploringjs.com/es6/ch_iteration.html#sec_iteration-protocol-in-depth

Demonstration:

function logIter(iter) {