Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile

My writing process

Stages of my writing process

  • Collecting material: For a number of topics that I may write about in the future, I have text files where I collect information as I come across it during coding, on the web, on Twitter, etc.

  • Outline: The collected material is my starting point. I rearrange it into an outline which I refine until I’m happy with it. Refining includes adding/removing/rearranging items and doing more research when I notice gaps in my knowledge.

  • Skeletal draft: I add bullet points and code examples until almost all of the content exists at least in skeletal form. This process often uncovers knowledge gaps and flaws in the structure of the content which I then can fix.

These are a few thoughts on how Node.js apps can make data portable between platforms. The focus in this case is on paths. Handling line terminators is covered elsewhere.

Feedback welcome!

Using the same paths on different platforms

Sometimes we’d like to use the same paths on different platforms. Then there are two issues that we are facing:

  • The path separator may be different.
  • The file structure may be different: home directories and directories for temporary files may be in different locations, etc.

Creating standalone ESM-based Node.js scripts on Unix and Windows

Approach:

  1. The file starts with shell code.
  2. That code uses Node.js to execute the file in ESM mode, after it removes the initial non-JavaScript lines.
    • Node.js does not currently have CLI flags for achieving what we do in this step. Therefore, we have to pipe to the node executable.

When editing this file, we want to use the JavaScript mode of our IDE or editor. Therefore, we try to “hide” the shell code from JavaScript as much as possible.

@rauschma
rauschma / README.md
Last active May 10, 2023 09:53
Better dynamic type checks

Better dynamic type checks

  • Update 2022-07-10: new approach based on Function.prototype.hasInstance()

Problems

In principle, the rule “typeof is for primitive values, instanceof is for objects” works. There are two exceptions:

  • Checking if a value is an object
  • Cross-realm instance checks
//========== .split() with lookbehind ==========
// Downside: Safari doesn’t support lookbehind
import * as assert from 'node:assert/strict';
const RE_SPLIT_AFTER_EOL = /(?<=\r?\n)/;
function splitLinesWithEols1(str) {
return str.split(RE_SPLIT_AFTER_EOL);
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Transformer Test</title>
</head>
<body>
<script type="module">
/**
import {ReadableStream} from 'node:stream/web';
/**
* @param iterable an iterable (asynchronous or synchronous)
*
* @see https://streams.spec.whatwg.org/#example-rs-pull
*/
function iterableToReadableStream(iterable) {
return new ReadableStream({
async start() {
import {Readable} from 'node:stream';
import {TextDecoderStream} from 'node:stream/web';
import {spawn} from 'node:child_process';
const childProcess = spawn(
'echo', ['hello world'],
{
stdio: ['ignore', 'pipe', 'inherit'],
}
);
@rauschma
rauschma / about.md
Last active May 25, 2022 17:23
Transferring objects between a ShadowRealm and an incubator realm

Transferring objects between a ShadowRealm and an incubator realm

Material:

//========== Solution 1: array.flatMap() and array.map() ==========
const prios = (arrayOfArrays) => arrayOfArrays.flatMap(
(arr, index) => {
return arr.map(elem => ({...elem, priority: index+1}))
}
);
// .flatMap() both maps and flattens
// More information: https://exploringjs.com/impatient-js/ch_arrays.html#Array.prototype.flatMap