Skip to content

Instantly share code, notes, and snippets.

@Kamisama666
Kamisama666 / reduceEventEmmitter.js
Created Jul 30, 2019
Listens to an EventEmitter and reduces the data returned.
View reduceEventEmmitter.js
/*
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);
});
View smartQuery.js
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) {
View printRangeTest.js
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;
}
View reduce_example.js
const searchedBookShelf = originalBooks.reduce((searchedBook, originalBook) => {
if (originalBook.id === bookOnShelf.id) {
return originalBook.shelf;
}
return searchedBook;
}, null);
@Kamisama666
Kamisama666 / reduceObservableToPromise.js
Last active Aug 29, 2017
Method to reduce an RxJs Observable to a Promise.
View reduceObservableToPromise.js
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 Jul 20, 2017
Composable function that checks if an object has a key, throws an error if not
View has.js
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 Jul 20, 2017
Composable OR function
View or.js
const fn = require('fn.js');
const or = fn.curry((defaultValue, value) => value ? value : defaultValue);
@Kamisama666
Kamisama666 / reactive-promises-paginations.js
Created Jun 27, 2017
Using promises and reactive programming (RxJs) to implement asynchronous pagination
View reactive-promises-paginations.js
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));
});
View qp.js
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
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
View mapAll.js
"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);