Despite its functional programming facilities, ECMAScript exhibits a strong preference for object-oriented "left-to-right" composition using instance methods. Unfortunately, composition by means of instance methods requires that extensibility be achieved by adding function-valued properties to the object or a prototype ancestor of the object. This leads to the following difficulties:
This file contains hidden or 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
Observable.from = function(iterable) { | |
return new Observable((push, error, close) => { | |
try { for (let x of iterable) push(x) } | |
catch (x) { error(x) } | |
finally { close() } | |
}); | |
}; |
This file contains hidden or 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 unwrapSymbol = Symbol(); | |
function Definitely(x) { | |
if (x == null || !(unwrapSymbol in x)) | |
return x; | |
return x[unwrapSymbol]; | |
} |
This file contains hidden or 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 unwrapSymbol = Symbol(); | |
function unwrap(x) { | |
if (this == null) | |
return this; | |
let f = this[unwrapSymbol]; | |
if (typeof f !== "function") |
This file contains hidden or 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
/* Redefine jQuery methods as "raw" methods */ | |
(function($) { "use strict"; | |
function wrap(method) { | |
return function() { | |
var jq = $(this), | |
result = jq[method].apply(jq, arguments); |
This file contains hidden or 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($) { "use strict"; | |
function wrap(method) { | |
return function() { | |
var jq = $(this), | |
result = jq[method].apply(jq, arguments); | |
if (result && result.constructor === $) |
This file contains hidden or 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 dedent(callSite, ...args) { | |
function format(str) { | |
let size = -1; | |
return str.replace(/\n(\s+)/g, (m, m1) => { | |
if (size < 0) | |
size = m1.replace(/\t/g, " ").length; |
This file contains hidden or 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
async function queueAlways() { | |
let x; | |
for (let i = 0; i < 100000; ++i) | |
x = await i; | |
} | |
async function queueIf() { |
This file contains hidden or 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
/* | |
This example uses an inner generator and skips over the first iteration in order to | |
prevent data loss which occurs on the first call to next. | |
*/ | |
import * as FS from "async-fs"; | |
export function readFile(path) { |
This file contains hidden or 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 enqueueMicrotask(fn) { | |
// Enqueue a function on the System-defined FIFO | |
// microtask queue. | |
} | |
var $status = "Promise#status", | |
$value = "Promise#value", | |
$onResolve = "Promise#onResolve", | |
$onReject = "Promise#onReject"; |