index.js 814 Bytes
Newer Older
YazhouChen's avatar
YazhouChen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/**
 * Return the first non null of the passed elements
 * it works the same as
 *
 * a || b
 *
 * but it works on falsie values too
 *
 * @method coalescy
 * @static
 * @return {Object} the first non null of the arguments passed. Null if all the values are null
 * @example
 * ```javascript
 * var clsc = require('coalescy');
 * var obj = clsc(null, []); // obj = [];
 * obj = clsc(null, {}); // obj = {};
 * obj = clsc(null, [], {}); // obj = []; // the first non null
 * obj = clsc(null, undefined, 0, []) // 0
 * ```
 */
module.exports = function clsc() {
  var args = arguments;

  args = [].slice.call( args );
  for (var i = 0, len = args.length; i < len; i++) {
    var current = args[ i ];
    if ( typeof current !== 'undefined' && current !== null  ) {
      return current;
    }
  }
  return null;
};