Deriving a new Array from an existing Array:
['■','●','▲'].slice(1, 3) ⟼ ['●','▲']
['■','●','■'].filter(x => x==='■') ⟼ ['■','■']
['▲','●'].map(x => x+x) ⟼ ['▲▲','●●']
['▲','●'].flatMap(x => [x,x]) ⟼ ['▲','▲','●','●']
#!/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(); |
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); |
// Code moved here: https://2ality.com/2021/06/error-cause.html#a-custom-error-class |
Problem:
( Lizenz: https://creativecommons.org/publicdomain/zero/1.0/deed.de )
My use case: a Markdown parser that should work both synchronously and asynchronously. The latter forces the invoking code to switch to async, which is overkill when parsing simple strings.
Currently the parser is based on async generators.
I did some experiments with CSP.
Free to read online: exploringjs.com/impatient-js/
esm
package.String.prototype.replaceAll()
Promise.any()
_
) as separators in number literals and bigint literalsIf we terminate a loop early, for-of
invokes method .return()
of the iterator. Generators return generator objects (that implement both the Iterable interface and the Iterator interface). If we invoke .return()
on such an object, the generator is terminated. We can prevent that by overriding .return()
.
More information: https://exploringjs.com/es6/ch_iteration.html#sec_iteration-protocol-in-depth
Demonstration:
function logIter(iter) {