This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
memo = function (fn) { | |
var cache = {}; | |
return function () { | |
var key = JSON.stringify(this, arguments); | |
return cache[key] || (cache[key] = fn.apply(this, arguments)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const copyObject = (source) => Object.assign({}, source); | |
const Stack = () => { | |
const array = []; | |
return { | |
push (value) { | |
array.push(value); | |
}, | |
pop () { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Queue = () => { | |
let array = []; | |
return { | |
push: (value) => array.push(value), | |
pull: () => array.shift(), | |
isEmpty: () => !array.length | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compact(a) { | |
return a.reduce(function(memo, x) { | |
return x ? memo.concat(x) : memo; | |
}, []); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, '')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findKey(obj, predicate) { | |
for (var prop in obj) { | |
if(obj.hasOwnProperty( prop ) ) { | |
if (predicate(obj[prop])) { | |
return prop; | |
} | |
} | |
} | |
} |