dumper.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Recursively print out all names and values in a data structure.
  3. * @module jsdoc/util/dumper
  4. */
  5. const util = require('util');
  6. const OBJECT_WALKER_KEY = 'hasBeenSeenByWalkerDumper';
  7. const SET_DEFINED = (typeof Set !== 'undefined');
  8. class ObjectWalker {
  9. constructor() {
  10. this.seenItems = SET_DEFINED ? new Set() : [];
  11. }
  12. seen(object) {
  13. let result;
  14. if (SET_DEFINED) {
  15. result = this.seenItems.has(object);
  16. }
  17. else {
  18. result = object[OBJECT_WALKER_KEY];
  19. }
  20. return result;
  21. }
  22. markAsSeen(object) {
  23. if (SET_DEFINED) {
  24. this.seenItems.add(object);
  25. }
  26. else {
  27. object[OBJECT_WALKER_KEY] = true;
  28. this.seenItems.push(object);
  29. }
  30. }
  31. removeSeenFlag(obj) {
  32. if (SET_DEFINED) {
  33. this.seenItems.delete(obj);
  34. }
  35. else {
  36. delete obj[OBJECT_WALKER_KEY];
  37. }
  38. }
  39. /* eslint-disable class-methods-use-this */
  40. // some objects are unwalkable, like Java native objects
  41. isUnwalkable(o) {
  42. return o && typeof o === 'object' && typeof o.constructor === 'undefined';
  43. }
  44. isFunction(o) {
  45. return (o && typeof o === 'function') || o instanceof Function;
  46. }
  47. isObject(o) {
  48. return (o && o instanceof Object) ||
  49. (o && typeof o.constructor !== 'undefined' && o.constructor.name === 'Object');
  50. }
  51. /* eslint-enable class-methods-use-this */
  52. checkCircularRefs(o, func) {
  53. if ( this.seen(o) ) {
  54. return '<CircularRef>';
  55. }
  56. else {
  57. this.markAsSeen(o);
  58. return func(o);
  59. }
  60. }
  61. walk(o) {
  62. let result;
  63. const self = this;
  64. if ( this.isUnwalkable(o) ) {
  65. result = '<Object>';
  66. }
  67. else if ( o === undefined ) {
  68. result = null;
  69. }
  70. else if ( Array.isArray(o) ) {
  71. result = this.checkCircularRefs(o, arr => {
  72. const newArray = [];
  73. arr.forEach(item => {
  74. newArray.push( self.walk(item) );
  75. });
  76. self.removeSeenFlag(arr);
  77. return newArray;
  78. });
  79. }
  80. else if ( util.isRegExp(o) ) {
  81. result = `<RegExp ${o}>`;
  82. }
  83. else if ( util.isDate(o) ) {
  84. result = `<Date ${o.toUTCString()}>`;
  85. }
  86. else if ( util.isError(o) ) {
  87. result = { message: o.message };
  88. }
  89. else if ( this.isFunction(o) ) {
  90. result = `<Function${o.name ? ` ${o.name}` : ''}>`;
  91. }
  92. else if ( this.isObject(o) && o !== null ) {
  93. result = this.checkCircularRefs(o, obj => {
  94. const newObj = {};
  95. Object.keys(obj).forEach(key => {
  96. if (!SET_DEFINED && key === OBJECT_WALKER_KEY) { return; }
  97. newObj[key] = self.walk(obj[key]);
  98. });
  99. self.removeSeenFlag(obj);
  100. return newObj;
  101. });
  102. }
  103. // should be safe to JSON.stringify() everything else
  104. else {
  105. result = o;
  106. }
  107. return result;
  108. }
  109. }
  110. /**
  111. * @param {*} object
  112. */
  113. exports.dump = function(...args) {
  114. const result = [];
  115. let walker;
  116. for (let arg of args) {
  117. walker = new ObjectWalker();
  118. result.push( JSON.stringify(walker.walk(arg), null, 4) );
  119. }
  120. return result.join('\n');
  121. };