Skip to content
dbj*Cond development http://dbj.org/dbj/?p=119
JavaScript Shell HTML
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
gevaltest
node_modules/.bin
nodetest
qunittest
wiki
.gitignore
.npmignore
ReadMe.md
Web.config
dbj.cond.js
mit-license.txt
package.json

ReadMe.md

dbj.cond()

(c) 2009-2015 .. and beyond, by dbj.org
Licensed under the MIT (MIT-LICENSE.txt)

An conditional coding abstraction inspired with LISP (cond ... ) and (case ...) statements. Non-trival conditional logic coding, but still clean code with no complex if/else cascades and with no switch(). Call syntax:

       dbj.cond( input_val,
                 chechk_val_1, outcome_1,
                 chechk_val_2, outcome_2,
                 default_outcome ) ;
      /*
      returns outcome_1 if input_val === check_val_1
      returns outcome_2 if input_val === check_val_2
      otherwise returns default_outcome

      there can be "any" number of check/outcome pairs
      */
  • Any legal values or expressions are allowed as arguments
  • standard comparison of input vs check vals is one of JS strict equality, aka "==="
  • processing stops on first comparison yielding true
  • There can be any number of check/outcome pairs

    node.js usage

    npm install dbj.cond
    
          var dbj = require("dbj.cond").dbj ;
                  dbj.cond(1,1,"1","!") //=> "1"

    Browser usage

                  // include dbj.cond.js
                  dbj.cond(1,1,"1","!"); //=> "1"

    Standard comparison is one of strict equality.

    Beyond basic usage

    User defined comparators are probably the most powerfull feature of dbj.cond(). JS switch() stement uses "===" as the only possible comparator. Using dbj.cond() it is easy to change the comparator used. To change the standard (strict equality) comparator:

          // switching to user defined comparator
          dbj.cond.comparator = function myComparator (a,b ) { return a != b ; };
                  dbj.cond(1,1,"1","!") //=> "!"
    
          // switching back to standard comparator
          dbj.cond.comparator = dbj.compare.standard ;
                  dbj.cond(1,1,"1","!") //=> "1"

    For reasons of performance dbj.cond.comparator is not checked for validity. For using dbj.cond() with complex JS types two powerfull non-standard comparators are provided, "deep" and "multi". Usage:

         // use dbj deep equality comparator
         dbj.cond.comparator = dbj.compare.deep;
         // compare objects
             dbj.cond({ "Alpha": 1 }, 
                      { "Beta": 2 }, false, 
                      { "Alpha": 1 }, "Works!", 
                      false ); //=> "Works!"
    
             // use dbj single to/from array comparator
             // which also does deep equality comparisons
             dbj.cond.comparator = dbj.compare.multi;
             // compare arrays for equality
             dbj.cond(
                 [1,2], 
                 [3,2], false, 
                 [1,2], "Works!"
                      , false 
                     ); //=> "Works!"
    
             // lookup into the array also works
             // input val array is 'looked up' 
             dbj.cond(
                  [1,2], 
                  [3,2]        , false, 
                  ['2d', [1,2]], "Works!"
                               , false 
                     ); //=> "Works!"  
                        // [1,2] is found in ['2d', [1,2]]
    
             // lookup from the array too
             // aka 'reverse' lookup
             dbj.cond(
                  ['2d', [1,2]], 
                  [3,2]         , false, 
                  [1,2]         , "Works!"
                                , false 
                     ); //=> "Works!"

    NOTE

    It is to be the reference source and knowledge base, but right now Wiki is work-in-progress, mainly to sync it with a code after numerous changes and re-thinking sessions. After this note is removed, Wiki will be released as the reference text on dbj.cond()


© by dbj();

Something went wrong with that request. Please try again.