Skip to content

Instantly share code, notes, and snippets.

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])   ['▲','▲','●','●']
@rauschma
rauschma / impatient-js-es2021.md
Last active Sep 27, 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 string.md

Strings

String.prototype.*

None of the string methods modify this – they always return fresh strings.

  • charAt(pos: number): string ES1

    Returns the character at index pos, as a string (JavaScript does not have a datatype for characters). str[i] is equivalent to str.charAt(i) and more concise (caveat: may not work on old engines).

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).
*
@rauschma
rauschma / PreventUnknownGet.js
Last active Aug 12, 2021
Prevent getting of unknown properties via ES6 proxies
View PreventUnknownGet.js
// The following code is valid ECMAScript 6, but doesn’t work in Firefox, yet
function PreventUnknownGet() {
}
PreventUnknownGet.prototype = new Proxy(Object.prototype, {
get(target, propertyKey, receiver) {
if (!(propertyKey in target)) {
throw new TypeError('Unknown property: '+propertyKey);
}
// Make sure we don’t block access to Object.prototype
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 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 pandoc-experiences.md

Using Pandoc to publish a book in multiple file formats: experiences and wishes

Pandoc was essential for publishing my book “JavaScript for impatient programmers”. The book exists in several versions:

  • Printable PDF (for a print-on-demand book on Amazon)
  • Screen PDF
  • Multi-page HTML
  • EPUB
  • MOBI
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);