lib: align console.table row to the left

PR-URL: https://github.com/nodejs/node/pull/50135
Fixes: https://github.com/nodejs/node/issues/50117
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Jithil P Ponnan 2023-10-28 12:14:22 +11:00 committed by GitHub
parent e2cf2f0fda
commit 14af167209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 68 deletions

View File

@ -40,11 +40,10 @@ const renderRow = (row, columnWidths) => {
for (let i = 0; i < row.length; i++) { for (let i = 0; i < row.length; i++) {
const cell = row[i]; const cell = row[i];
const len = getStringWidth(cell); const len = getStringWidth(cell);
const needed = (columnWidths[i] - len) / 2; const needed = (columnWidths[i] - len);
// round(needed) + ceil(needed) will always add up to the amount // round(needed) + ceil(needed) will always add up to the amount
// of spaces we need while also left justifying the output. // of spaces we need while also left justifying the output.
out += StringPrototypeRepeat(' ', needed) + cell + out += cell + StringPrototypeRepeat(' ', MathCeil(needed));
StringPrototypeRepeat(' ', MathCeil(needed));
if (i !== row.length - 1) if (i !== row.length - 1)
out += tableChars.middle; out += tableChars.middle;
} }

View File

@ -38,19 +38,19 @@ test([1, 2, 3], `
(index) Values (index) Values
0 1 0 1
1 2 1 2
2 3 2 3
`); `);
test([Symbol(), 5, [10]], ` test([Symbol(), 5, [10]], `
(index) 0 Values (index) 0 Values
0 Symbol() 0 Symbol()
1 5 1 5
2 10 2 10
`); `);
@ -58,46 +58,46 @@ test([null, 5], `
(index) Values (index) Values
0 null 0 null
1 5 1 5
`); `);
test([undefined, 5], ` test([undefined, 5], `
(index) Values (index) Values
0 undefined 0 undefined
1 5 1 5
`); `);
test({ a: 1, b: Symbol(), c: [10] }, ` test({ a: 1, b: Symbol(), c: [10] }, `
(index) 0 Values (index) 0 Values
a 1 a 1
b Symbol() b Symbol()
c 10 c 10
`); `);
test(new Map([ ['a', 1], [Symbol(), [2]] ]), ` test(new Map([ ['a', 1], [Symbol(), [2]] ]), `
(iteration index) Key Values (iteration index) Key Values
0 'a' 1 0 'a' 1
1 Symbol() [ 2 ] 1 Symbol() [ 2 ]
`); `);
test(new Set([1, 2, Symbol()]), ` test(new Set([1, 2, Symbol()]), `
(iteration index) Values (iteration index) Values
0 1 0 1
1 2 1 2
2 Symbol() 2 Symbol()
`); `);
@ -105,8 +105,8 @@ test({ a: 1, b: 2 }, ['a'], `
(index) a (index) a
a a
b b
`); `);
@ -114,8 +114,8 @@ test([{ a: 1, b: 2 }, { a: 3, c: 4 }], ['a'], `
(index) a (index) a
0 1 0 1
1 3 1 3
`); `);
@ -123,9 +123,9 @@ test(new Map([[1, 1], [2, 2], [3, 3]]).entries(), `
(iteration index) Key Values (iteration index) Key Values
0 1 1 0 1 1
1 2 2 1 2 2
2 3 3 2 3 3
`); `);
@ -133,9 +133,9 @@ test(new Map([[1, 1], [2, 2], [3, 3]]).values(), `
(iteration index) Values (iteration index) Values
0 1 0 1
1 2 1 2
2 3 2 3
`); `);
@ -143,9 +143,9 @@ test(new Map([[1, 1], [2, 2], [3, 3]]).keys(), `
(iteration index) Values (iteration index) Values
0 1 0 1
1 2 1 2
2 3 2 3
`); `);
@ -153,9 +153,9 @@ test(new Set([1, 2, 3]).values(), `
(iteration index) Values (iteration index) Values
0 1 0 1
1 2 1 2
2 3 2 3
`); `);
@ -164,15 +164,15 @@ test({ a: { a: 1, b: 2, c: 3 } }, `
(index) a b c (index) a b c
a 1 2 3 a 1 2 3
`); `);
test({ a: { a: { a: 1, b: 2, c: 3 } } }, ` test({ a: { a: { a: 1, b: 2, c: 3 } } }, `
(index) a (index) a
a [Object] a [Object]
`); `);
@ -180,7 +180,7 @@ test({ a: [1, 2] }, `
(index) 0 1 (index) 0 1
a 1 2 a 1 2
`); `);
@ -188,9 +188,9 @@ test({ a: [1, 2, 3, 4, 5], b: 5, c: { e: 5 } }, `
(index) 0 1 2 3 4 e Values (index) 0 1 2 3 4 e Values
a 1 2 3 4 5 a 1 2 3 4 5
b 5 b 5
c 5 c 5
`); `);
@ -198,9 +198,9 @@ test(new Uint8Array([1, 2, 3]), `
(index) Values (index) Values
0 1 0 1
1 2 1 2
2 3 2 3
`); `);
@ -208,9 +208,9 @@ test(Buffer.from([1, 2, 3]), `
(index) Values (index) Values
0 1 0 1
1 2 1 2
2 3 2 3
`); `);
@ -218,7 +218,7 @@ test({ a: undefined }, ['x'], `
(index) x (index) x
a a
`); `);
@ -238,23 +238,23 @@ test(new Map(), `
test([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ` test([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], `
(index) a b (index) a b
0 1 'Y' 0 1 'Y'
1 'Z' 2 1 'Z' 2
`); `);
{ {
const line = '─'.repeat(79); const line = '─'.repeat(79);
const header = `${' '.repeat(37)}name${' '.repeat(40)}`; const header = `name${' '.repeat(77)}`;
const name = 'very long long long long long long long long long long long ' + const name = 'very long long long long long long long long long long long ' +
'long long long long'; 'long long long long';
test([{ name }], ` test([{ name }], `
${line} ${line}
(index) ${header} (index) ${header}
${line} ${line}
0 '${name}' 0 '${name}'
${line} ${line}
`); `);
} }
@ -263,8 +263,8 @@ test({ foo: '¥', bar: '¥' }, `
(index) Values (index) Values
foo '¥' foo '¥'
bar '¥' bar '¥'
`); `);
@ -272,8 +272,8 @@ test({ foo: '你好', bar: 'hello' }, `
(index) Values (index) Values
foo '你好' foo '你好'
bar 'hello' bar 'hello'
`); `);
@ -285,8 +285,8 @@ test([{ foo: 10 }, { foo: 20 }], ['__proto__'], `
(index) __proto__ (index) __proto__
0 0
1 1
`); `);
assert.strictEqual('0' in Object.prototype, false); assert.strictEqual('0' in Object.prototype, false);