Skip to content

Instantly share code, notes, and snippets.

View rehia's full-sized avatar

Jérôme Avoustin rehia

View GitHub Profile
@rehia
rehia / foo
Last active August 29, 2015 14:11
How to test that something didn't happen in JavaScript other than with setTimeout ?
function Foo (emitter, expectedPerson) {
this.waitForSomething = function(callback) {
emitter.on('something_happened', function(person) {
if (expectedPerson === person) {
callback(person);
}
});
};
return this;
};
@rehia
rehia / complex.js
Last active August 29, 2015 14:19
Problem with promises : is it the right way ?
var client, payment;
return clientDao.get(id)
.then(function (foundClient) {
client = foundClient;
return paymentDao.get(paymentId);
}).then(function(foundPayment) {
client.credit(payment.amount);
payment.cancel();
return clientDao.save(client);
}).then(function() {
@rehia
rehia / rx-test-FIXED.js
Last active February 24, 2016 00:00
Don't manage to concat an observable with a hot observable, with buffering the 2nd one while the 1st is actually working
'use strict';
var Rx = require('rx');
var EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter();
var observable1 = new Rx.Subject();
var buffer = new Rx.ReplaySubject();
var observable2 = Rx.Observable.fromEvent(emitter, 'test');
@rehia
rehia / experience.js
Last active July 4, 2016 16:43
yielding array of promises
'use strict';
const fs = require('fs');
const co = require('co');
const files = ['./plan.md', './karma.conf.js', './package.json', './LICENSE'];
function checkFile(file) {
return new Promise((fulfill, reject) => {
console.log('checking file', file);
fs.stat(file, (error) => {
@rehia
rehia / debounceDistinct.js
Last active July 11, 2016 21:24
Debounce data based on a key or value
Rx.Observable.prototype.debounceDistinct = function debounceDistinct(delay, keySelector = value => value) {
var source = this;
return Rx.Observable.create((observer) => {
const allKeys = {};
function reinitDelayFor(key) {
allKeys[key] = false;
setTimeout(() => allKeys[key] = true, delay);
}
@rehia
rehia / Log with `Promise.all()`
Created July 25, 2016 18:25
Strange behavior with co, `yield*`, promises, and Node (v. 6.3.1)
/somewhere/yield.js:14
throw reason;
^
Error: test value a has some problems
at Promise (/somewhere/yield.js:19:12)
at display (/somewhere/yield.js:18:10)
at Array.map (native)
at /somewhere/yield.js:10:28
at next (native)
@rehia
rehia / createIndex
Last active January 19, 2017 14:22
Elasticsearch configuration for autocompletion search
{
settings: {
number_of_shards: 1,
analysis: {
filter: {
autocomplete_filter: {
type: 'ngram',
min_gram: 3,
max_gram: 15
}
@rehia
rehia / test.js
Created March 4, 2017 16:01
how to go functional?
'use strict';
const _ = require('lodash');
const chai = require('chai');
const should = chai.should();
describe('filter groups behavior', () => {
const X = [
{name: 'a'},
{name: 'b'},
const O = (a) => {
let compose = function* () {
for (let v of a) {
yield v;
}
};
const filter = function(p) {
let rest = compose;
compose = function* () {
{
"action":"edited",
"number":1,
"pull_request":{
"url":"https://api.github.com/repos/design-all-the-things/test-repo/pulls/1",
"id":321655669,
"node_id":"MDExOlB1bGxSZXF1ZXN0MzIxNjU1NjY5",
"html_url":"https://github.com/design-all-the-things/test-repo/pull/1",
"diff_url":"https://github.com/design-all-the-things/test-repo/pull/1.diff",
"patch_url":"https://github.com/design-all-the-things/test-repo/pull/1.patch",