splat.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. 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; }; }();
  3. 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); } }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. var util = require('util');
  6. var _require = require('triple-beam'),
  7. SPLAT = _require.SPLAT;
  8. /**
  9. * Captures the number of format (i.e. %s strings) in a given string.
  10. * Based on `util.format`, see Node.js source:
  11. * https://github.com/nodejs/node/blob/b1c8f15c5f169e021f7c46eb7b219de95fe97603/lib/util.js#L201-L230
  12. * @type {RegExp}
  13. */
  14. var formatRegExp = /%[scdjifoO%]/g;
  15. /**
  16. * Captures the number of escaped % signs in a format string (i.e. %s strings).
  17. * @type {RegExp}
  18. */
  19. var escapedPercent = /%%/g;
  20. var Splatter = function () {
  21. function Splatter(opts) {
  22. _classCallCheck(this, Splatter);
  23. this.options = opts;
  24. }
  25. /**
  26. * Check to see if tokens <= splat.length, assign { splat, meta } into the
  27. * `info` accordingly, and write to this instance.
  28. *
  29. * @param {Info} info Logform info message.
  30. * @param {String[]} tokens Set of string interpolation tokens.
  31. * @returns {Info} Modified info message
  32. * @private
  33. */
  34. _createClass(Splatter, [{
  35. key: '_splat',
  36. value: function _splat(info, tokens) {
  37. var msg = info.message;
  38. var splat = info[SPLAT] || info.splat || [];
  39. var percents = msg.match(escapedPercent);
  40. var escapes = percents && percents.length || 0;
  41. // The expected splat is the number of tokens minus the number of escapes
  42. // e.g.
  43. // - { expectedSplat: 3 } '%d %s %j'
  44. // - { expectedSplat: 5 } '[%s] %d%% %d%% %s %j'
  45. //
  46. // Any "meta" will be arugments in addition to the expected splat size
  47. // regardless of type. e.g.
  48. //
  49. // logger.log('info', '%d%% %s %j', 100, 'wow', { such: 'js' }, { thisIsMeta: true });
  50. // would result in splat of four (4), but only three (3) are expected. Therefore:
  51. //
  52. // extraSplat = 3 - 4 = -1
  53. // metas = [100, 'wow', { such: 'js' }, { thisIsMeta: true }].splice(-1, -1 * -1);
  54. // splat = [100, 'wow', { such: 'js' }]
  55. var expectedSplat = tokens.length - escapes;
  56. var extraSplat = expectedSplat - splat.length;
  57. var metas = extraSplat < 0 ? splat.splice(extraSplat, -1 * extraSplat) : [];
  58. // Now that { splat } has been separated from any potential { meta }. we
  59. // can assign this to the `info` object and write it to our format stream.
  60. if (metas.length === 1) {
  61. info.meta = metas[0];
  62. } else if (metas.length) {
  63. info.meta = metas;
  64. }
  65. info.message = util.format.apply(util, [msg].concat(_toConsumableArray(splat)));
  66. return info;
  67. }
  68. /**
  69. * Transforms the `info` message by using `util.format` to complete
  70. * any `info.message` provided it has string interpolation tokens.
  71. * If no tokens exist then `info` is immutable.
  72. *
  73. * @param {Info} info Logform info message.
  74. * @param {Object} opts Options for this instance.
  75. * @returns {Info} Modified info message
  76. */
  77. }, {
  78. key: 'transform',
  79. value: function transform(info) {
  80. var msg = info.message;
  81. var splat = info[SPLAT] || info.splat;
  82. // No need to process anything if splat is undefined
  83. if (!splat || !splat.length) {
  84. return info;
  85. }
  86. // Extract tokens, if none available default to empty array to
  87. // ensure consistancy in expected results
  88. var tokens = msg && msg.match && msg.match(formatRegExp);
  89. // This condition will take care of inputs with info[SPLAT]
  90. // but no tokens present
  91. if (!tokens && (splat || splat.length)) {
  92. var metas = splat.length > 1 ? splat.splice(0) : splat;
  93. // Now that { splat } has been separated from any potential { meta }. we
  94. // can assign this to the `info` object and write it to our format stream.
  95. if (metas.length === 1) {
  96. info.meta = metas[0];
  97. } else if (metas.length) {
  98. info.meta = metas;
  99. }
  100. return info;
  101. }
  102. if (tokens) {
  103. return this._splat(info, tokens);
  104. }
  105. return info;
  106. }
  107. }]);
  108. return Splatter;
  109. }();
  110. /*
  111. * function splat (info)
  112. * Returns a new instance of the splat format TransformStream
  113. * which performs string interpolation from `info` objects. This was
  114. * previously exposed implicitly in `winston < 3.0.0`.
  115. */
  116. module.exports = function (opts) {
  117. return new Splatter(opts);
  118. };