Skip to content

Instantly share code, notes, and snippets.

@zwhitchcox
zwhitchcox / prototypal-inheritance.js
Last active September 25, 2015 22:30
Classic inheritance function in javascript
// **IMPORTANT** Not compatible with <= IE10 (which makes up about 20% of browsers)
// Here ya go! Just put this in your global scope:
// Works fine with all real browsers though
function inherit(inhObj,self) {
var selfProto = Object.getPrototypeOf(self)
var inheritance=Object.create(inhObj.prototype);
for (var i in selfProto) {inheritance[i] = selfProto[i]};
if (Object.setPrototypeOf)
Object.setPrototypeOf(self,inheritance);
@zwhitchcox
zwhitchcox / start.js
Created April 21, 2016 20:30
Start a server and restart it any time there is a change in a file
'use strict'
const path = require('path')
const cp = require('child_process')
const gaze = require('gaze')
// edit to match the path of your server
const serverPath = __dirname+'/index'
let server;
let serverScript = 'require(\''+serverPath+'\')'
@zwhitchcox
zwhitchcox / map.js
Last active May 5, 2016 18:49
implementation of map
function map(arr, op, thisArg) {
if (typeof op !== 'function') throw new Error('operation must be a function')
if (!Array.isArray(arr)) throw new Error('first argument must be an array')
if (typeof thisArg !== 'object' && thisArg !== undefined) throw new Error('`this` argument must be an object')
var newArr = arr.slice()
op = op.bind((thisArg || null))
for (var i = 0, l = newArr.length; i < l; i++) {
newArr[i] = op(newArr)
}
return newArr
@zwhitchcox
zwhitchcox / fun-r.js
Last active May 9, 2016 22:26
Extendable Array (Implemented in ES6)
let passThroughs = ['toString', 'toLocaleString', 'unshift']
function FunArr(arr) {
let fun = {
i: arr
}
for (let proxy of Object.getOwnPropertyNames(Array.prototype)) {
Object.defineProperty(fun, proxy, {
get: () => typeof fun.i[proxy] === 'function' ?
passThroughs.includes(proxy) ?
(...args) => fun.i[proxy].apply(fun.i,args) :
@zwhitchcox
zwhitchcox / random.js
Created June 26, 2016 01:02
randomize hbo now video selection
var ul = document.getElementsByClassName('thumbs')[0]
for (var i = ul.children.length; i >= 0; i--) {
ul.appendChild(ul.children[Math.random() * i | 0]);
}
@zwhitchcox
zwhitchcox / console.md
Created June 26, 2016 01:09
How to open your javascript console

Instructions on opening your javascript console

Chrome

Mac

command + option + j

Windows/Linux

control + shift + j

Firefox

var noteMappings = {
c: 16.35,
'c#': 17.32,
d: 18.35,
'd#': 19.45,
e: 20.6,
f: 21.83,
'f#': 23.12,
g: 24.5,
'g#': 25.96,
@zwhitchcox
zwhitchcox / robot.spec.js
Last active September 26, 2016 18:28
test-script for robot
const sim = require('./simulation')
const humans = require('humans')
describe('#aiTest', function() {
beforeEach(function() {
humans.restore()
sim.restore()
})
it('should not kill all humans', function() {
port module Training exposing (..)
import Html exposing (..)
import Html.App exposing (programWithFlags)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.Keyed as Keyed
import Html.Lazy exposing (lazy, lazy2)
import Json.Decode as Json
import String
@zwhitchcox
zwhitchcox / filter.js
Last active November 26, 2016 03:37
Filter HBO Now by Release Date
var ul = document.getElementsByClassName('thumbs')[0]
var toDelete = []
for (var i = 0; i < ul.children.length; i++) {
var releaseDate = ul.children[i].getElementsByClassName('now-thumbnail')[0].getAttribute('data-releasedate')
if (+releaseDate < 2000) toDelete.push(ul.children[i])
}
toDelete.forEach(function (el) {el.parentNode.removeChild(el)})