Skip to content

Instantly share code, notes, and snippets.

@bjdixon
bjdixon / memo.js
Last active August 29, 2015 14:26
Memoize a function
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 July 30, 2015 16:34
A Model with undo and redo stacks
const copyObject = (source) => Object.assign({}, source);
const Stack = () => {
const array = [];
return {
push (value) {
array.push(value);
},
pop () {
@bjdixon
bjdixon / queue.js
Created July 30, 2015 17:39
Queue in javascript
const Queue = () => {
let array = [];
return {
push: (value) => array.push(value),
pull: () => array.shift(),
isEmpty: () => !array.length
};
};
@bjdixon
bjdixon / flatten.js
Created August 21, 2015 15:26
flatten an array es5
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 September 8, 2015 20:47
take an array and return an array with falsy values removed
function compact(a) {
return a.reduce(function(memo, x) {
return x ? memo.concat(x) : memo;
}, []);
}
@bjdixon
bjdixon / partial.js
Last active September 14, 2015 20:00
partial and findIndex
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 September 10, 2015 20:57
type checking for bool, string and number
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 September 10, 2015 18:15
takes a function and returns a function that will only be applied if it has arguments that aren't null or undefined
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 October 2, 2015 00:50
Get querystring key value pairs
// 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 October 13, 2015 15:47
returns first key that passes predicate
function findKey(obj, predicate) {
for (var prop in obj) {
if(obj.hasOwnProperty( prop ) ) {
if (predicate(obj[prop])) {
return prop;
}
}
}
}