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
import { watch } from 'node:fs/promises';
import * as fs from 'node:fs';
import * as path from 'node:path';
async function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
const bin = path.basename(process.argv[1]);

Creating synchronous iterators

The methods of class Iterator let us process data incrementally. Let’s explore where we can use them.

Getting iterators from iterables
  • Iterator.from(iterable) always returns instances of Iterator (converting non-instances to instances as needed).
  • iterable[Symbol.iterator]() returns an iterator:
    • With all built-in data structures, the result is an instance of Iterator.
    • With other, older iterable objects, the result may not be an instance of Iterator.

Framework Laptop 13 DIY Edition (previous generation, 2024-05-20)

For sale in Munich (to be picked up in person): EUR 700

  • Original price (2024-05-20): €1269
  • Intel Core i5-1340P (13th Gen)
  • RAM: 2 × 16GB (DDR4-3200)
  • SSD: 500GB (WD_BLACK SN770 NVMe)
  • Expansion “cards” (that slot into the laptop case):
  • 2 USB-C
@rauschma
rauschma / create-enum.ts
Last active March 25, 2025 12:50
createEnum(): helper for creating enum objects
// More information on enum objects as an alternative to enums:
// https://2ality.com/2025/01/typescript-enum-patterns.html#alternative-to-enum%3A-object-literal-1
/**
* Returns an enum object. Adds the following improvements:
* - Sets the prototype to `null`.
* - Freezes the object.
* - The result has the same type as if `as const` had been applied.
*/
function createEnum<

Book on JavaScript

  • If you see a JavaScript feature in this book that you don’t understand, you can look it up in my book “Exploring JavaScript” which is free to read online. Some of the “Further reading” sections at the ends of chapters refer to this book.

Books on TypeScript

// Experiment: I’m not sure if I would use this myself.
const call = Symbol('call');
class Callable {
constructor() {
// Can’t use .bind() here. Not sure why. Maybe the result doesn’t
// interact well with Object.setPrototypeOf().
const _this = (...args) => new.target.prototype[call].call(_this, ...args);
Object.setPrototypeOf(_this, new.target.prototype);
return _this;
@rauschma
rauschma / partial-application.ts
Last active January 29, 2025 13:07
Typing partial application in TypeScript
//========== Testing types ==========
const expectType = <Type>(_: Type): void => void 0;
type TypeEqual<Target, Value> = (<T>() => T extends Target
? 1
: 2) extends <T>() => T extends Value ? 1 : 2
? true
: false;
//========== applyPartial ==========
// Q: Why not an object literal?
// A: Then you have to create separate constants for the symbols:
// https://2ality.com/2025/01/typescript-enum-patterns.html#using-symbols-as-property-values
type PropertyValues<Obj> = Obj[keyof Obj];
function createEnum<C extends new (...args: unknown[]) => unknown>(enumClass: C): Omit<C, 'prototype'> {
return enumClass;
}
const Color = createEnum(class {
type CreateTuple<Len extends number, Acc extends unknown[] = []> =
Acc['length'] extends Len
? Acc
: CreateTuple<Len, [...Acc, true]>;
;
type Length<Tup extends Array<unknown>> =
Tup['length']
;
type Unshift<Tuple extends Array<unknown>, Value> =