pad-levels.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
  7. function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
  8. function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
  9. function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
  10. function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
  11. function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
  12. function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
  13. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  14. function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
  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. return _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. }();
  97. /*
  98. * function padLevels (info)
  99. * Returns a new instance of the padLevels Format which pads
  100. * levels to be the same length. This was previously exposed as
  101. * { padLevels: true } to transports in `winston < 3.0.0`.
  102. */
  103. module.exports = function (opts) {
  104. return new Padder(opts);
  105. };
  106. module.exports.Padder = module.exports.Format = Padder;