No if’s.Only buts ? Enter dbj.cond(). Stright into the deep end.
var x = dbj.cond(v1,v2,v3,v4,v5,v6)
Is a shorthand for:
1 2 3 4 5 6 7 8 9 |
/* compare() returns true or false */ var x = if ( compare(v1,v2) ) v3 ; // x may be v3, v5 or v6 else if ( compare(v1,v4) ) v5 ; else v6 ; /* v2 and v4 can be arrays, in which case their members will be checked. */ |
Original dbj.cond() , worked only on values that can be compared
for equality, with native ‘===’ operator.
New dbj.cond() allows users to compare values for equality with other values of any type or arrays of them. Syntax is the same as before:
1 2 3 4 5 6 |
// dbj.cond(a,b,c,...,d) ; // returns c if a equals b, // otherwise returns the last argument: d // there can be any number of pairs arguments // like b and c |
There can be any number of pairs before last argument and after the first argument.
Behaviour is that arguments can be of any type including objects, arrays, dates, etc. Function compare(a,b)
is called comparator.
false as a result of comparing the arguments. Simple comparator example:
1 2 3 4 |
// // returns true if a is less than b, false otherwise function less_then ( a, b ) { return a < b; } // |
examples
1 2 3 4 |
dbj.cond( 1, 1, "A", 2, "B", "C") // returns "A""" dbj.cond( 1, [3,2,1],"A", 2, "B", "C") // returns "A" , 1 is found first in [3,2,1] |
Order is “first found, first served”. Any types can be compared meaningfully. For example:
1 2 3 4 5 6 7 8 9 10 11 |
dbj.cond( /./, /./, "A", /$/, "B", "C") // returns "A" // Function types are also compared and not called as functions dbj.cond( function(){return 1;}, function(){return 1;}, "A", function(){return 1;}, "B", "C" ) ; // returns "A" |
This behavior allows for functions dispatching. Example:
1 2 3 4 5 6 7 |
// function dispatcher ( fx ) { return dbj.cond( fx, f1, f2, f3, f4, f5 ) ; // returns f2,f4 // return f5 if neither f1 or f3 are equal to fx } // |
Comparator is user definable. dbj.cond() is using comparator function assigned to:
1 2 |
dbj.cond.comparator = null; // if null default equality comparator is used |
Please see online qUnit Tests, for further examples.Internals
- Comparator in essence defines the behaviour of dbj.cond()
- Default inbuilt comparator works for all types.
- First argument of the dbj.cond() is the left side in the comparison.
Second value can be single value or array. - Default comparator allows for arrays of values to be compared with the single input value of the same type
- Examples:
123456789101112131415default_comparator( 1, [3,2,1] )// --> true, 1 is found in [3,2,1]default_comparator( function (){ return 1;}, [3,2,1] )// --> false, input value is not executed as functiondefault_comparator( [3,2,1], [3,2,1] )// --> true, comparing equal arraysdefault_comparator( [3,2,1], [[7,8],[3,2,1]] )// --> true, second array is equal to input - First argument of the comparator is also called “input”
- Heart of complex comparator for
dbj.cond()
, is originaly made by Philippe Rathé - Tests for equality of any JavaScript type. Used in QUnit.
- Discussions and reference: http://philrathe.com/articles/equiv