Skip to content

Instantly share code, notes, and snippets.

View dschnare's full-sized avatar

Darren Schnare dschnare

View GitHub Profile
@dschnare
dschnare / env.js
Last active January 24, 2018 20:22
Simple environment variable file loader
const fs = require('fs')
const { promisify } = require('util')
const readFile = promisify(fs.readFile)
/**
* Parses text for environment variables and saves them to an object as keys.
*
* Empty lines and lines starting with `#` are skipped.
*
@dschnare
dschnare / spy.js
Created January 23, 2018 18:52
A simple test spy factory function
function spy (fn = () => {}) {
f.calls = []
function f (...args) {
const returnValue = fn(...args)
f.calls.push({ args, returnValue })
return returnValue
}
return f
}
@dschnare
dschnare / walk.js
Created February 22, 2019 21:55
Object graph walker
// Walk an object graph, calling the visit function with
// value, key, and object at each level.
function walk (o, visit) {
visit(o, '', o)
if (o && (Array.isArray(o) || Object(o) === o)) {
let stack = [ o ]
while (stack.length) {
let obj = stack.shift()
for (let k in obj) {
const v = obj[k]
@dschnare
dschnare / interview.md
Last active June 18, 2019 15:23
Interview questions

Junior Server Developer:

Write a Nodejs+Express web server that responds with { message: 'Hello World!' } when it receives a request at GET /hello.

Junior React Developer:

Write a React component that abstracts embedding YouTube and Vimeo videos. The component should accept the prop src that is a string equal to the source of the video to load. The component will determine what video it is based on the domain of the source and then decide how to embed the video.

Intermediate Server Developer:

@dschnare
dschnare / lists.js
Last active May 30, 2019 03:30
Sorted and priority list factories
/**
* @param {any[]} items
* @param {any} item
* @param {{ (a: any, b: any) => number }} compare
* @param {number} low
* @param {number} high
* @return {number}
*/
function binarySearch (items, item, compare, low = 0, high = items.length) {
if (high <= low) {
@dschnare
dschnare / read-option.js
Last active April 2, 2020 21:27
Read a command line option
/**
* Read a command line option. Option names must start with '-'.
*
* Options:
*
* `required` Determines if the option must appear on the command line.
*
* `multi` Determines if the option can be specified more than once on the
* command line. Causes the return value to be an array.
*
@dschnare
dschnare / json-pointer.js
Last active September 13, 2019 18:43
JSONPointer and walkObject APIs
const walkObject = (function () {
function walkObjectRec (obj, visit, { pointer = '', key = '', visited = new WeakMap(), parent = undefined } = {}) {
if (obj === null || obj === undefined) {
visit(obj, key, pointer, parent)
} else if (typeof obj === 'object') {
if (visited.has(obj)) {
visited.set(obj, visited.get(obj) + 1)
} else {
visited.set(obj, 1)
visit(obj, key, pointer, parent)
@dschnare
dschnare / service-container.js
Last active September 21, 2019 00:45
Simple service container
/**
* A simple service container.
*
* @example
* const app = express()
*
* const appContainer = new ServiceContainer()
* const config = Object.freeze({ })
* appContainer.constant('Config', config)
* appContainer.singleton('Db', () => new Db())
@dschnare
dschnare / template.js
Last active April 5, 2021 15:56
Simple EcmaScript template string-based templating system
/*
* Simple EcmaScript templating system.
*
* Templates are just functions that accept an object of
* properties and return a string.
*
* const Hello = (props = {}) => `
* Hello ${props.message || 'World'}!
* `
* console.log(Hello({ message: 'Mom' }))
@dschnare
dschnare / t.js
Created September 2, 2019 20:43
Type checking at runtime
/**
* Checks the type of a value. If the value is invalid then throws.
*
* Errors:
*
* TypeError
*
* Thrown whenever a value has an invalid type
*
* Properties: