index.js 814 B

1234567891011121314151617181920212223242526272829303132
  1. /**
  2. * Return the first non null of the passed elements
  3. * it works the same as
  4. *
  5. * a || b
  6. *
  7. * but it works on falsie values too
  8. *
  9. * @method coalescy
  10. * @static
  11. * @return {Object} the first non null of the arguments passed. Null if all the values are null
  12. * @example
  13. * ```javascript
  14. * var clsc = require('coalescy');
  15. * var obj = clsc(null, []); // obj = [];
  16. * obj = clsc(null, {}); // obj = {};
  17. * obj = clsc(null, [], {}); // obj = []; // the first non null
  18. * obj = clsc(null, undefined, 0, []) // 0
  19. * ```
  20. */
  21. module.exports = function clsc() {
  22. var args = arguments;
  23. args = [].slice.call( args );
  24. for (var i = 0, len = args.length; i < len; i++) {
  25. var current = args[ i ];
  26. if ( typeof current !== 'undefined' && current !== null ) {
  27. return current;
  28. }
  29. }
  30. return null;
  31. };