Skip to content

Instantly share code, notes, and snippets.

View RReverser's full-sized avatar
🇺🇦

Ingvar Stepanyan RReverser

🇺🇦
View GitHub Profile
@RReverser
RReverser / promise-tree.js
Created April 17, 2014 07:33
Promise Tree traversal
function when(value, func) {
var promise = Promise.cast(value);
if (func) {
promise = promise.then(function (value) {
return Promise.cast(func(value)).then(function (newValue) {
return newValue === undefined ? value : newValue;
});
});
}
feature Promises streams generators
asynchronous
(can push from inner callbacks)
true true false
multiple push false true true
error catch true false false
error forwarding true false true
external dependency is a must false true false
var stream = require('stream');
var Rx = require('rx');
var util = require('util');
function ObservableStream(transform) {
stream.Duplex.call(this, {objectMode: true});
this.subject = new Rx.Subject();
this.observable = transform(this.subject);
this.subscription = null;
@RReverser
RReverser / index.js
Created July 29, 2014 21:59
requirebin sketch
// just for RequireBin
console.log = function () {
var pre = document.createElement('pre');
pre.textContent = Array.prototype.map.call(arguments, function (arg) {
return typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg;
}).join(' ');
document.body.appendChild(pre);
};
var esprima = require('esprima-fb'), ast;
function Template(str, data) {
return str.replace(/\${([^}]+)}/g, (_, name) => data[name]);
}
var answer = 42;
(function () {
var answer = 60;
console.log(Template('Answer is ${answer}', {answer}));
})();
### Keybase proof
I hereby claim:
* I am RReverser on github.
* I am rreverser (https://keybase.io/rreverser) on keybase.
* I have a public key whose fingerprint is 3456 B5A0 0E7F 67D8 A049 5B85 7634 8879 1722 011A
To claim this, I am signing this object:
@RReverser
RReverser / es6-intrinsic.js
Created June 14, 2015 14:04
ES6 spec: 6.1.7.4 Well-Known Intrinsic Objects
/*
The MIT License (MIT)
Copyright (c) 2015 Ingvar Stepanyan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@RReverser
RReverser / index.js
Last active September 6, 2015 11:53
jsunderhood tutorial
#!/usr/bin/env node
var fs = require('fs');
var acorn = require('./parser');
var transform = require('./transform');
var escodegen = require('escodegen');
var filename = process.argv[2];
if (!filename) {
console.log('Usage: transpile filename.js');
var createChain = function (ops) {
var chain = function () {};
chain.__asyncOps = ops;
return new Proxy(chain, AsyncContextHandler);
};
function AsyncAccessError() {
TypeError.call(this, 'Can\'t perform synchronous operation on remote object.');
}
function awaitableVersionOf(nodeFunction) {
return (...args) => new Promise((resolve, reject) => {
nodeFunction(
...args,
(error, ...cbArgs) => error ? reject(error) : resolve(...cbArgs)
);
});
}