calculateMaximumColumnWidthIndex.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _lodash = require('lodash');
  6. var _lodash2 = _interopRequireDefault(_lodash);
  7. var _calculateCellWidthIndex = require('./calculateCellWidthIndex');
  8. var _calculateCellWidthIndex2 = _interopRequireDefault(_calculateCellWidthIndex);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * Produces an array of values that describe the largest value length (width) in every column.
  12. *
  13. * @param {Array[]} rows
  14. * @returns {number[]}
  15. */
  16. exports.default = rows => {
  17. if (!rows[0]) {
  18. throw new Error('Dataset must have at least one row.');
  19. }
  20. const columns = _lodash2.default.fill(Array(rows[0].length), 0);
  21. _lodash2.default.forEach(rows, row => {
  22. const columnWidthIndex = (0, _calculateCellWidthIndex2.default)(row);
  23. _lodash2.default.forEach(columnWidthIndex, (valueWidth, index0) => {
  24. if (columns[index0] < valueWidth) {
  25. columns[index0] = valueWidth;
  26. }
  27. });
  28. });
  29. return columns;
  30. };