pad-levels.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* eslint no-unused-vars: 0 */
  2. 'use strict';
  3. var _createClass = function () { 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  5. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6. var _require = require('triple-beam'),
  7. configs = _require.configs,
  8. LEVEL = _require.LEVEL,
  9. MESSAGE = _require.MESSAGE;
  10. var Padder = function () {
  11. function Padder() {
  12. var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { levels: configs.npm.levels };
  13. _classCallCheck(this, Padder);
  14. this.paddings = Padder.paddingForLevels(opts.levels, opts.filler);
  15. this.options = opts;
  16. }
  17. /**
  18. * Returns the maximum length of keys in the specified `levels` Object.
  19. * @param {Object} levels Set of all levels to calculate longest level against.
  20. * @returns {Number} Maximum length of the longest level string.
  21. */
  22. _createClass(Padder, [{
  23. key: 'transform',
  24. /**
  25. * Prepends the padding onto the `message` based on the `LEVEL` of
  26. * the `info`. This is based on the behavior of `winston@2` which also
  27. * prepended the level onto the message.
  28. *
  29. * See: https://github.com/winstonjs/winston/blob/2.x/lib/winston/logger.js#L198-L201
  30. *
  31. * @param {Info} info Logform info object
  32. * @param {Object} opts Options passed along to this instance.
  33. * @returns {Info} Modified logform info object.
  34. */
  35. value: function transform(info, opts) {
  36. info.message = '' + this.paddings[info[LEVEL]] + info.message;
  37. if (info[MESSAGE]) {
  38. info[MESSAGE] = '' + this.paddings[info[LEVEL]] + info[MESSAGE];
  39. }
  40. return info;
  41. }
  42. }], [{
  43. key: 'getLongestLevel',
  44. value: function getLongestLevel(levels) {
  45. var lvls = Object.keys(levels).map(function (level) {
  46. return level.length;
  47. });
  48. return Math.max.apply(Math, _toConsumableArray(lvls));
  49. }
  50. /**
  51. * Returns the padding for the specified `level` assuming that the
  52. * maximum length of all levels it's associated with is `maxLength`.
  53. * @param {String} level Level to calculate padding for.
  54. * @param {String} filler Repeatable text to use for padding.
  55. * @param {Number} maxLength Length of the longest level
  56. * @returns {String} Padding string for the `level`
  57. */
  58. }, {
  59. key: 'paddingForLevel',
  60. value: function paddingForLevel(level, filler, maxLength) {
  61. var targetLen = maxLength + 1 - level.length;
  62. var rep = Math.floor(targetLen / filler.length);
  63. var padding = '' + filler + filler.repeat(rep);
  64. return padding.slice(0, targetLen);
  65. }
  66. /**
  67. * Returns an object with the string paddings for the given `levels`
  68. * using the specified `filler`.
  69. * @param {Object} levels Set of all levels to calculate padding for.
  70. * @param {String} filler Repeatable text to use for padding.
  71. * @returns {Object} Mapping of level to desired padding.
  72. */
  73. }, {
  74. key: 'paddingForLevels',
  75. value: function paddingForLevels(levels) {
  76. var filler = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ' ';
  77. var maxLength = Padder.getLongestLevel(levels);
  78. return Object.keys(levels).reduce(function (acc, level) {
  79. acc[level] = Padder.paddingForLevel(level, filler, maxLength);
  80. return acc;
  81. }, {});
  82. }
  83. }]);
  84. return Padder;
  85. }();
  86. /*
  87. * function padLevels (info)
  88. * Returns a new instance of the padLevels Format which pads
  89. * levels to be the same length. This was previously exposed as
  90. * { padLevels: true } to transports in `winston < 3.0.0`.
  91. */
  92. module.exports = function (opts) {
  93. return new Padder(opts);
  94. };
  95. module.exports.Padder = module.exports.Format = Padder;