Skip to content

Instantly share code, notes, and snippets.

View eschwartz's full-sized avatar

Edan Schwartz eschwartz

View GitHub Profile
@eschwartz
eschwartz / Dockerfile
Last active November 2, 2023 15:11
npm install from private repo, in docker build
# Add these lines to your dockerfile, before `npm install`
# Copy the bitbucket private key to your docker image
COPY ./bitbucket_ssh_key /opt/my-app
# Copy the ssh script to your docker image
COPY ./ssh-bitbucket.sh /opt/my-app
# Tell git to use your `ssh-bitbucket.sh` script
ENV GIT_SSH="/opt/map-project-tile-server/ssh-bitbucket.sh"
@eschwartz
eschwartz / server-side-calculator-regex.js
Created December 22, 2021 21:42
Regex Solution to Server Side Calculator
// 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]+)?)?$/
function intent({DOM}) {
return {
addTodo: DOM.get('input', 'change').
map(evt => evt.target.value).
filter(val => val.trim().length),
removeTodo: DOM.get('button', 'click').
// Map the remove button click to the item text
map(evt => evt.target.previousElementSibling.innerText.trim())
};
}
@eschwartz
eschwartz / assert-file-exists.ts
Last active December 21, 2017 20:08
Assert a file does not exist (node)
import * as fs from 'fs';
import * as assert from 'assert';
import * as _ from 'lodash';
function assertFileExists(filePath:string, msg?:string) {
try {
fs.statSync(filePath);
}
catch (err) {
if (_.includes(['ENOENT', 'ENOTDIR'], err.code)) {
@eschwartz
eschwartz / assert-matches.ts
Created December 20, 2017 21:54
Assert that a string matches a regular expression
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 / assert-file-exists.js
Last active December 18, 2017 18:30
Assert that a file exists
const fs = require('fs');
const assert = require('assert');
const _ = require('lodash');
function fileExists(filePath, msg) {
try {
fs.statSync(filePath);
}
catch (err) {
if (_.includes(['ENOENT', 'ENOTDIR'], err.code)) {
@eschwartz
eschwartz / tmp.js
Last active December 13, 2017 21:23
Promise wrapper for node-tmp
const co = require('co');
const _tmp = require('tmp');
_tmp.setGracefulCleanup();
// Promise wrappers around tmp
const tmp = {
/** @return {Promise<{path, cleanup}>} */
file: () => co(function* () {
return yield cb => _tmp.file({ unsafeCleanup: true }, (err, path, fd, cleanup) => cb(err, {
path,
@eschwartz
eschwartz / assert-partial-no-lodash.ts
Last active December 1, 2017 23:04
assert-partial.js
import * as assert from 'assert';
function assertPartial(actualObj:any, expectedPartial: any, msg?:string) {
const actualPartial:any = Object.keys(expectedPartial)
.reduce((part, key) => ({
...part,
[key]: actualPartial[key]
}), {});
assert.deepStrictEqual(actualPartial, expectedPartial, msg);
@eschwartz
eschwartz / loadEnv.node8.js
Last active November 14, 2017 17:57
Load env vars from S3
const S3 = require('aws-sdk').S3;
const dotenv = require('dotenv');
const Url = require('url');
const p = require('util').promisify;
const fs = require('fs');
/**
* @param {string} envUri
* @returns {Promise<Object>}
*/
@eschwartz
eschwartz / promise-sequence.js
Last active September 7, 2017 19:57
Run a set of async fns in sequence
/**
* Run a series of async functions in sequence,
* so that each fn waits to run until the last one has completed.
*
* @param {function():Promise<T>} fns
* @returns {Promise<T[]>
*/
function sequence(fns) {
return fns
.reduce((_prevResults, fn) => (