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 hasProperty(obj, prop) { | |
return obj[prop] !== undefined; | |
}; | |
Array.prototype.callFunction = function(target, key) { | |
var args = [].splice.call(arguments, 0).slice(2); | |
this.forEach(function(elem) { | |
try { | |
if (hasProperty(elem, key) && typeof elem[key] == 'function') { |
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
MapObj = (obj, mapFn) -> | |
if obj.length && obj.join | |
return obj.map(mapFn) | |
result = {} | |
result[key] = mapFn(val, key) for key, val of obj | |
result | |
FilterObj = (obj, filterFn) -> | |
if obj.length && obj.join | |
return obj.filter(filterFn) |
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 MapObj = function(obj, mapFn) { | |
var key, result, val; | |
if (obj.length && obj.join) { | |
return obj.map(mapFn); | |
} | |
result = {}; | |
for (key in obj) { | |
val = obj[key]; | |
result[key] = mapFn(val, key); | |
} |
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 variableArgs, | |
slice = [].slice; | |
variableArgs = function(callbacks) { | |
var defaultKey; | |
defaultKey = 'default'; | |
return function() { | |
var args, numArgs, ref; | |
args = 1 <= arguments.length ? slice.call(arguments, 0) : []; | |
numArgs = args.length || 0; |
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 typedArgument(type, value, defaultValue) { | |
return (typeof value !== 'undefined' && typeof value !== 'null' && ((typeof type === 'function' && value.constructor === type) || (typeof type === 'string' && (!type || typeof value === type)))) ? value : defaultValue; | |
} | |
/** Example: **/ | |
function add(a,b) { | |
var a = typedArgument(Number, a, 0); | |
var b = typedArgument('number', b, 0); | |
return a + b; | |
} |
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 extend() { | |
var result = arguments[0]; | |
for(var i = 1; i < arguments.length; i++) { | |
for(var key in arguments[i]) { | |
result[key] = arguments[i][key]; | |
} | |
} | |
return result; | |
} |
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 toArray(array) { | |
return Array.prototype.slice.call(array); | |
} |
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 extend() { | |
var result = arguments[0]; | |
for(var i = 1; i < arguments.length; i++) { | |
for(var key in arguments[i]) { | |
result[key] = arguments[i][key]; | |
} | |
} | |
return result; | |
} |
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 object = { | |
"value": 1, | |
"anotherValue": 2, | |
"nested": { | |
"value": 3 | |
} | |
}; | |
object.value | |
// => 1 |
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 pickWeighted(array) { | |
var n = Math.random(), | |
totalChances = 0, | |
total = 0, | |
result; | |
array = array.map(function(row) { | |
var min, max; | |
var chance = typeof row == 'object' || typeof row == 'function' ? row.chance : 1; | |
if (chance) { | |
min = totalChances; |
OlderNewer