Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile
<!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
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: