format.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // This is based on https://github.com/browserify/node-util/blob/master/util.js
  2. // Copyright Joyent, Inc. and other Node contributors.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to permit
  9. // persons to whom the Software is furnished to do so, subject to the
  10. // following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included
  13. // in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  18. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  19. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  20. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  21. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. import {Fragment} from 'react';
  23. import isObject from 'lodash/isObject';
  24. import ObjectInspector, {OnExpandCallback} from 'sentry/components/objectInspector';
  25. const formatRegExp = /%[sdj%]/g;
  26. function isNull(arg: unknown) {
  27. return arg === null;
  28. }
  29. interface FormatProps {
  30. args: any[];
  31. expandPaths?: string[];
  32. onExpand?: OnExpandCallback;
  33. }
  34. /**
  35. * Based on node's `util.format()`, returns a formatted "string" using the
  36. * first argument as a printf-like format string which can contain zero or more
  37. * format specifiers. Uses `<ObjectInspector>` to print objects.
  38. *
  39. * %c is ignored for now
  40. */
  41. export default function Format({onExpand, expandPaths, args}: FormatProps) {
  42. const f = args[0];
  43. if (typeof f !== 'string') {
  44. const objects: any[] = [];
  45. for (let i = 0; i < args.length; i++) {
  46. objects.push(
  47. <ObjectInspector
  48. key={i}
  49. data={args[i]}
  50. expandPaths={expandPaths}
  51. onExpand={onExpand}
  52. />
  53. );
  54. }
  55. return <Fragment>{objects}</Fragment>;
  56. }
  57. let i = 1;
  58. const len = args.length;
  59. const pieces: any[] = [];
  60. const str = String(f).replace(formatRegExp, function (x) {
  61. if (x === '%%') {
  62. return '%';
  63. }
  64. if (i >= len) {
  65. return x;
  66. }
  67. switch (x) {
  68. case '%s':
  69. const val = args[i++];
  70. try {
  71. return String(val);
  72. } catch {
  73. return 'toString' in val ? val.toString : JSON.stringify(val);
  74. }
  75. case '%d':
  76. return Number(args[i++]);
  77. case '%j':
  78. try {
  79. return JSON.stringify(args[i++]);
  80. } catch (_) {
  81. return '[Circular]';
  82. }
  83. default:
  84. return x;
  85. }
  86. });
  87. pieces.push(str);
  88. for (let x = args[i]; i < len; x = args[++i]) {
  89. if (isNull(x) || !isObject(x)) {
  90. pieces.push(' ' + x);
  91. } else {
  92. pieces.push(' ');
  93. pieces.push(
  94. <ObjectInspector key={i} data={x} expandPaths={expandPaths} onExpand={onExpand} />
  95. );
  96. }
  97. }
  98. return <Fragment>{pieces}</Fragment>;
  99. }