Skip to content

Minor defect in alignCenter logic #115

@michaelgwelch

Description

@michaelgwelch

On line 38 you should be checking (I believe) that width is even, not half-width.

table/src/alignString.js

Lines 33 to 45 in 37fa97c

const alignCenter = (subject, width) => {
let halfWidth;
halfWidth = width / 2;
if (halfWidth % 2 === 0) {
return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
} else {
halfWidth = Math.floor(halfWidth);
return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
}
};

Here's a little program to illustrate the issue.

#!/usr/bin/env node
const { table } = require('table');

const data = [
    ["ten characters"],
    ["ten characte"]
]

const config = {
    columns: {
        0: {
            alignment: "center"
        }
    }
}

const output = table(data, config);
console.log(output);

And here's the output

> ./index.js 
╔════════════════╗
║ ten characters ║
╟────────────────╢
║  ten characte   ║
╚════════════════╝

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions