Skip to content

Instantly share code, notes, and snippets.

View dschnare's full-sized avatar

Darren Schnare dschnare

View GitHub Profile
@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 / 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 / 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 / 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 / BusinessObject.js
Last active November 18, 2019 04:01
Object hierarchy inspired by Smalltalk and OOD
/**
* Business Object class object that acts as the root of an object hierarchy.
* Creates object instances that have methods marked as read-only and the object
* sealed, meaning no new properties can be added and behaviour cannot be changed
* without extending the class object (i.e. tamperproof).
*
* @example
* const Box = BObject.subClass({
* className: 'Box',
* new ($base, width, height) {
@dschnare
dschnare / collate.js
Last active October 7, 2017 13:51
groupBy and collate
/**
* Collates all properties with varying values or the specified properties in
* the property map into a single object.
*
* If the propMap is an Object then the keys are the properties to collate and
* the values are the target property name to created on the collated object. If
* the key in the propMap is a comma delimited string then the target property
* will be set to an object with all the properties in the comma delimited
* key.
*
@dschnare
dschnare / bs-carousel-nesting.js
Created March 27, 2015 17:42
Adds nesting support to Twitter Bootstrap carousels.
@dschnare
dschnare / index.html
Last active December 23, 2015 04:59
Applies several stretch strategies to a viewport's contents following the examples here: http://msdn.microsoft.com/en-us/library/system.windows.media.stretch.aspx. This snippet requires jQuery.
<!DOCTYPE html>
<html>
<head>
<title>TITLE</title>
<style type="text/css">
html,
body {
margin: 0;
}
.viewport {
@dschnare
dschnare / cachetext.correct.js
Last active January 18, 2020 03:12
HTML5 Canvas Tricks: Text Caching and Registration Point
// Create our stage canvas.
var stage = document.createElement('canvas');
stage.style.border = '1px solid #000';
document.body.appendChild(stage);
// Create offscreen buffer for our text rendering.
// This way all we have to do is draw our buffer to
// the main canvas rather than drawing text each frame.
var textBuffer = document.createElement('canvas');
@dschnare
dschnare / validate.js
Last active December 21, 2015 16:59
jQuery Validate File Fields Quirkiness - Example of how to manually update the valid state of a file field when using jQuery Validate.
// jQuery Validate setup.
$('form').validate({
rules: {
"cover-letter": {
required: true,
accept: "application/pdf|application/msword|application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}
},
messages: {
"cover-letter": "Please upload your cover letter (PDF, DOC or DOCX)."