Skip to content

Commit

Permalink
fix: make left default alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Oct 20, 2021
1 parent 8bdf4bd commit 06567e4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface MarkdownTableOptions {
const DEFAULT_TABLE_OPTIONS: MarkdownTableOptions = {};

export function csvToTable(csv: CSV, options: Omit<MarkdownTableOptions, 'pretty'> = {}): MarkdownTable {
const { columns, exclude, alignment: _alignment = 'center' } = options;
const { columns, exclude, alignment: _alignment = 'left' } = options;
const data: CSV = [...csv];
const dataHeaders = data.shift() as CSVHeader;

Expand Down Expand Up @@ -85,8 +85,8 @@ function padColumn(column: string, length: number, alignment: TableAlignment): s
case 'left':
return column.padEnd(length, ' ');
case 'center':
const left = Math.ceil(length / 2);
return column.padStart(left, ' ').padEnd(length, ' ');
const right = column.length + Math.ceil((length - column.length) / 2);
column = column.padEnd(right, ' ');
case 'right':
return column.padStart(length, ' ');
}
Expand Down
30 changes: 15 additions & 15 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('test exported library', () => {
{ a: 4, b: 5, c: 6 },
];
const result = generateMarkdownTable(json);
expect(result).toStrictEqual('| a | b | c |\n| :---: | :---: | :---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
expect(result).toStrictEqual('| a | b | c |\n| :--- | :--- | :--- |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
});

test('should generate pretty table from json', () => {
Expand All @@ -18,7 +18,7 @@ describe('test exported library', () => {
];
const result = generateMarkdownTable(json, { pretty: true });
expect(result).toStrictEqual(
'| a | b | c |\n| :---: | :---: | :---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |'
'| a | b | c |\n| :--- | :--- | :--- |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |'
);
});

Expand All @@ -29,7 +29,7 @@ describe('test exported library', () => {
];
const result = generateMarkdownTable(json, true);
expect(result).toStrictEqual(
'| a | b | c |\n| :---: | :---: | :---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |'
'| a | b | c |\n| :--- | :--- | :--- |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |'
);
});

Expand All @@ -40,7 +40,7 @@ describe('test exported library', () => {
];
const result = generateMarkdownTable(['a', 'b', 'c'], csv, true);
expect(result).toStrictEqual(
'| a | b | c |\n| :---: | :---: | :---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |'
'| a | b | c |\n| :--- | :--- | :--- |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |'
);
});

Expand All @@ -65,13 +65,13 @@ describe('test exported library', () => {
['4', '5', '6'],
];
const result = generateMarkdownTable(csv);
expect(result).toStrictEqual('| a | b | c |\n| :---: | :---: | :---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
expect(result).toStrictEqual('| a | b | c |\n| :--- | :--- | :--- |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
});

test('should generate table from csv string', () => {
const csv = 'a,b,c\n1,2,3\n4,5,6';
const result = generateMarkdownTable(csv);
expect(result).toStrictEqual('| a | b | c |\n| :---: | :---: | :---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
expect(result).toStrictEqual('| a | b | c |\n| :--- | :--- | :--- |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
});

test('should generate table from headers and csv', () => {
Expand All @@ -80,13 +80,13 @@ describe('test exported library', () => {
[4, 5, 6],
];
const result = generateMarkdownTable(['a', 'b', 'c'], csv);
expect(result).toStrictEqual('| a | b | c |\n| :---: | :---: | :---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
expect(result).toStrictEqual('| a | b | c |\n| :--- | :--- | :--- |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
});

test('should generate table from headers and csv string', () => {
const csv = '1,2,3\n4,5,6';
const result = generateMarkdownTable(['a', 'b', 'c'], csv);
expect(result).toStrictEqual('| a | b | c |\n| :---: | :---: | :---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
expect(result).toStrictEqual('| a | b | c |\n| :--- | :--- | :--- |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |');
});

test('should respect include columns', () => {
Expand All @@ -95,7 +95,7 @@ describe('test exported library', () => {
{ a: 4, b: 5, c: 6 },
];
const result = generateMarkdownTable(json, { columns: ['a', 'c', 'd'] });
expect(result).toStrictEqual('| a | c | d |\n| :---: | :---: | :---: |\n| 1 | 3 | |\n| 4 | 6 | |');
expect(result).toStrictEqual('| a | c | d |\n| :--- | :--- | :--- |\n| 1 | 3 | |\n| 4 | 6 | |');
});

test('should respect exclude columns', () => {
Expand All @@ -104,7 +104,7 @@ describe('test exported library', () => {
{ a: 4, b: 5, c: 6 },
];
const result = generateMarkdownTable(json, { exclude: ['b'] });
expect(result).toStrictEqual('| a | c |\n| :---: | :---: |\n| 1 | 3 |\n| 4 | 6 |');
expect(result).toStrictEqual('| a | c |\n| :--- | :--- |\n| 1 | 3 |\n| 4 | 6 |');
});

test('should throw error on unsupported parameters', () => {
Expand All @@ -127,7 +127,7 @@ describe('test exported library', () => {
const options: MarkdownTableOptions = { exclude: ['b'] };
const md = generateMarkdownTable(csv, options);

expect(md).toStrictEqual('| a | c |\n| :---: | :---: |\n| 1 | 3 |\n| 4 | 6 |');
expect(md).toStrictEqual('| a | c |\n| :--- | :--- |\n| 1 | 3 |\n| 4 | 6 |');
});

test('should work with boolean values', () => {
Expand All @@ -136,7 +136,7 @@ describe('test exported library', () => {
[true, false],
];
const result = generateMarkdownTable(csv);
expect(result).toStrictEqual('| a | b |\n| :---: | :---: |\n| true | false |');
expect(result).toStrictEqual('| a | b |\n| :--- | :--- |\n| true | false |');
});

test('should work with numbers', () => {
Expand All @@ -145,7 +145,7 @@ describe('test exported library', () => {
[1, 2],
];
const result = generateMarkdownTable(csv);
expect(result).toStrictEqual('| a | b |\n| :---: | :---: |\n| 1 | 2 |');
expect(result).toStrictEqual('| a | b |\n| :--- | :--- |\n| 1 | 2 |');
});

test('should work with null and undefined', () => {
Expand All @@ -154,7 +154,7 @@ describe('test exported library', () => {
[null, undefined],
];
const result = generateMarkdownTable(csv);
expect(result).toStrictEqual('| a | b |\n| :---: | :---: |\n| | |');
expect(result).toStrictEqual('| a | b |\n| :--- | :--- |\n| | |');
});

test('should work with mixed types', () => {
Expand All @@ -164,6 +164,6 @@ describe('test exported library', () => {
[undefined, 1, ''],
];
const result = generateMarkdownTable(csv);
expect(result).toStrictEqual('| a | b | c |\n| :---: | :---: | :---: |\n| true | false | |\n| | 1 | |');
expect(result).toStrictEqual('| a | b | c |\n| :--- | :--- | :--- |\n| true | false | |\n| | 1 | |');
});
});
18 changes: 17 additions & 1 deletion test/md.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('test markdown functions', () => {
const descriptor = csvToTable(csv);

expect(descriptor).toStrictEqual({
alignment: ['center', 'center', 'center'],
alignment: ['left', 'left', 'left'],
headers: ['a', 'b', 'c'],
rows: [
['1', '2', '3'],
Expand Down Expand Up @@ -87,4 +87,20 @@ describe('test markdown functions', () => {
'| a | b | c |\n| :--- | :---: | ---: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |'
);
});

test('should align odd centered rows left', () => {
const table: MarkdownTable = {
alignment: ['center', 'center'],
headers: ['aaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbb'],
rows: [
['aaaa', 'bbbbb'],
['ccccc', 'dddd'],
],
};
const md = prettyPrintTable(table);

expect(md).toStrictEqual(
'| aaaaaaaaaaaaaaa | bbbbbbbbbbbbbbb |\n| :-------------: | :-------------: |\n| aaaa | bbbbb |\n| ccccc | dddd |'
);
});
});

0 comments on commit 06567e4

Please sign in to comment.