Skip to content

Instantly share code, notes, and snippets.

View benjamingr's full-sized avatar
🖊️
Limited availability.

Benjamin Gruenbaum benjamingr

🖊️
Limited availability.
View GitHub Profile
const React = require('react');
const { expect } = require('chai');
const { shallow, configure } = require('enzyme');
const { createSandbox } = require('sinon');
require('react-dom'); // required by the enzyme adapter internally
const Adapter = require('enzyme-adapter-react-16');
configure({ adapter: new Adapter() });
var called = 0;
const React = require('react');
const { expect } = require('chai');
const { shallow, configure } = require('enzyme');
const { createSandbox } = require('sinon');
require('react-dom'); // required by the enzyme adapter internally
const Adapter = require('enzyme-adapter-react-16');
configure({ adapter: new Adapter() });
var called = 0;
class AggregateAbortController extends AbortController {
constructor(...controllers = []) {
super();
this.controllers = controllers;
}
abort() {
for(const controllers of this.controllers) {
controllers.abort();
}
}
// generators are lazy so maxItems isn't needed anymore - you just take as many items as you need
// it's also lazy, so if you need less items you will pay less for it. Naming a Set `set` is like naming a Number `number`
const uniqueMergeWithoutMax = function *(arrays, withoutValue) {
const seen = new Set();
for(const array of arrays) {
for(const item of array) {
if (item === withoutValue || seen.has(item)) continue;
seen.add(item);
yield item;

A Promise handles a single event when an async operation completes or fails.

So, a promise doesn't handle anything - a promise is just a value over time. The charactaristics of promises are that:

  • Because a promise is just a value - promises are multicast which means multiple .thens are 'transparent'.
  • Native promises are what async functions return and what they await.
  • Promises have three states (fulfilled, pending and rejected) and transition between them just once.

Promises aren't really eager - but if you have a promise an operation has already started - so terms lik running promises don't make sense. They have a built in (stage 4 and part of JavaScript) multi-value counterpart (async iterators) which do multiple values and

function cleanUp (arr, max) {
const cnts = {} // keep track of what we find
return arr.reduce((a, i) => { // loop over the array index by index
cnts[i] = (cnts[i] || 0) + 1; // mark that I seen the number
if (cnts[i] <= max) { // check to see if we are under the max
return a.concat(i) //if we are, add it to an arry
}
return a // return the array for reduce
}, [])
}
const obj1 = {"1,1" : "hello"},
obj2 = {"1,1" : "hi"},
obj3 = [obj1, obj2].reduce((a, c) => {
return {...a, [k] : (a[k] || []).concat(c[k]);
}, {});
@benjamingr
benjamingr / peer5-load-conditionally.js
Last active August 31, 2017 08:13
This code snippet lets you load Peer5 without
// load Peer5 with a script timeout
var initiated;
function initClappr() {
if (initiated) return;
initiated = true;
// var player = new Clappr.Player();
// rest of clappr initialization
}
Eggs.Map = function() {}
,
Eggs.Map.init = function() {
function t() {
var t = ($(window).scrollTop() + $(window).height()) / $(document).height() * .6;
s.css("background-size", t * a + "px auto"),
s.css("background-position", 800 - o.x * t + "px " + (40 - o.y * t) + "px")
}
function e() {
var t = i()
diff --git a/benchmark/doxbee-sequential/callbacks-caolan-async-waterfall.js b/benchmark/doxbee-sequential/callbacks-caolan-async-waterfall.js
deleted file mode 100644
index 439f9bba..00000000
--- a/benchmark/doxbee-sequential/callbacks-caolan-async-waterfall.js
+++ /dev/null
@@ -1,71 +0,0 @@
-require('../lib/fakes');
-var async = require('async');
-
-module.exports = function upload(stream, idOrPath, tag, done) {