Dashboard sipadu mbip
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dump.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('dump', function(Y) {
  9. /**
  10. * Returns a simple string representation of the object or array.
  11. * Other types of objects will be returned unprocessed. Arrays
  12. * are expected to be indexed. Use object notation for
  13. * associative arrays.
  14. *
  15. * If included, the dump method is added to the YUI instance.
  16. *
  17. * @module dump
  18. */
  19. var L = Y.Lang,
  20. OBJ = '{...}',
  21. FUN = 'f(){...}',
  22. COMMA = ', ',
  23. ARROW = ' => ',
  24. /**
  25. * Returns a simple string representation of the object or array.
  26. * Other types of objects will be returned unprocessed. Arrays
  27. * are expected to be indexed.
  28. *
  29. * @method dump
  30. * @param {Object} o The object to dump.
  31. * @param {Number} d How deep to recurse child objects, default 3.
  32. * @return {String} the dump result.
  33. * @for YUI
  34. */
  35. dump = function(o, d) {
  36. var i, len, s = [], type = L.type(o);
  37. // Cast non-objects to string
  38. // Skip dates because the std toString is what we want
  39. // Skip HTMLElement-like objects because trying to dump
  40. // an element will cause an unhandled exception in FF 2.x
  41. if (!L.isObject(o)) {
  42. return o + '';
  43. } else if (type == 'date') {
  44. return o;
  45. } else if (o.nodeType && o.tagName) {
  46. return o.tagName + '#' + o.id;
  47. } else if (o.document && o.navigator) {
  48. return 'window';
  49. } else if (o.location && o.body) {
  50. return 'document';
  51. } else if (type == 'function') {
  52. return FUN;
  53. }
  54. // dig into child objects the depth specifed. Default 3
  55. d = (L.isNumber(d)) ? d : 3;
  56. // arrays [1, 2, 3]
  57. if (type == 'array') {
  58. s.push('[');
  59. for (i = 0, len = o.length; i < len; i = i + 1) {
  60. if (L.isObject(o[i])) {
  61. s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ);
  62. } else {
  63. s.push(o[i]);
  64. }
  65. s.push(COMMA);
  66. }
  67. if (s.length > 1) {
  68. s.pop();
  69. }
  70. s.push(']');
  71. // regexp /foo/
  72. } else if (type == 'regexp') {
  73. s.push(o.toString());
  74. // objects {k1 => v1, k2 => v2}
  75. } else {
  76. s.push('{');
  77. for (i in o) {
  78. if (o.hasOwnProperty(i)) {
  79. try {
  80. s.push(i + ARROW);
  81. if (L.isObject(o[i])) {
  82. s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ);
  83. } else {
  84. s.push(o[i]);
  85. }
  86. s.push(COMMA);
  87. } catch (e) {
  88. s.push('Error: ' + e.message);
  89. }
  90. }
  91. }
  92. if (s.length > 1) {
  93. s.pop();
  94. }
  95. s.push('}');
  96. }
  97. return s.join('');
  98. };
  99. Y.dump = dump;
  100. L.dump = dump;
  101. }, '3.4.0' ,{requires:['yui-base']});