Skip to content

Instantly share code, notes, and snippets.

View mmckegg's full-sized avatar

Matt McKegg mmckegg

  • Wellington, New Zealand
View GitHub Profile
@mmckegg
mmckegg / find-trailing-commas.regexp
Created May 30, 2013 03:05
Regexp to find trailing commas in javascript files that throw errors in IE and destroy everything!
,\s*\n+(\s*\/\/.*\n)*\s*[\}\)\]]
@mmckegg
mmckegg / forEachAsync.js
Created October 13, 2013 01:57
Async forEach
function forEach(array, fn, cb){
var i = -1
function next(err){
if (err) return cb&&cb(err)
i += 1
if (i<array.length){
fn(array[i], next, i)
} else {
cb&&cb(null)
}
@mmckegg
mmckegg / waterflow.js
Created October 13, 2013 02:00
Flow control for object building - passes object as first param.
function flow(target, items, cb){
forEach(items, function(item, next){
if (Array.isArray(item)){
item[0].apply(this, [target].concat(item.slice(1), cb))
} else {
item(target, next)
}
}, function(err){
if (err) return cb&&cb(err)
cb(null, target)
function mergeClone(){
var result = {}
for (var i=0;i<arguments.length;i++){
var obj = arguments[i]
if (obj){
Object.keys(obj).forEach(function(key){
result[key] = obj[key]
})
}
}
function padNumber(number, pad) {
var N = Math.pow(10, pad);
return number < N ? ("" + (N + number)).slice(1) : "" + number
}
function walkDom(rootNode, iterator){
var currentNode = rootNode.firstChild
while (currentNode){
iterator(currentNode)
if (currentNode.firstChild){
currentNode = currentNode.firstChild
} else {
while (currentNode && !currentNode.nextSibling){
if (currentNode !== rootNode) {
currentNode = currentNode.parentNode
function rgb(hex){
if (hex.charAt(0) == '#'){
var color = hex.slice(1)
var splitter = color.length/3
var value = []
for (var i=0;i<3;i++){
var part = parseInt(color.slice(i*splitter, (i+1)*splitter), 16)
if (splitter == 1){
value[i] = ((part+1)*16)-1
} else {
@mmckegg
mmckegg / bouce.sh
Created November 15, 2013 02:45
bounce local port off ssh server
ssh -R *:1234:localhost:9966 user@server.com
# http://server.com:1234
@mmckegg
mmckegg / raf.js
Created December 9, 2013 00:27
cross browser requestAnimationFrame that works like setInterval (returns a function that will stop loop if called)
module.exports = animate
var requestAnimationFrame =
global.requestAnimationFrame ||
global.webkitRequestAnimationFrame ||
global.mozRequestAnimationFrame ||
global.msRequestAnimationFrame ||
global.oRequestAnimationFrame ||
function(fn, el) {
setTimeout(fn, 1000/60)
@mmckegg
mmckegg / gist:44c31fa6c51ed15b06ad
Created August 2, 2014 03:50
observable array
var mutators = [
'fill', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'
]
function ObservableArray(onChange){
var array = []
array.set = set
array.onchange = onChange