Node.js 格式的控制台输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19837697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js formatted console output
提问by exebook
Is there a simple built-in way to output formatted data to console in Node.js?
是否有一种简单的内置方法可以将格式化数据输出到 Node.js 中的控制台?
Indent, align field to left or right, add leading zeros?
缩进,向左或向右对齐字段,添加前导零?
采纳答案by TheChetan
Two new(1) built in methods String.Prototype.padStartand String.Prototype.padEndwere introduced in ES2017 (ES8) which perform the required padding functions.
ES2017 (ES8) 中引入了两个 new(1) 内置方法String.Prototype.padStart和String.Prototype.padEnd,它们执行所需的填充功能。
(1) node >= 8.2.1 (or >= 7.5.0 if run with the --harmony flag)
(1)节点 >= 8.2.1(或 >= 7.5.0 如果使用 --harmony 标志运行)
Examples from the mdn page:
来自 mdn 页面的示例:
'abc'.padStart(10); // " abc"
'abc'.padStart(10, "foo"); // "foofoofabc"
'abc'.padStart(6,"123465"); // "123abc"
'abc'.padStart(8, "0"); // "00000abc"
'abc'.padStart(1); // "abc"
'abc'.padEnd(10); // "abc "
'abc'.padEnd(10, "foo"); // "abcfoofoof"
'abc'.padEnd(6, "123456"); // "abc123"
'abc'.padEnd(1); // "abc"
For indenting a json onto the console try using JSON.stringify. The third parameter provides the indention required.
要将json 缩进到控制台,请尝试使用JSON.stringify。第三个参数提供所需的缩进。
JSON.stringify({ a:1, b:2, c:3 }, null, 4);
// {
// "a": 1,
// "b": 2,
// "c": 3
// }
回答by WiredPrairie
There's nothing built into NodeJS to do this. The "closest" you'd come is util.format, which still doesn't do much unfortunately (reference).
NodeJS 没有内置任何东西来做到这一点。你来的“最接近”是util.format,不幸的是它仍然没有做太多(参考)。
You'll need to look into other modules to provide a richer formatting experience. For example: sprintf.
您需要研究其他模块以提供更丰富的格式化体验。例如:sprintf。
Sprintf-js allows both positional (0, 1, 2) arguments and named arguments.
Sprintf-js 允许位置 (0, 1, 2) 参数和命名参数。
A few examples of padding and alignment:
填充和对齐的几个例子:
var sprintf=require("sprintf-js").sprintf;
console.log(sprintf("Space Padded => %10.2f", 123.4567));
console.log(sprintf(" _ Padded => %'_10.2f", 123.4567));
console.log(sprintf(" 0 Padded => %010.2f", 123.4567));
console.log(sprintf(" Left align => %-10.2f", 123.4567));
Results:
结果:
Space Padded => 123.46
_ Padded => ____123.46
0 Padded => 0000123.46
Left align => 123.46
回答by DivineCoder
If the data is tabular, then the simplest way would be to do it with console.table
如果数据是表格形式,那么最简单的方法是使用 console.table
This is the code.
这是代码。
console.table(
COMMANDS.map(command => {
return {
"Long Option": command.long_option,
"Short Option": command.short_option,
Description: command.description
};
})
);
You don't need external libraries for it.
Here is sample output. You only need to pass an array object.

Not only in Nodejs, but it also works in chrome.
不仅在 Nodejs 中,它也适用于 chrome。
https://developer.mozilla.org/en-US/docs/Web/API/Console/table
https://developer.mozilla.org/en-US/docs/Web/API/Console/table
回答by user568109
If you have simpler needs you can look into util.format. It can generate string from various parameters. If you want printf like formatting you can use either sprintfpackage or sprintf-jspackage.
如果您有更简单的需求,您可以查看util.format。它可以从各种参数生成字符串。如果您想要 printf 之类的格式,您可以使用sprintf包或sprintf-js包。
回答by LeeGee
回答by jjstiff
You might also like string-kit and terminal-kit.
您可能还喜欢 string-kit 和 terminal-kit。
https://www.npmjs.com/package/string-kit
https://www.npmjs.com/package/string-kit
https://www.npmjs.com/package/terminal-kit
https://www.npmjs.com/package/terminal-kit
https://blog.soulserv.net/terminal-friendly-application-with-node-js-part-ii-moving-and-editing/
https://blog.soulserv.net/terminal-friendly-application-with-node-js-part-ii-moving-and-editing/

