objDisplay.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*!
  2. * Chai - flag utility
  3. * Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
  4. * MIT Licensed
  5. */
  6. /*!
  7. * Module dependancies
  8. */
  9. var inspect = require('./inspect');
  10. var config = require('../config');
  11. /**
  12. * ### .objDisplay (object)
  13. *
  14. * Determines if an object or an array matches
  15. * criteria to be inspected in-line for error
  16. * messages or should be truncated.
  17. *
  18. * @param {Mixed} javascript object to inspect
  19. * @name objDisplay
  20. * @api public
  21. */
  22. module.exports = function (obj) {
  23. var str = inspect(obj)
  24. , type = Object.prototype.toString.call(obj);
  25. if (config.truncateThreshold && str.length >= config.truncateThreshold) {
  26. if (type === '[object Function]') {
  27. return !obj.name || obj.name === ''
  28. ? '[Function]'
  29. : '[Function: ' + obj.name + ']';
  30. } else if (type === '[object Array]') {
  31. return '[ Array(' + obj.length + ') ]';
  32. } else if (type === '[object Object]') {
  33. var keys = Object.keys(obj)
  34. , kstr = keys.length > 2
  35. ? keys.splice(0, 2).join(', ') + ', ...'
  36. : keys.join(', ');
  37. return '{ Object (' + kstr + ') }';
  38. } else {
  39. return str;
  40. }
  41. } else {
  42. return str;
  43. }
  44. };