Skip to content

Instantly share code, notes, and snippets.

@zenparsing
zenparsing / observable-scheduling.js
Last active August 29, 2015 14:20
Scheduling Observable using Promises
Observable.from = function(iterable) {
return new Observable((push, error, close) => {
try { for (let x of iterable) push(x) }
catch (x) { error(x) }
finally { close() }
});
};
@zenparsing
zenparsing / definitely-maybe.js
Last active August 29, 2015 14:19
Definitely Maybe with Proxy
const unwrapSymbol = Symbol();
function Definitely(x) {
if (x == null || !(unwrapSymbol in x))
return x;
return x[unwrapSymbol];
}
@zenparsing
zenparsing / optional-chaining-proxy.js
Last active April 10, 2023 18:14
Optional chaining with JS Proxy
const unwrapSymbol = Symbol();
function unwrap(x) {
if (this == null)
return this;
let f = this[unwrapSymbol];
if (typeof f !== "function")
@zenparsing
zenparsing / jake.js
Last active August 29, 2015 14:09
jQuery Reimagined
/* Redefine jQuery methods as "raw" methods */
(function($) { "use strict";
function wrap(method) {
return function() {
var jq = $(this),
result = jq[method].apply(jq, arguments);
@zenparsing
zenparsing / jquery-direct.js
Last active August 29, 2015 14:09
jquery direct
(function($) { "use strict";
function wrap(method) {
return function() {
var jq = $(this),
result = jq[method].apply(jq, arguments);
if (result && result.constructor === $)
@zenparsing
zenparsing / abstract-references.md
Last active August 29, 2015 14:07
Abstract References for ECMAScript

Abstract References for ECMAScript

Overview and Motivation

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:

@zenparsing
zenparsing / dedent-template.js
Last active April 25, 2023 22:16
Dedenting Template Strings
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;
@zenparsing
zenparsing / measure-await.js
Created September 3, 2014 21:09
Await Performance
async function queueAlways() {
let x;
for (let i = 0; i < 100000; ++i)
x = await i;
}
async function queueIf() {
@zenparsing
zenparsing / async-io-1.js
Last active August 29, 2015 14:05
Reading and Writing Files with Async Generators
/*
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) {
@zenparsing
zenparsing / Promise.js
Created December 28, 2013 03:48
Minimal ES6 Promise
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";