Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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);