ConvertAnsi.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = exports.serialize = exports.test = void 0;
  6. var _ansiRegex = _interopRequireDefault(require('ansi-regex'));
  7. var _ansiStyles = _interopRequireDefault(require('ansi-styles'));
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. /**
  12. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  13. *
  14. * This source code is licensed under the MIT license found in the
  15. * LICENSE file in the root directory of this source tree.
  16. */
  17. const toHumanReadableAnsi = text =>
  18. text.replace((0, _ansiRegex.default)(), match => {
  19. switch (match) {
  20. case _ansiStyles.default.red.close:
  21. case _ansiStyles.default.green.close:
  22. case _ansiStyles.default.cyan.close:
  23. case _ansiStyles.default.gray.close:
  24. case _ansiStyles.default.white.close:
  25. case _ansiStyles.default.yellow.close:
  26. case _ansiStyles.default.bgRed.close:
  27. case _ansiStyles.default.bgGreen.close:
  28. case _ansiStyles.default.bgYellow.close:
  29. case _ansiStyles.default.inverse.close:
  30. case _ansiStyles.default.dim.close:
  31. case _ansiStyles.default.bold.close:
  32. case _ansiStyles.default.reset.open:
  33. case _ansiStyles.default.reset.close:
  34. return '</>';
  35. case _ansiStyles.default.red.open:
  36. return '<red>';
  37. case _ansiStyles.default.green.open:
  38. return '<green>';
  39. case _ansiStyles.default.cyan.open:
  40. return '<cyan>';
  41. case _ansiStyles.default.gray.open:
  42. return '<gray>';
  43. case _ansiStyles.default.white.open:
  44. return '<white>';
  45. case _ansiStyles.default.yellow.open:
  46. return '<yellow>';
  47. case _ansiStyles.default.bgRed.open:
  48. return '<bgRed>';
  49. case _ansiStyles.default.bgGreen.open:
  50. return '<bgGreen>';
  51. case _ansiStyles.default.bgYellow.open:
  52. return '<bgYellow>';
  53. case _ansiStyles.default.inverse.open:
  54. return '<inverse>';
  55. case _ansiStyles.default.dim.open:
  56. return '<dim>';
  57. case _ansiStyles.default.bold.open:
  58. return '<bold>';
  59. default:
  60. return '';
  61. }
  62. });
  63. const test = val =>
  64. typeof val === 'string' && !!val.match((0, _ansiRegex.default)());
  65. exports.test = test;
  66. const serialize = (val, config, indentation, depth, refs, printer) =>
  67. printer(toHumanReadableAnsi(val), config, indentation, depth, refs);
  68. exports.serialize = serialize;
  69. const plugin = {
  70. serialize,
  71. test
  72. };
  73. var _default = plugin;
  74. exports.default = _default;