Skip to content

Instantly share code, notes, and snippets.

View ccnokes's full-sized avatar

Cameron Nokes ccnokes

View GitHub Profile
@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 / 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 / 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 / custom-cache-ng1.js
Last active August 21, 2017 03:11
Use a custom cache class in angularJS $http
// This class implements the same interface that the cache created by $cacheFactory does
// See https://github.com/angular/angular.js/blob/master/src/ng/cacheFactory.js#L142
// This cache evicts an entry once it's expired (which we define as 5 seconds).
class ExpirationCache {
constructor(timeout = 5000) {
this.store = new Map();
this.timeout = timeout;
}
get(key) {
@ccnokes
ccnokes / retry-example.js
Created August 11, 2017 20:36
Rx.Observable.retry example
function randomErrorObs() {
return Rx.Observable.create(obs => {
let n = 0;
if(Math.random() < 0.5) {
obs.next(++n);
} else {
console.warn('producing error');
obs.error('bummer');
}
});
@ccnokes
ccnokes / SimpleStore.ts
Created August 16, 2017 16:08
A simple store thing
import { EventEmitter } from 'events';
const enum Actions {
get,
set,
delete
}
export class Store extends EventEmitter {
private data = new Map<string, any>();
@ccnokes
ccnokes / basic-$http-cache.js
Created August 21, 2017 02:51
AngularJS $http cache
// this will cache the response indefinitely in a cache created via $cacheFactory
// that cache is shared globally among all $http requests
$http.get('http://pokeapi.co/api/v2/pokemon/1/', {
cache: true
});
@ccnokes
ccnokes / basic-$http-lru-cache.js
Last active August 21, 2017 03:26
angularJS LRU cache
// create an LRU (least recently used) cache with up to 10 entries
const lru = $cacheFactory('my-cache', { capacity: 10 });
$http.get('http://pokeapi.co/api/v2/pokemon/1/', {
cache: lru
});
@ccnokes
ccnokes / url-search-params-utils.js
Last active June 23, 2020 23:31
Utils for URLSearchParams
function objToParams(obj) {
const params = new URLSearchParams();
for(let key in obj) {
if(Array.isArray(obj[key])) {
obj[key].forEach(item => params.append(key, item));
}
else if(typeof obj[key] === 'object') {
continue;
}
else {
@ccnokes
ccnokes / format-url.js
Last active June 17, 2018 03:42
`formatUrl` function that formats query params and such
import * as _ from 'lodash';
let n = 2;
let someString;
// NOTE `_.pickBy(params, _.negate(_.isNil))` removes undefined/null entries
// so you don't have to worry about `undefined` getting coerced to a string
const formatUrl = (urlStr, params) => urlStr + '?' +
new URLSearchParams(_.pickBy(params, _.negate(_.isNil))).toString();