tty-table 6.0.0 → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.mjs CHANGED
@@ -505,28 +505,28 @@ import smartwrap from "smartwrap";
505
505
  var addPadding = (config, width) => {
506
506
  return width + config.paddingLeft + config.paddingRight;
507
507
  };
508
+ var getDisplayWidth = (value) => {
509
+ const lines = stripAnsi2(value.toString()).split(/[\n\r]/);
510
+ let widest = 0;
511
+ for (const line of lines) {
512
+ const width = (0, import_wcwidth.default)(line);
513
+ if (width > widest) widest = width;
514
+ }
515
+ return widest;
516
+ };
508
517
  var getMaxLength = (columnOptions, rows, columnIndex) => {
509
- let iterable;
518
+ let widest = 0;
510
519
  if (columnOptions && (columnOptions.value || columnOptions.alias)) {
511
- let val = columnOptions.alias || columnOptions.value;
512
- val = val.toString();
513
- const headerRow = Array(rows[0].length);
514
- headerRow[columnIndex] = val;
515
- iterable = rows.slice();
516
- iterable.push(headerRow);
517
- } else {
518
- iterable = rows;
520
+ const value = columnOptions.alias || columnOptions.value;
521
+ widest = getDisplayWidth(value);
519
522
  }
520
- const widest = iterable.reduce((prev, row) => {
523
+ for (const row of rows) {
521
524
  if (row[columnIndex]) {
522
525
  const value = row[columnIndex].value ? row[columnIndex].value : row[columnIndex];
523
- const width = Math.max(
524
- ...stripAnsi2(value.toString()).split(/[\n\r]/).map((s) => (0, import_wcwidth.default)(s))
525
- );
526
- return width > prev ? width : prev;
526
+ const width = getDisplayWidth(value);
527
+ if (width > widest) widest = width;
527
528
  }
528
- return prev;
529
- }, 0);
529
+ }
530
530
  return widest;
531
531
  };
532
532
  var getAvailableWidth = (config) => {
@@ -591,13 +591,13 @@ var wrapCellText = (config, cellValue, columnIndex, cellOptions, rowType) => {
591
591
  emptySpace--;
592
592
  const padBoth = Math.floor(emptySpace / 2);
593
593
  const padRemainder = emptySpace % 2;
594
- line = Array(padBoth + 1).join(" ") + line + Array(padBoth + 1 + padRemainder).join(" ");
594
+ line = " ".repeat(padBoth) + line + " ".repeat(padBoth + padRemainder);
595
595
  break;
596
596
  case cellOptions[alignTgt] === "right":
597
- line = Array(emptySpace - cellOptions.paddingRight).join(" ") + line + Array(cellOptions.paddingRight + 1).join(" ");
597
+ line = " ".repeat(emptySpace - cellOptions.paddingRight - 1) + line + " ".repeat(cellOptions.paddingRight);
598
598
  break;
599
599
  default:
600
- line = Array(cellOptions.paddingLeft + 1).join(" ") + line + Array(emptySpace - cellOptions.paddingLeft).join(" ");
600
+ line = " ".repeat(cellOptions.paddingLeft) + line + " ".repeat(emptySpace - cellOptions.paddingLeft - 1);
601
601
  }
602
602
  }
603
603
  return startMatches[0] + line + endMatches[0];
@@ -668,26 +668,18 @@ var getColumnWidths = (config, rows) => {
668
668
  // src/render.ts
669
669
  import stripAnsi3 from "strip-ansi";
670
670
  var stringifyData = (config, inputData) => {
671
- const sections = {
672
- header: [],
673
- body: [],
674
- footer: []
675
- };
676
- const marginLeft = Array(config.marginLeft + 1).join(" ");
671
+ const sections = { header: [], body: [], footer: [] };
672
+ const marginLeft = " ".repeat(config.marginLeft);
677
673
  const borderStyle = config.borderCharacters[config.borderStyle];
678
674
  const borders = [];
679
675
  const constructorType = getConstructorGeometry(inputData[0] || [], config);
680
676
  const rows = coerceConstructorGeometry(config, inputData, constructorType);
681
- if (!global.columnWidths) {
682
- global.columnWidths = {};
683
- }
677
+ if (!global.columnWidths) global.columnWidths = {};
684
678
  if (global.columnWidths[config.tableId]) {
685
679
  config.table.columnWidths = global.columnWidths[config.tableId];
686
680
  } else {
687
681
  const formattedRows = rows.map((row, rowIndex) => {
688
- return row.map((cell, cellIndex) => {
689
- return buildCell(config, cell, cellIndex, "body", rowIndex, rows, inputData, true);
690
- });
682
+ return row.map((cell, cellIndex) => buildCell(config, cell, cellIndex, "body", rowIndex, rows, inputData, true));
691
683
  });
692
684
  global.columnWidths[config.tableId] = config.table.columnWidths = getColumnWidths(config, formattedRows);
693
685
  }
@@ -696,60 +688,49 @@ var stringifyData = (config, inputData) => {
696
688
  sections.header = [];
697
689
  break;
698
690
  case config.showHeader === true:
699
- // explicitly true, show
700
691
  case !!config.table.header[0].find((obj) => obj.value || obj.alias):
701
- sections.header = config.table.header.map((row) => {
702
- return buildRow(config, row, "header", null, rows, inputData);
703
- });
692
+ sections.header = config.table.header.map((row) => buildRow(config, row, "header", null, rows, inputData));
704
693
  break;
705
694
  default:
706
695
  sections.header = [];
707
696
  }
708
- sections.body = rows.map((row, rowIndex) => {
709
- return buildRow(config, row, "body", rowIndex, rows, inputData);
710
- });
697
+ sections.body = rows.map((row, rowIndex) => buildRow(config, row, "body", rowIndex, rows, inputData));
711
698
  sections.footer = config.table.footer instanceof Array && config.table.footer.length > 0 ? [config.table.footer] : [];
712
- sections.footer = sections.footer.map((row) => {
713
- return buildRow(config, row, "footer", null, rows, inputData);
714
- });
699
+ sections.footer = sections.footer.map((row) => buildRow(config, row, "footer", null, rows, inputData));
715
700
  for (let a = 0; a < 3; a++) {
716
701
  borders[a] = borderStyle[a].l;
717
702
  config.table.columnWidths.forEach((columnWidth, index, arr) => {
718
- borders[a] += Array(Math.max(columnWidth, 2)).join(borderStyle[a].h);
703
+ borders[a] += borderStyle[a].h.repeat(Math.max(columnWidth, 2) - 1);
719
704
  borders[a] += index + 1 < arr.length ? borderStyle[a].j : "";
720
705
  });
721
706
  borders[a] += borderStyle[a].r;
722
707
  borders[a] = a < 2 ? `${marginLeft + borders[a]}
723
708
  ` : marginLeft + borders[a];
724
709
  }
725
- let output = borders[0];
726
- Object.keys(sections).forEach((p, i) => {
727
- while (sections[p].length) {
728
- const row = sections[p].shift();
710
+ const output = [borders[0]];
711
+ for (const [sectionIndex, sectionName] of Object.keys(sections).entries()) {
712
+ const section = sections[sectionName];
713
+ for (let rowIndex = 0; rowIndex < section.length; rowIndex++) {
714
+ const row = section[rowIndex];
729
715
  row.forEach((line) => {
730
- output = `${output + marginLeft + borderStyle[1].v + line.join(borderStyle[1].v) + borderStyle[1].v}
731
- `;
716
+ output.push(marginLeft + borderStyle[1].v + line.join(borderStyle[1].v) + borderStyle[1].v + "\n");
732
717
  });
733
718
  switch (true) {
734
- // skip if end of body and no footer
735
- case (sections[p].length === 0 && i === 1 && sections.footer.length === 0):
719
+ case (rowIndex === section.length - 1 && sectionIndex === 1 && sections.footer.length === 0):
736
720
  break;
737
- // skip if end of footer
738
- case (sections[p].length === 0 && i === 2):
721
+ case (rowIndex === section.length - 1 && sectionIndex === 2):
739
722
  break;
740
- // skip if compact
741
- case (config.compact && p === "body" && !row.empty):
723
+ case (config.compact && sectionName === "body" && !row.empty):
742
724
  break;
743
- // skip if border style is "none"
744
725
  case (config.borderStyle === "none" && config.compact):
745
726
  break;
746
727
  default:
747
- output += borders[1];
728
+ output.push(borders[1]);
748
729
  }
749
730
  }
750
- });
751
- output += borders[2];
752
- const finalOutput = Array(config.marginTop + 1).join("\n") + output;
731
+ }
732
+ output.push(borders[2]);
733
+ const finalOutput = "\n".repeat(config.marginTop) + output.join("");
753
734
  config.height = finalOutput.split(/\r\n|\r|\n/).length;
754
735
  return finalOutput;
755
736
  };
@@ -760,11 +741,8 @@ var buildRow = (config, row, rowType, rowIndex, rowData, inputData) => {
760
741
  return row;
761
742
  }
762
743
  const lengthDifference = config.table.columnWidths.length - row.length;
763
- if (lengthDifference > 0) {
764
- row = row.concat(Array.apply(null, new Array(lengthDifference)).map(() => null));
765
- } else if (lengthDifference < 0) {
766
- row.length = config.table.columnWidths.length;
767
- }
744
+ if (lengthDifference > 0) row = row.concat(Array.apply(null, new Array(lengthDifference)).map(() => null));
745
+ else if (lengthDifference < 0) row.length = config.table.columnWidths.length;
768
746
  row = row.map((elem, elemIndex) => {
769
747
  const cell = buildCell(config, elem, elemIndex, rowType, rowIndex, rowData, inputData);
770
748
  minRowHeight = minRowHeight < cell.length ? cell.length : minRowHeight;
@@ -773,18 +751,12 @@ var buildRow = (config, row, rowType, rowIndex, rowData, inputData) => {
773
751
  minRowHeight = rowType === "header" ? minRowHeight : minRowHeight + (config.paddingBottom + config.paddingTop);
774
752
  const linedRow = Array.apply(null, { length: minRowHeight }).map(Function.call, () => []);
775
753
  row.forEach(function(cell, a) {
776
- const whitespace = Array(config.table.columnWidths[a]).join(" ");
754
+ const whitespace = " ".repeat(Math.max(config.table.columnWidths[a] - 1, 0));
777
755
  if (rowType === "body") {
778
- for (let i = 0; i < config.paddingTop; i++) {
779
- cell.unshift(whitespace);
780
- }
781
- for (let i = 0; i < config.paddingBottom; i++) {
782
- cell.push(whitespace);
783
- }
784
- }
785
- for (let i = 0; i < minRowHeight; i++) {
786
- linedRow[i].push(typeof cell[i] !== "undefined" ? cell[i] : whitespace);
756
+ for (let i = 0; i < config.paddingTop; i++) cell.unshift(whitespace);
757
+ for (let i = 0; i < config.paddingBottom; i++) cell.push(whitespace);
787
758
  }
759
+ for (let i = 0; i < minRowHeight; i++) linedRow[i].push(typeof cell[i] !== "undefined" ? cell[i] : whitespace);
788
760
  });
789
761
  return linedRow;
790
762
  };
@@ -803,9 +775,7 @@ var buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputDat
803
775
  switch (true) {
804
776
  case (typeof elem === "undefined" || elem === null):
805
777
  cellValue = config.errorOnNull ? config.defaultErrorValue : config.defaultValue;
806
- if (!isColorEnabled()) {
807
- cellValue = stripAnsi3(cellValue);
808
- }
778
+ if (!isColorEnabled()) cellValue = stripAnsi3(cellValue);
809
779
  cellOptions.isNull = true;
810
780
  break;
811
781
  case (typeof elem === "object" && elem !== null && typeof elem.value !== "undefined"):
@@ -818,13 +788,7 @@ var buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputDat
818
788
  },
819
789
  style,
820
790
  resetStyle
821
- })(
822
- cellValue,
823
- columnIndex,
824
- rowIndex,
825
- rowData,
826
- inputData
827
- );
791
+ })(cellValue, columnIndex, rowIndex, rowData, inputData);
828
792
  break;
829
793
  default:
830
794
  cellValue = elem;
@@ -836,25 +800,13 @@ var buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputDat
836
800
  },
837
801
  style,
838
802
  resetStyle
839
- })(
840
- cellValue,
841
- columnIndex,
842
- rowIndex,
843
- rowData,
844
- inputData
845
- );
803
+ })(cellValue, columnIndex, rowIndex, rowData, inputData);
846
804
  }
847
- if (dryRun) {
848
- return cellValue;
849
- }
850
- }
851
- if (!cellOptions.reset) {
852
- cellValue = colorizeCell(cellValue, cellOptions, rowType);
805
+ if (dryRun) return cellValue;
853
806
  }
807
+ if (!cellOptions.reset) cellValue = colorizeCell(cellValue, cellOptions, rowType);
854
808
  const { cell, innerWidth } = wrapCellText(cellOptions, cellValue, columnIndex, cellOptions, rowType);
855
- if (rowType === "header") {
856
- config.table.columnInnerWidths.push(innerWidth);
857
- }
809
+ if (rowType === "header") config.table.columnInnerWidths.push(innerWidth);
858
810
  return cell;
859
811
  };
860
812
  var getConstructorGeometry = (row, config) => {
@@ -863,17 +815,9 @@ var getConstructorGeometry = (row, config) => {
863
815
  const keys = Object.keys(row);
864
816
  if (config.adapter === "automattic") {
865
817
  const key = keys[0];
866
- if (row[key] instanceof Array) {
867
- type = "automattic-cross";
868
- } else {
869
- type = "automattic-vertical";
870
- }
871
- } else {
872
- type = "o-horizontal";
873
- }
874
- } else {
875
- type = "a-horizontal";
876
- }
818
+ type = row[key] instanceof Array ? "automattic-cross" : "automattic-vertical";
819
+ } else type = "o-horizontal";
820
+ } else type = "a-horizontal";
877
821
  return type;
878
822
  };
879
823
  var coerceConstructorGeometry = (config, rows, constructorType) => {
@@ -883,16 +827,14 @@ var coerceConstructorGeometry = (config, rows, constructorType) => {
883
827
  config.columnSettings[0] = config.columnSettings[0] || {};
884
828
  config.columnSettings[0].color = config.headerColor;
885
829
  output = rows.map((obj) => {
886
- const arr = [];
887
830
  const key = Object.keys(obj)[0];
888
- arr.push(key);
889
- return arr.concat(obj[key]);
831
+ return [key].concat(obj[key]);
890
832
  });
891
833
  break;
892
834
  case "automattic-vertical":
893
835
  config.columnSettings[0] = config.columnSettings[0] || {};
894
836
  config.columnSettings[0].color = config.headerColor;
895
- output = rows.map(function(value) {
837
+ output = rows.map((value) => {
896
838
  const key = Object.keys(value)[0];
897
839
  return [key, value[key]];
898
840
  });
@@ -900,14 +842,11 @@ var coerceConstructorGeometry = (config, rows, constructorType) => {
900
842
  case "o-horizontal":
901
843
  if (config.table.header[0].length && config.table.header[0].every((obj) => obj.value)) {
902
844
  output = rows.map((row) => config.table.header[0].map((obj) => row[obj.value]));
903
- } else {
904
- output = rows.map((obj) => Object.values(obj));
905
- }
845
+ } else output = rows.map((obj) => Object.values(obj));
906
846
  break;
907
847
  case "a-horizontal":
908
848
  output = rows;
909
849
  break;
910
- default:
911
850
  }
912
851
  return output;
913
852
  };