Skip to content

Instantly share code, notes, and snippets.

Avatar

Edan Schwartz eschwartz

View GitHub Profile
@eschwartz
eschwartz / server-side-calculator-regex.js
Created Dec 22, 2021
Regex Solution to Server Side Calculator
View server-side-calculator-regex.js
// Track the equation as a string
let equation = '';
// 🤯 🤯 🤯 🤯 🤯 🤯 🤯
// This regular expression defines a set of "rules" for
// how our equation string should look
// It also tells us how to break apart the different sections of the
// string into "groups"
// These are super powerful, though not always so easy to use 😉
let equationRegEx = /^([0-9]+(\.[0-9]+)?)?([\+\-\/\*])?([0-9]+(\.[0-9]+)?)?$/
@eschwartz
eschwartz / assert-matches.ts
Created Dec 20, 2017
Assert that a string matches a regular expression
View assert-matches.ts
import * as assert from 'assert';
function assertMatches(actual:string, regex:RegExp, msg?:string) {
if (regex.test(actual)) {
assert(false, `${msg}: expected "${actual}" to match regex "${regex.toString()}"`)
}
}
export default assertMatches;
@eschwartz
eschwartz / mapValuesAsync.ts
Created Jul 10, 2017
Like _.mapValues, but asynchronous
View mapValuesAsync.ts
// 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) => {
View mapAsync.ts
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 / flattenObject.js
Last active Mar 23, 2017
Flatten an object
View flattenObject.js
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}.`)) :
View assertMatch.ts
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 Jan 20, 2017
Pipe a read-stream to a write-stream (promisifed)
View pipe.ts
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 / filter-async.js
Last active Nov 17, 2016
Filter array, using asyncronous logic
View filter-async.js
/**
* @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 Nov 16, 2016
Generate a Kubernetes ReplicationController
View create-k8s-config.js
// 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() {
@eschwartz
eschwartz / ReplicationController.js
Last active Nov 16, 2016
Node.js ReplicationController model
View ReplicationController.js
const _ = require('lodash');
/**
* @param {{
* name: string,
* version: string,
* image: string,
* labels?: Object,
* replicas?: int,
* imagePullPolicy?: 'Always' | 'Never' | 'IfNotPresent',