Skip to content

Instantly share code, notes, and snippets.

@Kamisama666
Kamisama666 / reduceEventEmmitter.js
Created July 30, 2019 09:55
Listens to an EventEmitter and reduces the data returned.
/*
Listens to an EventEmitter and reduces the data returned. The EventEmitter must at least three types of events:
- dataEvent: it sends the data to be reduced
- endEvent: it signals the completion of the operation
- errorEvent: it sends an error. The operation is interrupted to handle it
*/
function reduceEventEmmitter({dataEvent, endEvent, errorEvent = null}, accumulator = [], reducer, eventEmitter, cb) {
eventEmitter.on(dataEvent, function handleDataEvent(data) {
accumulator = reducer(accumulator, data);
});
const async = require('async');
const pg = require('pg')
// QueryConnect: (QuerySource, Callback) -> Connection
// QueryDisconnect: Connection -> Connection
// getSmartQuery : (QueryConnect, QueryDisconnect, QuerySource) -> SmartQuery
function getSmartQuery(connect, disconnect, querySource) {
let conn = null;
function connectClient(query, _connectClient) {
const unique = list => list.filter((value, index, list) => list.indexOf(value) === index);
const sortNumber = (a, b) => a - b;
const expandBlock = (accum, block) => {
if (block.indexOf('-') === -1) {
accum.push(parseInt(block));
return accum;
}
const searchedBookShelf = originalBooks.reduce((searchedBook, originalBook) => {
if (originalBook.id === bookOnShelf.id) {
return originalBook.shelf;
}
return searchedBook;
}, null);
@Kamisama666
Kamisama666 / reduceObservableToPromise.js
Last active August 29, 2017 14:00
Method to reduce an RxJs Observable to a Promise.
const Q = require('q');
/**
* Method to reduce observable to a promise.
*
* @param observable
*/
function reduceToPromise(observable) {
return observable.reduce((acc, item) => [...acc, item], [])
.toPromise(Q.Promise);
@Kamisama666
Kamisama666 / has.js
Created July 20, 2017 11:23
Composable function that checks if an object has a key, throws an error if not
const fn = require('fn.js');
const has = fn.curry(function(key, object) {
if (!object[key]) {
throw new Error(`Missing key '${key}' on object`);
}
return object;
})
@Kamisama666
Kamisama666 / or.js
Created July 20, 2017 11:16
Composable OR function
const fn = require('fn.js');
const or = fn.curry((defaultValue, value) => value ? value : defaultValue);
@Kamisama666
Kamisama666 / reactive-promises-paginations.js
Created June 27, 2017 11:14
Using promises and reactive programming (RxJs) to implement asynchronous pagination
const Rx = require('rx');
getPages(5, 1)
.then(reduceToPromise)
.then(console.log)
function getPages(perPage, page) {
return new Promise(function(resolve, reject) {
resolve(getAllPages(perPage, page));
});
const Q = require('q');
/**
* Transforms a Q Promise to a Promise
* @param {QPromise} qpromise
* @return {Promise}
*/
function qp(qpromise) {
return new Promise(function(resolve, reject) {
qpromise
@Kamisama666
Kamisama666 / mapAll.js
Created May 8, 2017 10:20
Maps a function that returns a promise and then passes that array of promises to Q.all so they are all processed. Uses Fn.js and Q. Example included
"use strict";
const Q = require('q');
const fn = require('fn.js');
// Maps a function that returns a promise and then passes that array of promises to Q.all so they are all processed
const mapAll = fn.curry(fn.compose(Q.all, fn.map), 2);
// Returns a promise
function returnPromise(something) {
return Q().then(() => something);