Last active
December 9, 2015 19:51
-
-
Save netpoetica/f1e3ea21781eaf945100 to your computer and use it in GitHub Desktop.
doif (pronounced "doof") JavaScript subset
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
// | |
// raw whitepaper for functional interface/subset of JS that eliminates logical operations | |
// similar to IFTTT. Based off of this image/site: https://ifttt.com/products | |
// | |
function doIf(cond, cb){ | |
if(cond instanceof Array){ | |
doIfEach(cond, cb); | |
} else if(cond === true && typeof cb === 'function'){ | |
cb(); | |
} | |
} | |
// Example: | |
// doIf(config.webSockets, fn) | |
// Takes two functions - ignores the first one, exec the second one. | |
function inLieuOf(fn1, fn2){ | |
fn2(); | |
} | |
// Run fn once, then check cond - if true, repeat. | |
function doWhile(cond, fn){ | |
} | |
function doIfEach(arr, cb){ | |
var conds = arr.length; | |
arr.forEach(function(cond){ | |
// If the item passed was a function, call it and then | |
// expect it to represent the comparable condition | |
if(typeof cond === 'function'){ | |
cond = cond(); | |
} | |
if(cond){ | |
conds--; | |
} | |
}); | |
if(conds === 0 && typeof cb === 'function'){ | |
cb(); | |
} | |
} | |
// Example: | |
/* | |
doIfEach([ | |
name == 'Tom', | |
function(){ | |
return true; | |
} | |
], function); | |
*/ | |
function doOneIf(cond, arrFn){ | |
var len = arrFn.len; | |
var fn = arrFn[Math.floor(Math.random() * len)]; | |
doIf(cond, fn); | |
} | |
// Example: | |
/* | |
doOneIf(true, [ | |
function(){ | |
}, | |
function(){ | |
} | |
]); | |
*/ | |
// Example: | |
function doSomeIf(cond, arrFn){ | |
var len = arrFn.len; | |
var howMany = Math.floor(Math.random() * len); | |
for( ; i > howMany; howMany--){ | |
doIf(cond, arrFn[Math.floor(Math.random() * len)]); | |
} | |
} | |
/* | |
doSomeIf(true, [ | |
function(){ | |
}, | |
function(){ | |
} | |
]); | |
*/ | |
function dontIf(cond, fn){ | |
doIf(!(cond), fn) | |
} | |
// Example: | |
// dontIf(true, function(){}); | |
// Will most likely run your function but not guaranteed. | |
function likely(fn){ | |
} | |
// Unlikely to run your function, but maybe. | |
function unlikely(fn){ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment