Skip to content

Instantly share code, notes, and snippets.

View bradoyler's full-sized avatar
👋
say hi

brad oyler bradoyler

👋
say hi
View GitHub Profile
@bradoyler
bradoyler / middleware.js
Created August 2, 2014 15:51
express middleware module template (NodeJs)
module.exports.doSomething = function() {
return function(req, res, next) {
console.log('do something...');
//next();
}
};
@bradoyler
bradoyler / parentheses_check.js
Last active August 29, 2015 14:05
check for balanced parens
function parenthesesAreBalanced(s)
{
var parentheses = "[]{}()",
stack = [], //Parentheses stack
i, //Index in the string
c; //Character in the string
for (i = 0; c = s[i++];)
{
var bracePosition = parentheses.indexOf(c),
@bradoyler
bradoyler / pdkplayer.html
Last active August 29, 2015 14:06
minimal PDK player example // source http://jsbin.com/miwaca/7
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="minimal PDK player example" />
<meta name="tp:EnableExternalController" content="true" />
<meta charset="utf-8">
<title>minimal PDK player</title>
</head>
<body>
@bradoyler
bradoyler / async.each.js
Last active August 29, 2015 14:06
using async.parallel vs async.each
async = require("async");
var results = [];
async.each(states,
function(state, callback){
var url = baseUrl + state.name;
request(url, function(err, res, body){
results.push(data);
callback();
});
@bradoyler
bradoyler / adaptive-imgs.html
Last active August 29, 2015 14:08
Adaptive image <img> technique for above the fold images, using minimal inline js - DEMO http://jsbin.com/meroti/13 (works in IE8)
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="adaptive img solution for above the fold images - using inline js" />
<meta charset="utf-8">
<title>Adaptive Img - JS Bin</title>
</head>
<body>
<img class="js-adaptive-img" src='//:0'
data-bp0='http://media2.s-nbcnews.com/j/newscms/2014_41/708631/141009-liberia-ebola-jsw-1158a_b58372304e3003a1bed177b8d90e126d.nbcnews-fp-320-320.jpg'
@bradoyler
bradoyler / flatten.js
Last active August 29, 2015 14:13
flatten a list / array in js
//input: [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
function flatten(list) {
return list.reduce(function (acc, val) {
return acc.concat(val.constructor === Array ? flatten(val) : val);
}, []);
}
// output: [1, 2, 3, 4, 5, 6, 7, 8]
@bradoyler
bradoyler / reverseString.js
Created January 23, 2015 00:55
reverse string, like a paragraph
/* input
---------- Ice and Fire ------------
fire, in end will world the say Some
ice. in say Some
desire of tasted I've what From
fire. favor who those with hold I
... elided paragraph last ...
Frost Robert -----------------------
@bradoyler
bradoyler / repeat.js
Created January 23, 2015 01:14
repeat a string
String.prototype.repeat = function(n) {
return new Array(1 + (n || 0)).join(this);
}
console.log("ha".repeat(5)); // hahahahaha
// this is built-in to ES6
@bradoyler
bradoyler / iframe_track.js
Created April 3, 2015 02:55
iframe click track (pdk)
// quick hack to track iframe clicks (for pdkPlayer)
$('iframe').each(function (i,el) {
var iframe = el;
function addIframeClickEvent() {
//console.log('### found iframe', iframe.id);
$(iframe).contents().find('body').on('click', function (event) {
console.log('### click iframe', new Date().getMinutes()+'m', new Date().getSeconds()+'s');
});
}
@bradoyler
bradoyler / delayedIterator.js
Last active August 29, 2015 14:25
handy for worker process stuff when you want to throttle the frequency of requests.
function delayedIterator(array, interval, callback, done) {
var index = 0;
var iterator = function iterator() {
setTimeout(function () {
if(array[index]) {
callback(array[index], index);
index++;
iterator();
}
else {