Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile
import * as assert from 'assert/strict';
/**
* I’m not sure if this is practically useful but:
* The single utility method .findValue() for Arrays can be used
* to implement several existing Array methods.
*/
Array.prototype.findValue = function (cb, defaultResult, thisValue) {
thisValue ??= this;
for (let i=0; i<this.length; i++) {

Cheat sheet: classes

A JavaScript class:

class Person {
  constructor(firstName) { // (A)
    this.firstName = firstName; // (B)
  }
  describe() { // (C)

Cheat sheet: modules

Named exports, named imports, namespace imports

If we put export in front of a named entity inside a module, it becomes a named export of that module. All other entities are private to the module.

//===== lib1.mjs =====
// Named exports

Cheat sheet: strings

Strings are primitive values in JavaScript and immutable. That is, string-related operations always produce new strings and never change existing strings.

Working with strings

Literals for strings:

const str1 = 'Don\'t say "goodbye"'; // string literal

Cheat sheet: Arrays

JavaScript Arrays are a very flexible data structure and used as lists, stacks, queues, tuples (e.g. pairs), etc. Some

Using Arrays

Creating Arrays, reading and writing elements:

/*
Functionality – when executed, these lines are copied to the clipboard:
1. The URL of the current web page.
2. The title of the current web page (as originally written)
3. The title of the current web page (converted to sentence case with a crude algorithm)
4. Optionally: The currently selected text (if any).
Installation: Create a bookmark whose URL is the code below (including `javascript:{···}`):
– Tested in Chrome and Safari.
– Both browsers can handle URLs with multiple lines.

ECMAScript module specifiers

There are three kinds of module specifiers:

  • Absolute specifiers are full URLs that are used directly – for example:
    'file:///opt/nodejs/config.mjs'
    'https://unpkg.com/liltest@0.0.5/dist/liltest.js'
    

Cheat sheet: public prototype methods and accessors

const getterKey = Symbol('getterKey');
const setterKey = Symbol('setterKey');
const syncMethodKey = Symbol('syncMethodKey');
const syncGenMethodKey = Symbol('syncGenMethodKey');
const asyncMethodKey = Symbol('asyncMethodKey');
const asyncGenMethodKey = Symbol('asyncGenMethodKey');
function hyphenize(str) {
// /y is to make sure that there are no characters between the matches
// /g is so that .match() works the way we need here
// /u is not strictly necessary here, but generally a good habit
return str.match(/([a-z]{1,2})/uyg).join('-');
}
assert.equal(
hyphenize('hello'), 'he-ll-o'
);
/**
* 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).
*