pp_js.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package jsre
  17. const pp_js = `
  18. function pp(object, indent) {
  19. try {
  20. JSON.stringify(object)
  21. } catch(e) {
  22. return pp(e, indent);
  23. }
  24. var str = "";
  25. if(object instanceof Array) {
  26. str += "[";
  27. for(var i = 0, l = object.length; i < l; i++) {
  28. str += pp(object[i], indent);
  29. if(i < l-1) {
  30. str += ", ";
  31. }
  32. }
  33. str += " ]";
  34. } else if (object instanceof Error) {
  35. str += "\033[31m" + "Error:\033[0m " + object.message;
  36. } else if (isBigNumber(object)) {
  37. str += "\033[32m'" + object.toString(10) + "'";
  38. } else if(typeof(object) === "object") {
  39. str += "{\n";
  40. indent += " ";
  41. var fields = getFields(object);
  42. var last = fields[fields.length - 1];
  43. fields.forEach(function (key) {
  44. str += indent + key + ": ";
  45. try {
  46. str += pp(object[key], indent);
  47. } catch (e) {
  48. str += pp(e, indent);
  49. }
  50. if(key !== last) {
  51. str += ",";
  52. }
  53. str += "\n";
  54. });
  55. str += indent.substr(2, indent.length) + "}";
  56. } else if(typeof(object) === "string") {
  57. str += "\033[32m'" + object + "'";
  58. } else if(typeof(object) === "undefined") {
  59. str += "\033[1m\033[30m" + object;
  60. } else if(typeof(object) === "number") {
  61. str += "\033[31m" + object;
  62. } else if(typeof(object) === "function") {
  63. str += "\033[35m" + object.toString().split(" {")[0];
  64. } else {
  65. str += object;
  66. }
  67. str += "\033[0m";
  68. return str;
  69. }
  70. var redundantFields = [
  71. 'valueOf',
  72. 'toString',
  73. 'toLocaleString',
  74. 'hasOwnProperty',
  75. 'isPrototypeOf',
  76. 'propertyIsEnumerable',
  77. 'constructor'
  78. ];
  79. var getFields = function (object) {
  80. var members = Object.getOwnPropertyNames(object);
  81. if (object.constructor && object.constructor.prototype) {
  82. members = members.concat(Object.getOwnPropertyNames(object.constructor.prototype));
  83. }
  84. var fields = members.filter(function (member) {
  85. return !isMemberFunction(object, member)
  86. }).sort()
  87. var funcs = members.filter(function (member) {
  88. return isMemberFunction(object, member)
  89. }).sort()
  90. var results = fields.concat(funcs);
  91. return results.filter(function (field) {
  92. return redundantFields.indexOf(field) === -1;
  93. });
  94. };
  95. var isMemberFunction = function(object, member) {
  96. try {
  97. return typeof(object[member]) === "function";
  98. } catch(e) {
  99. return false;
  100. }
  101. }
  102. var isBigNumber = function (object) {
  103. var result = typeof BigNumber !== 'undefined' && object instanceof BigNumber;
  104. if (!result) {
  105. if (typeof(object) === "object" && object.constructor != null) {
  106. result = object.constructor.toString().indexOf("function BigNumber(") == 0;
  107. }
  108. }
  109. return result
  110. };
  111. function prettyPrint(/* */) {
  112. var args = arguments;
  113. var ret = "";
  114. for(var i = 0, l = args.length; i < l; i++) {
  115. ret += pp(args[i], "") + "\n";
  116. }
  117. return ret;
  118. }
  119. var print = prettyPrint;
  120. `