Skip to content

Instantly share code, notes, and snippets.

View ccnokes's full-sized avatar

Cameron Nokes ccnokes

View GitHub Profile
@ccnokes
ccnokes / axios-instance-config.js
Created July 6, 2017 16:23
Good default configuration for axios in node.js
const axios = require('axios');
const http = require('http');
const https = require('https');
module.exports = axios.create({
//60 sec timeout
timeout: 60000,
//keepAlive pools and reuses TCP connections, so it's faster
httpAgent: new http.Agent({ keepAlive: true }),
@ccnokes
ccnokes / yes.js
Last active June 30, 2017 17:12
unix yes command in node.js
// throughput is usually ~350-400 MiB/s
// run: node yes.js | pv > /dev/null
const buf = Buffer.alloc(4096, 'y\n', 'utf8');
const str = buf.toString();
const { Readable } = require('stream');
class Y extends Readable {
_read() {
this.push(str);
@ccnokes
ccnokes / waitAtLeast.js
Created May 31, 2017 18:46
Make an async function take at least X amount of time
/**
* make an async function take at least X amount of time
* @param limit - in ms
* @param fn - returns promise
* @returns {Promise.<T>}
*/
function waitAtLeast(limit, fn) {
let start = Date.now();
let end = start + limit;
@ccnokes
ccnokes / detect-reflow.js
Created May 27, 2017 05:34
script for detecting DOM method calls that are know to cause a reflow
// These methods and getter/setters force layout/reflow in Chrome/WebKit
// From https://gist.github.com/paulirish/5d52fb081b3570c81e3a
const getterSetters = [
'offsetLeft',
'offsetTop',
'offsetWidth',
'offsetHeight',
'offsetParent',
'clientLeft',
@ccnokes
ccnokes / make-lodash-decorator.js
Last active July 5, 2018 09:17
Wrap a lodash method in a TS decorator
//wrap a fn that returns a function into a decorator
function makeFnWrapDecorator(fnWrapper: Function) {
return function decoratorWrapper(...args) {
return function decorator(target, propertyKey: string, descriptor: PropertyDescriptor) {
const fn = descriptor.value;
let wrappedFn = fnWrapper.apply(null, [fn, ...args]);
return {
configurable: true,
get() {
return wrappedFn;
@ccnokes
ccnokes / lazy-container.js
Last active December 15, 2017 14:11
A map that lazily gets and caches values on access
function makeLazy(name, fn, obj = {}) {
const nullSym = Symbol('nil'); //could change this to be more es5 friendly
let val = nullSym; //I'm not sure if holding the value in this closure or right on the object is better
Object.defineProperty(obj, name, {
enumerable: true,
get() {
if(val === nullSym) {
val = fn();
@ccnokes
ccnokes / pipe.js
Last active May 21, 2017 02:06
Pipe function, taken from twitter
// sync version
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
// example
const newFunc = pipe(fn1, fn2, fn3);
const result = newFunc(arg);
// async version
// take a series of promise producing functions and return a single promise
@ccnokes
ccnokes / sum-objs.js
Created February 22, 2017 22:22
Function that sums objects of the same shape
// objs[] *must* be the same shape
// returns new object with summation of all properties
function sumObjs(objs) {
const keys = Object.keys(objs[0]);
// initialize return object with 0s
const ret = keys.reduce((aggr, k) => {
aggr[k] = 0;
return aggr;
}, {});
// sum each property
const { Observable } = require('rxjs/Observable');
require('rxjs/add/operator/filter');
require('rxjs/add/operator/switchMap');
require('rxjs/add/operator/take');
require('rxjs/add/operator/toPromise');
const axios = require('axios');
const online$ = createOnline$();
//only make the network request when we're online
@ccnokes
ccnokes / vanilla-online-offline.js
Last active September 22, 2020 02:35
Online/offline emitter in vanilla JS
function createOnlineEmitter() {
let cbs = []; //array of registered callbacks for the event
let unsub; //function for removing the main event listener
//this is the main event listener that gets registered with window.online/offline event
const mainListener = (isOnline) => {
//call all the subscribed callbacks
cbs.forEach(cb => cb(isOnline));
};