Skip to content

Instantly share code, notes, and snippets.

Brian Dixon bjdixon

Block or report user

Report or block bjdixon

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bjdixon
bjdixon / memo.js
Last active Aug 29, 2015
Memoize a function
View memo.js
memo = function (fn) {
var cache = {};
return function () {
var key = JSON.stringify(this, arguments);
return cache[key] || (cache[key] = fn.apply(this, arguments));
}
}
@bjdixon
bjdixon / model.js
Created Jul 30, 2015
A Model with undo and redo stacks
View model.js
const copyObject = (source) => Object.assign({}, source);
const Stack = () => {
const array = [];
return {
push (value) {
array.push(value);
},
pop () {
@bjdixon
bjdixon / queue.js
Created Jul 30, 2015
Queue in javascript
View queue.js
const Queue = () => {
let array = [];
return {
push: (value) => array.push(value),
pull: () => array.shift(),
isEmpty: () => !array.length
};
};
@bjdixon
bjdixon / flatten.js
Created Aug 21, 2015
flatten an array es5
View flatten.js
function flatten(a) {
return a.map(function(val) {
return (Array.isArray(val)) ? flatten(val) : val;
});
}
//example usage
var arr = [1, 2, [3, 4], 5, [6, [7, [8]], 9], 10];
var flat = flatten(arr); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
@bjdixon
bjdixon / compact.js
Created Sep 8, 2015
take an array and return an array with falsy values removed
View compact.js
function compact(a) {
return a.reduce(function(memo, x) {
return x ? memo.concat(x) : memo;
}, []);
}
@bjdixon
bjdixon / partial.js
Last active Sep 14, 2015
partial and findIndex
View partial.js
function partial (fn) {
var initialArgs = Array.prototype.slice.call(arguments, 1);
return function () {
var remainingArgs = Array.prototype.slice.call(arguments);
return fn.apply({}, initialArgs.concat(remainingArgs));
}
}
function findIndex(arr, predicate) {
var i;
@bjdixon
bjdixon / isType.js
Last active Sep 10, 2015
type checking for bool, string and number
View isType.js
var isBoolean = is('Boolean'),
isNumber = is('Number'),
isString = is('String'),
isObject = is('Object'),
isArray = is('Array');
function is(type) {
var fastTypes = ['undefined', 'boolean', 'string', 'number', 'function', 'symbol'];
if (fastTypes.indexOf(type.toLowerCase()) >= 0) {
type = type.toLowerCase();
@bjdixon
bjdixon / maybe.js
Created Sep 10, 2015
takes a function and returns a function that will only be applied if it has arguments that aren't null or undefined
View maybe.js
function maybe(fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
if (!args.length) {
return;
}
for (var index = 0, length = args.length; index < length; index += 1) {
if (args[index] == null) {
return;
}
@bjdixon
bjdixon / getParams.js
Created Oct 2, 2015
Get querystring key value pairs
View getParams.js
// get the querystring key value pairs
// eg. http:www.example.com/document?a=1&b="2"&c=JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B
// will return {a: "1", b: "2", c: "JavaScript_шеллы"}
var params = (function getURLParams(qs) {
var pairs = {};
qs.forEach(function(param) {
var pair = param.split('=');
pairs[pair[0]] = decodeURIComponent(pair[1].replace(/\"/g, ''));
});
@bjdixon
bjdixon / findKey.js
Created Oct 13, 2015
returns first key that passes predicate
View findKey.js
function findKey(obj, predicate) {
for (var prop in obj) {
if(obj.hasOwnProperty( prop ) ) {
if (predicate(obj[prop])) {
return prop;
}
}
}
}
You can’t perform that action at this time.