Skip to content

Instantly share code, notes, and snippets.

@netpoetica
Last active December 9, 2015 19:51

Revisions

  1. Keith Rosenberg revised this gist Dec 9, 2015. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions doif.js
    Original file line number Diff line number Diff line change
    @@ -91,3 +91,13 @@ function dontIf(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){

    }
  2. Keith Rosenberg revised this gist Dec 8, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions doif.js
    Original file line number Diff line number Diff line change
    @@ -15,12 +15,12 @@ function doIf(cond, cb){

    // 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){
  3. Keith Rosenberg created this gist Dec 8, 2015.
    93 changes: 93 additions & 0 deletions doif.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    //
    // 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){

    }

    // 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(){});