Skip to content

Commit

Permalink
fix: Implement sum() once and correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Oct 28, 2024
1 parent 9f71de5 commit 901803a
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions table/_layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { Row, type RowType } from "./row.ts";
import type { BorderOptions, Table, TableSettings } from "./table.ts";
import { getUnclosedAnsiRuns, longest, strLength } from "./_utils.ts";

const sum = (numList: Array<number>): number =>
numList.reduce((a, b) => a + b, 0);

/** Layout render settings. */
interface RenderSettings {
padding: Array<number>;
Expand Down Expand Up @@ -89,9 +92,9 @@ export class TableLayout {
}

/* Try to get the total table width within a maximum */
const totalWidth = width.reduce((a, b) => a + b);
const totalWidth = sum(width);
const maxAllowable = this.options.maxTableWidth -
padding.filter((x) => x).reduce((a, b) => a + b);
sum(padding.filter((x) => x));
if (totalWidth > maxAllowable && this.options.colRigidity != 1) {
const rigidity = width.map(
(_w, i) => (
Expand All @@ -100,15 +103,11 @@ export class TableLayout {
: this.options.colRigidity
),
);
const rigidTotal = width.map((w, i) => rigidity[i] >= 1 ? w : 0).reduce(
(a, b) => a + b,
);
const rigidTotal = sum(width.map((w, i) => rigidity[i] >= 1 ? w : 0));

const slack = maxAllowable - rigidTotal;
if (slack > 0) {
const flexTotal = width.map((w, i) => rigidity[i] < 1 ? w : 0).reduce(
(a, b) => a + b,
);
const flexTotal = sum(width.map((w, i) => rigidity[i] < 1 ? w : 0));
const flexFactor = slack / flexTotal;
for (let colIndex = 0; colIndex < width.length; colIndex++) {
if (rigidity[colIndex] < 1) {
Expand Down

0 comments on commit 901803a

Please sign in to comment.