Node.js formatted console output

China☆狼群 提交于 2019-12-31 09:11:07

问题


Is there a simple built-in way to output formatted data to console in Node.js?

Indent, align field to left or right, add leading zeros?


回答1:


Two new(1) built in methods String.Prototype.padStart and String.Prototype.padEnd were introduced in ES2017 (ES8) which perform the required padding functions.

(1) node >= 8.2.1 (or >= 7.5.0 if run with the --harmony flag)

Examples from the mdn page:

'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.stringify({ a:1, b:2, c:3 }, null, 4);
// {
//    "a": 1,
//    "b": 2,
//    "c": 3
// }



回答2:


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).

You'll need to look into other modules to provide a richer formatting experience. For example: sprintf.

Sprintf-js allows both positional (0, 1, 2) arguments and named arguments.

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    



回答3:


If the data is tabular, then the simplest way would be to do it with console.table

https://nodejs.org/dist/latest-v10.x/docs/api/console.html#console_console_table_tabulardata_properties

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.

https://developer.mozilla.org/en-US/docs/Web/API/Console/table




回答4:


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 sprintf package or sprintf-js package.




回答5:


Take a look at Log4JS, which is an attempt at a functional port of Log4j




回答6:


You might also like string-kit and terminal-kit.

https://www.npmjs.com/package/string-kit

https://www.npmjs.com/package/terminal-kit

https://blog.soulserv.net/terminal-friendly-application-with-node-js-part-ii-moving-and-editing/



来源:https://stackoverflow.com/questions/19837697/node-js-formatted-console-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!