Skip to content

Instantly share code, notes, and snippets.

View eschwartz's full-sized avatar

Edan Schwartz eschwartz

View GitHub Profile
@eschwartz
eschwartz / mapValuesAsync.ts
Created July 10, 2017 20:17
Like _.mapValues, but asynchronous
// https://gist.github.com/eschwartz/70a15f12e6ef90f377d5d51fba9c86d8
import mapAsync from './mapAsync';
interface Dict<TVal> {
[key: string]: TVal
}
async function mapValuesAsync<TVal, TRes>(obj:Dict<TVal>, iter:(val:TVal, key:string) => Promise<TRes>):Promise<Dict<TRes>> {
const keyResPairs:[string, TRes][] = await mapAsync<string, [string, TRes]>(Object.keys(obj),
async (key:string) => {
@eschwartz
eschwartz / mapAsync.ts
Created July 10, 2017 20:17
Async map
import * as _ from 'lodash';
async function mapAsync<TVal, TRes>(items:TVal[], iter:(val:TVal, i:(number | string)) => Promise<TRes>):Promise<TRes[]> {
return Promise.all(_.map(items, iter));
}
export default mapAsync;
@eschwartz
eschwartz / exec-promise.js
Last active July 9, 2017 09:15
exec-promise: Promise wrapper around child_process.exec
const childProcess = require('child_process');
// otherwise we get a stupid warning.
process.stdout.setMaxListeners(100);
process.stderr.setMaxListeners(100);
/**
* Promisified child_process.exec
*
* @param cmd
@eschwartz
eschwartz / flattenObject.js
Last active March 23, 2017 15:57
Flatten an object
const _ = require('lodash');
function flattenObj(obj, prefix) {
prefix || (prefix = '');
return Object.keys(obj)
.reduce((flat, key) => (
_.isObject(obj[key]) ?
// Recursively flatten objects
Object.assign(flat, flattenObj(obj[key], `${prefix}${key}.`)) :
function assertMatch(actual:string, regex: string | RegExp, msg?:string):void {
const regexNorml:RegExp = _.isString(regex) ? new RegExp(regex as string) : regex;
const isMatch = regexNorml.test(actual);
if (!isMatch) {
assert.fail(actual, regexNorml.toString(), msg, 'matches');
}
}
@eschwartz
eschwartz / pipe.ts
Created January 20, 2017 21:35
Pipe a read-stream to a write-stream (promisifed)
import * as stream from 'stream';
function pipe(readStream:stream.Readable, writeStream: NodeJS.WritableStream):Promise<void> {
return new Promise<void>((onRes, onErr) => {
readStream
.on('error', onErr)
.pipe(writeStream)
.on('error', onErr)
.on('finish', () => onRes())
});
@eschwartz
eschwartz / poll.js
Last active January 9, 2017 17:22
Poll
/**
* eg.
*
* poll(
* () => Promise.resolve(Math.random()),
* val => val > 0.5
* )
* .then(val => console.log(`You won, with ${val}!`));
*
* @param {():Promise<T>} run
@eschwartz
eschwartz / deferred.js
Last active November 21, 2016 14:23
Deferred
function Deferred() {
var deferred = {
resolve: null,
reject: null
};
deferred.promise = new Promise((resolve, reject) => {
// Save resolve/reject to deferred, for later usage.
deferred.resolve = resolve;
deferred.reject = reject;
});
@eschwartz
eschwartz / filter-async.js
Last active November 17, 2016 22:30
Filter array, using asyncronous logic
/**
* @param {Array<T>} list
* @param {(T):Promise<Boolean>} filter
* @returns {Promise<T[]>}
*/
function filterAsync(list, filter) {
return Promise
.all(
list.map(item => filter(item)
.then(filterRes => ({ filterRes, item }))
@eschwartz
eschwartz / create-k8s-config.js
Last active November 16, 2016 17:41
Generate a Kubernetes ReplicationController
// See https://gist.github.com/eschwartz/4f16fcb963954d433753ffe32ce5d5ca
const ReplicationController = require('./ReplicationController');
const fs = require('fs-extra');
// See https://gist.github.com/eschwartz/310a7d9938b60eaf49c8d56c1ccfb759
const loadS3Env = require('./loadS3Env');
const path = require('path');
const Cli = require('admiral-cli');
const co = require('co');
function main() {