Skip to content

Instantly share code, notes, and snippets.

@danielrw7
danielrw7 / event-framework.js
Created June 21, 2015 22:31
Simple event triggering/handling framework
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') {
@danielrw7
danielrw7 / object-map-filter.coffee
Created July 10, 2015 22:39
Object mapping and filtering
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)
@danielrw7
danielrw7 / object-map-filter.js
Created July 10, 2015 22:40
Object mapping and filtering
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);
}
@danielrw7
danielrw7 / variableArgs.js
Last active September 14, 2015 01:13
A simple javascript/coffeescript helper function to run different callbacks for different number of arguments
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;
@danielrw7
danielrw7 / typedArgument.js
Created November 2, 2015 19:01
JavaScript typed arguments
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;
}
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;
}
function toArray(array) {
return Array.prototype.slice.call(array);
}
@danielrw7
danielrw7 / extendable.js
Created December 6, 2015 18:49
Helpers for creating extendable JavaScript objects
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;
}
@danielrw7
danielrw7 / example.js
Last active February 26, 2016 15:50
Nested Object Protection Wrapper
var object = {
"value": 1,
"anotherValue": 2,
"nested": {
"value": 3
}
};
object.value
// => 1
@danielrw7
danielrw7 / pick-weighted.js
Last active December 9, 2015 19:20
Randomly pick an array element with weighted chances
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;