Movatterモバイル変換


[0]ホーム

URL:


columnify
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/columnify package

1.6.0 • Public • Published

columnify

Columnify Unit TestsNPM VersionLicense

Create text-based columns suitable for console output from objects orarrays of objects.

Columns are automatically resized to fit the content of the largestcell. Each cell will be padded with spaces to fill the available spaceand ensure column contents are left-aligned.

Designed tohandle sensible wrapping in npm search results.

npm search before & after integrating columnify:

npm-tidy-search

Installation

$ npm install columnify

Usage

varcolumnify=require('columnify')varcolumns=columnify(data,options)console.log(columns)

Examples

Columnify Objects

Objects are converted to a list of key/value pairs:

vardata={"commander@0.6.1":1,"minimatch@0.2.14":3,"mkdirp@0.3.5":2,"sigmund@1.0.0":3}console.log(columnify(data))

Output:

KEY               VALUEcommander@0.6.1   1minimatch@0.2.14  3mkdirp@0.3.5      2sigmund@1.0.0     3

Custom Column Names

vardata={"commander@0.6.1":1,"minimatch@0.2.14":3,"mkdirp@0.3.5":2,"sigmund@1.0.0":3}console.log(columnify(data,{columns:['MODULE','COUNT']}))

Output:

MODULE            COUNTcommander@0.6.1   1minimatch@0.2.14  3mkdirp@0.3.5      2sigmund@1.0.0     3

Columnify Arrays of Objects

Column headings are extracted from the keys in supplied objects.

varcolumnify=require('columnify')varcolumns=columnify([{name:'mod1',version:'0.0.1'},{name:'module2',version:'0.2.0'}])console.log(columns)

Output:

NAME    VERSIONmod1    0.0.1  module2 0.2.0

Filtering & Ordering Columns

By default, all properties are converted into columns, whether or notthey exist on every object or not.

To explicitly specify which columns to include, and in which order,supply a "columns" or "include" array ("include" is just an alias).

vardata=[{name:'module1',description:'some description',version:'0.0.1',},{name:'module2',description:'another description',version:'0.2.0',}]varcolumns=columnify(data,{columns:['name','version']})console.log(columns)

Output:

NAME    VERSIONmodule1 0.0.1module2 0.2.0

Global and Per Column Options

You can set a number of options at a global level (ie. for all columns) or on a per column basis.

Set options on a per column basis by using theconfig option to specify individual columns:

varcolumns=columnify(data,{optionName:optionValue,config:{columnName:{optionName:optionValue},columnName:{optionName:optionValue},}})

Maximum and Minimum Column Widths

As with all options, you can define themaxWidth andminWidth globally, or for specified columns. By default, wrapping will happen at word boundaries. Empty cells or those which do not fill theminWidth will be padded with spaces.

varcolumns=columnify([{name:'mod1',description:'some description which happens to be far larger than the max',version:'0.0.1',},{name:'module-two',description:'another description larger than the max',version:'0.2.0',}],{minWidth:20,config:{description:{maxWidth:30}}})console.log(columns)

Output:

NAME                 DESCRIPTION                    VERSION             mod1                 some description which happens 0.0.1                                    to be far larger than the max                      module-two           another description larger     0.2.0                                    than the max

Maximum Line Width

You can set a hard maximum line width using themaxLineWidth option.Beyond this value data is unceremoniously truncated with no truncationmarker.

This can either be a number or 'auto' to set the value to the width ofstdout.

Setting this value to 'auto' prevent TTY-imposed line-wrapping whenlines exceed the screen width.

Truncating Column Cells Instead of Wrapping

You can disable wrapping and instead truncate content at the maximumcolumn width by using thetruncate option. Truncation respects word boundaries. A truncation marker,, will appear next to the last word in any truncated line.

varcolumns=columnify(data,{truncate:true,config:{description:{maxWidth:20}}})console.log(columns)

Output:

NAME       DESCRIPTION          VERSIONmod1       some description…    0.0.1  module-two another description… 0.2.0

Align Right/Center

You can set the alignment of the column data by using thealign option.

vardata={"mocha@1.18.2":1,"commander@2.0.0":1,"debug@0.8.1":1}columnify(data,{config:{value:{align:'right'}}})

Output:

KEY                  VALUEmocha@1.18.2             1commander@2.0.0          1debug@0.8.1              1

align: 'center' works in a similar way.

Padding Character

Set a character to fill whitespace within columns with thepaddingChr option.

vardata={"shortKey":"veryVeryVeryVeryVeryLongVal","veryVeryVeryVeryVeryLongKey":"shortVal"}columnify(data,{paddingChr:'.'})

Output:

KEY........................ VALUE......................shortKey................... veryVeryVeryVeryVeryLongValveryVeryVeryVeryVeryLongKey shortVal...................

Preserve Existing Newlines

By default,columnify sanitises text by replacing any occurance of 1 or more whitespace characters with a single space.

columnify can be configured to respect existing new line characters using thepreserveNewLines option. Note this will still collapse all other whitespace.

vardata=[{name:"glob@3.2.9",paths:["node_modules/tap/node_modules/glob","node_modules/tape/node_modules/glob"].join('\n')},{name:"nopt@2.2.1",paths:["node_modules/tap/node_modules/nopt"]},{name:"runforcover@0.0.2",paths:"node_modules/tap/node_modules/runforcover"}]console.log(columnify(data,{preserveNewLines:true}))

Output:

NAME              PATHSglob@3.2.9        node_modules/tap/node_modules/glob                  node_modules/tape/node_modules/globnopt@2.2.1        node_modules/tap/node_modules/noptrunforcover@0.0.2 node_modules/tap/node_modules/runforcover

Compare this with output withoutpreserveNewLines:

console.log(columnify(data,{preserveNewLines:false}))// or justconsole.log(columnify(data))
NAME              PATHSglob@3.2.9        node_modules/tap/node_modules/glob node_modules/tape/node_modules/globnopt@2.2.1        node_modules/tap/node_modules/noptrunforcover@0.0.2 node_modules/tap/node_modules/runforcover

Custom Truncation Marker

You can change the truncation marker to something other than the default by using thetruncateMarker option.

varcolumns=columnify(data,{truncate:true,truncateMarker:'>',widths:{description:{maxWidth:20}}})console.log(columns)

Output:

NAME       DESCRIPTION          VERSIONmod1       some description>    0.0.1  module-two another description> 0.2.0

Custom Column Splitter

If your columns need some bling, you can split columns with customcharacters by using thecolumnSplitter option.

varcolumns=columnify(data,{columnSplitter:' | '})console.log(columns)

Output:

NAME       | DESCRIPTION                                                  | VERSIONmod1       | some description which happens to be far larger than the max | 0.0.1module-two | another description larger than the max                      | 0.2.0

Control Header Display

Control whether column headers are displayed by using theshowHeaders option.

varcolumns=columnify(data,{showHeaders:false})

This also works well for hiding a single column header, like anid column:

varcolumns=columnify(data,{config:{id:{showHeaders:false}}})

Transforming Column Data and Headers

If you need to modify the presentation of column content or heading content there are two useful options for doing that:dataTransform andheadingTransform. Both of these take a function and need to return a valid string.

varcolumns=columnify([{name:'mod1',description:'SOME DESCRIPTION TEXT.'},{name:'module-two',description:'SOME SLIGHTLY LONGER DESCRIPTION TEXT.'}],{dataTransform:function(data){returndata.toLowerCase()},headingTransform:function(heading){returnheading.toLowerCase()},config:{name:{headingTransform:function(heading){heading="module "+headingreturn"*"+heading.toUpperCase()+"*"}}}})

Output:

*MODULE NAME* description                           mod1          some description text.                module-two    some slightly longer description text.

Multibyte Character Support

columnify usesmycoboco/wcwidth.js to calculate length of multibyte characters:

vardata=[{name:'module-one',description:'some description',version:'0.0.1',},{name:'这是一个很长的名字的模块',description:'这真的是一个描述的内容这个描述很长',version:"0.3.3"}]console.log(columnify(data))

Without multibyte handling:

i.e. before columnify added this feature

NAME         DESCRIPTION       VERSIONmodule-one   some description  0.0.1这是一个很长的名字的模块 这真的是一个描述的内容这个描述很长 0.3.3

With multibyte handling:

NAME                     DESCRIPTION                        VERSIONmodule-one               some description                   0.0.1这是一个很长的名字的模块 这真的是一个描述的内容这个描述很长 0.3.3

Contributions

 project  : columnify repo age : 8 years active   : 47 days commits  : 180 files    : 57 authors  :   123Tim Oxley           68.3%    11Nicholas Hoffman    6.1%     8Tim                 4.4%     7Arjun Mehta         3.9%     6Dany                3.3%     5Tim Kevin Oxley     2.8%     5Wei Gao             2.8%     4Matias Singers      2.2%     3Michael Kriese      1.7%     2sreekanth370        1.1%     2Dany Shaanan        1.1%     1Tim Malone          0.6%     1Seth Miller         0.6%     1andyfusniak         0.6%     1Isaac Z. Schlueter  0.6%

License

MIT

Package Sidebar

Install

npm i columnify

Weekly Downloads

3,633,001

Version

1.6.0

License

MIT

Unpacked Size

38.8 kB

Total Files

9

Last publish

Collaborators

  • timoxley

[8]ページ先頭

©2009-2025 Movatter.jp