pad-levels.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* eslint no-unused-vars: 0 */
  2. 'use strict';
  3. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  4. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  5. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  6. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  7. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  8. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  9. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
  10. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  11. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  12. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  13. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  14. function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  15. var _require = require('triple-beam'),
  16. configs = _require.configs,
  17. LEVEL = _require.LEVEL,
  18. MESSAGE = _require.MESSAGE;
  19. var Padder = /*#__PURE__*/function () {
  20. function Padder() {
  21. var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
  22. levels: configs.npm.levels
  23. };
  24. _classCallCheck(this, Padder);
  25. this.paddings = Padder.paddingForLevels(opts.levels, opts.filler);
  26. this.options = opts;
  27. }
  28. /**
  29. * Returns the maximum length of keys in the specified `levels` Object.
  30. * @param {Object} levels Set of all levels to calculate longest level against.
  31. * @returns {Number} Maximum length of the longest level string.
  32. */
  33. _createClass(Padder, [{
  34. key: "transform",
  35. value:
  36. /**
  37. * Prepends the padding onto the `message` based on the `LEVEL` of
  38. * the `info`. This is based on the behavior of `winston@2` which also
  39. * prepended the level onto the message.
  40. *
  41. * See: https://github.com/winstonjs/winston/blob/2.x/lib/winston/logger.js#L198-L201
  42. *
  43. * @param {Info} info Logform info object
  44. * @param {Object} opts Options passed along to this instance.
  45. * @returns {Info} Modified logform info object.
  46. */
  47. function transform(info, opts) {
  48. info.message = "".concat(this.paddings[info[LEVEL]]).concat(info.message);
  49. if (info[MESSAGE]) {
  50. info[MESSAGE] = "".concat(this.paddings[info[LEVEL]]).concat(info[MESSAGE]);
  51. }
  52. return info;
  53. }
  54. }], [{
  55. key: "getLongestLevel",
  56. value: function getLongestLevel(levels) {
  57. var lvls = Object.keys(levels).map(function (level) {
  58. return level.length;
  59. });
  60. return Math.max.apply(Math, _toConsumableArray(lvls));
  61. }
  62. /**
  63. * Returns the padding for the specified `level` assuming that the
  64. * maximum length of all levels it's associated with is `maxLength`.
  65. * @param {String} level Level to calculate padding for.
  66. * @param {String} filler Repeatable text to use for padding.
  67. * @param {Number} maxLength Length of the longest level
  68. * @returns {String} Padding string for the `level`
  69. */
  70. }, {
  71. key: "paddingForLevel",
  72. value: function paddingForLevel(level, filler, maxLength) {
  73. var targetLen = maxLength + 1 - level.length;
  74. var rep = Math.floor(targetLen / filler.length);
  75. var padding = "".concat(filler).concat(filler.repeat(rep));
  76. return padding.slice(0, targetLen);
  77. }
  78. /**
  79. * Returns an object with the string paddings for the given `levels`
  80. * using the specified `filler`.
  81. * @param {Object} levels Set of all levels to calculate padding for.
  82. * @param {String} filler Repeatable text to use for padding.
  83. * @returns {Object} Mapping of level to desired padding.
  84. */
  85. }, {
  86. key: "paddingForLevels",
  87. value: function paddingForLevels(levels) {
  88. var filler = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ' ';
  89. var maxLength = Padder.getLongestLevel(levels);
  90. return Object.keys(levels).reduce(function (acc, level) {
  91. acc[level] = Padder.paddingForLevel(level, filler, maxLength);
  92. return acc;
  93. }, {});
  94. }
  95. }]);
  96. return Padder;
  97. }();
  98. /*
  99. * function padLevels (info)
  100. * Returns a new instance of the padLevels Format which pads
  101. * levels to be the same length. This was previously exposed as
  102. * { padLevels: true } to transports in `winston < 3.0.0`.
  103. */
  104. module.exports = function (opts) {
  105. return new Padder(opts);
  106. };
  107. module.exports.Padder = module.exports.Format = Padder;