format.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // @ts-expect-error ts does not like that this can return an integer (e.g. for `%d`)
  61. const str = String(f).replace(formatRegExp, function (x) {
  62. if (x === '%%') {
  63. return '%';
  64. }
  65. if (i >= len) {
  66. return x;
  67. }
  68. switch (x) {
  69. case '%s':
  70. return String(args[i++]);
  71. case '%d':
  72. return Number(args[i++]);
  73. case '%j':
  74. try {
  75. return JSON.stringify(args[i++]);
  76. } catch (_) {
  77. return '[Circular]';
  78. }
  79. default:
  80. return x;
  81. }
  82. });
  83. pieces.push(str);
  84. for (let x = args[i]; i < len; x = args[++i]) {
  85. if (isNull(x) || !isObject(x)) {
  86. pieces.push(' ' + x);
  87. } else {
  88. pieces.push(' ');
  89. pieces.push(
  90. <ObjectInspector key={i} data={x} expandPaths={expandPaths} onExpand={onExpand} />
  91. );
  92. }
  93. }
  94. return <Fragment>{pieces}</Fragment>;
  95. }