-
Alice is writing a new library, and she is excited to use the new ES6 syntax. However, she would like to use an older but still good package she found on npm, that exports its interface using CommonJS. She does so easily after reading the NodeJS documentation on how to do this.
-
Bob is updating a module for his work, and he needs to support existing CommonJS-using codebases as well as a new project that prefers to stick with ES6 for static analysis reasons. He publishes a package that exports both kinds of interfaces.
-
Carol is updating her popular code coverage reporting tool for the new world. She uses the ESM loader hooks to instrument test code as it is imported so she can get code coverage reporting on par with what she has for CommonJS.
-
David is writing a transpiler. He writes a custom hook that transpiles source as it's loaded from his language to JavaScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: fastRead('Lorem ipsum dolor sit amet') | |
function fastRead(text, left = '<b>', right = '</b>') { | |
return text?.replace(/[A-Za-zÀ-ÖØ-öø-ÿ0-9]+/gm, (word) => { | |
const split = word.length > 3 ? Math.ceil(word.length / 2) : 1; | |
return left + word.substring(0, split) + right + word.substring(split); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From callbacks to Promises to async functions | |
function callbackFunc(x, callback) { | |
f1(x, (err1, result1) => { | |
if (err1) { | |
console.error(err1); | |
callback(err1); | |
return; | |
} | |
f2(result1, (err2, result2) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function logColor(color, args) { | |
console.log(`%c ${args.join(' ')}`, `color: ${color}`); | |
} | |
const log = { | |
aliceblue: (...args) => { logColor('aliceblue', args)}, | |
antiquewhite: (...args) => { logColor('antiquewhite', args)}, | |
aqua: (...args) => { logColor('aqua', args)}, | |
aquamarine: (...args) => { logColor('aquamarine', args)}, | |
azure: (...args) => { logColor('azure', args)}, |
- Early work by Ihab Awad (Google) and Kris Kowal (FastSoft)
- 2009-09: 2nd draft of module strawman, still very close to "closures as modules",
export x = 42
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var kb = require("ble_hid_keyboard"); | |
NRF.setServices(undefined, { hid : kb.report }); | |
var reset_timer; | |
var next = "n"; | |
var prev = "p"; | |
function sendCharNext(){ | |
if (next == next.toLowerCase()){ | |
sk = 0; | |
} else { |
by Wassim Chegham (@manekinekko)
From this awesome animation, originally from this tweet
getData( a => {
getMoreData(a, b => {
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pixels = ['r','g','b','a','r','g','b','a','r','g','b','a']; | |
const propMap = ['r', 'g', 'b', 'a']; | |
const rgbas = pixels.reduce((acc, curr, i) => { | |
const mod = i%4; | |
switch (mod) { | |
case 0: | |
return [...acc, {r: curr}]; | |
default: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* tslint:disable */ | |
const program = | |
` | |
module "login" | |
go to "app/login" | |
fill "[email protected]" in "#username" | |
fill "foobar" in "#password" | |
click "#login" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
let canvas = document.querySelector('canvas'); | |
// Optional frames per second argument. | |
let stream = canvas.captureStream(25); | |
var options = {mimeType: 'video/webm; codecs=vp9'}; | |
let recorder = new MediaRecorder(stream, options); | |
let blobs = []; | |
function download(blob) { | |
var url = window.URL.createObjectURL(blob); |
NewerOlder