Skip to content

Instantly share code, notes, and snippets.

View bendman's full-sized avatar
🏠
Working from home

Ben Duncan bendman

🏠
Working from home
View GitHub Profile
@bendman
bendman / amd-kit.js
Created September 21, 2012 05:09
Simple AMD Loader
/**
* Array.indexOf Polyfill (courtesy of MDN)
*/
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement /*, fromIndex */ ) {
if (this === null) {
throw new TypeError();
}
var t = new Object(this);
var len = t.length >>> 0;
@bendman
bendman / .tmux.conf
Created October 3, 2020 17:30
My tmux config
# reload configs
bind r source-file ~/.tmux.conf
# increase the scroll buffer size
set -g history-limit 10000
# add the prefix key Ctrl+J
set-option -g prefix C-j
set-option -g prefix2 C-b
@bendman
bendman / nodeExternalIp.js
Created March 25, 2014 10:46
In Node.js, get the external IP address of the host machine.
function getExternalIp() {
var ifconfig = require('os').networkInterfaces();
var device, i, I, protocol;
for (device in ifconfig) {
// ignore network loopback interface
if (device.indexOf('lo') !== -1 || !ifconfig.hasOwnProperty(device)) {
continue;
}
for (i=0, I=ifconfig[device].length; i<I; i++) {
@bendman
bendman / api-example.js
Last active February 6, 2017 19:46
Proxy `fetch` in a way that automatically handles file uploads and/or JSON parsing/serializing
import fetch from './fetch-proxy.js';
export async function saveImage(imageURI) {
return await fetch('user/picture', {
method: 'PUT',
body: { photoContent: imageURI },
fileKeys: ['photoContent'],
});
}
@bendman
bendman / createAction.js
Created February 2, 2017 18:22
Abstract creating an action that has a type and a payload
// Abstract creating an action that has a type and a payload:
// const addTodo = createAction(ADD_TODO);
// is equivalent to
// const addTodo = (payload) => ({ type: ADD_TODO, payload });
//
// Optionally you can pass in a payloadMap when defining an action
// to reshape the payload before storing it on the action.
import { identity } from 'lodash';
@bendman
bendman / iterate-promises.js
Created June 30, 2016 20:48
Using generators and promises for more understandable async pipelines.
// This demo uses Babel for es2015 compilation and Promises, and superagent for requests.
// An example async pipeline, using a demo API
document.getElementById('run').addEventListener('click', (evt) => {
iteratePromises(function* () {
log('Requesting list of posts');
let postsResponse = yield superagent.get(
'http://jsonplaceholder.typicode.com/posts/');
log('Requesting author of first post');
@bendman
bendman / bbp.conf
Last active December 20, 2015 13:09
Nginx Config for Boston Bike Party App
server {
listen *:80;
server_name api.bostonbikeparty.com;
error_log /Users/bendman/Documents/Development/bbp/logs/error.log info;
access_log /Users/bendman/Documents/Development/bbp/logs/access.log main;
location / {
proxy_pass http://bostonbikeparty.com:3000;
proxy_set_header Host $http_host;
@bendman
bendman / symlinkFixer.js
Created December 10, 2015 03:26
Fix broken symlink binaries to be relative symlinks
// Find symlinks that don't work and fix them.
//
// This is done by:
// 1. Getting a list of all file paths in a directory
// 2. Filtering for broken symlinks by reading binary content as unicode
// 5. Determining relative path between source and target
// 6. Deleting old link file
// 7. Creating new relative link file
var fs = require('fs');
@bendman
bendman / inherit.js
Created August 21, 2012 17:46
Inheritance Test
var Builder = function(parentObj, childObj) {
var F = function() {};
F.prototype = parentObj;
return $.extend(new F(), childObj);
};
var parent = {
Inherit: 'parent inherit',
Both: 'parent value'
};