Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
The columnify npm package is a utility that allows for formatting console output into a columnar data format. It is particularly useful for creating readable tables out of JSON data or arrays of objects. With columnify, you can easily align text in columns, customize column widths, and manage how text wraps within columns.
Basic Column Formatting
This feature allows for the simple transformation of an array of objects into a neatly formatted table. Each object property becomes a column.
const columnify = require('columnify')
const data = [{ name: 'John', age: 22 }, { name: 'Jane', age: 33 }]
console.log(columnify(data))
Custom Column Widths
This feature enables the customization of column widths, allowing for a uniform appearance or to accommodate longer text without breaking the layout.
const columnify = require('columnify')
const data = [{ name: 'John', age: 22 }, { name: 'Jane', age: 33 }]
console.log(columnify(data, { minWidth: 20 }))
Column Alignment
This feature allows for the alignment of text within columns. In this example, the 'age' column is right-aligned, enhancing the readability of numerical data.
const columnify = require('columnify')
const data = [{ name: 'John', age: 22, location: 'New York' }, { name: 'Jane', age: 33, location: 'California' }]
console.log(columnify(data, { config: { age: { align: 'right' } } }))
cli-table is a similar package that provides functionalities for rendering unicode-aided tables on the command line. Compared to columnify, cli-table offers more customization options for borders and styles but might require more setup for simple tasks.
The 'table' package is another alternative that allows for creating text-based tables with customizable padding, margin, and border styles. It supports content wrapping and column width management similar to columnify but with additional features like text truncation and alignment.
Create text-based columns suitable for console output. Supports minimum and maximum column widths via truncation and text wrapping.
Designed to handle sensible wrapping in npm search results.
$ npm install --save columnify@latest
var columnify = require('columnify')
var columns = columnify(data, options)
console.log(columns)
Text is aligned under column headings. Columns are automatically resized to fit the content of the largest cell. Each cell will be padded with spaces to fill the available space and ensure column contents are left-aligned.
var columnify = require('columnify')
var columns = columnify([{
name: 'mod1',
version: '0.0.1'
}, {
name: 'module2',
version: '0.2.0'
}])
console.log(columns)
NAME VERSION
mod1 0.0.1
module2 0.2.0
You can define the maximum width before wrapping for individual cells in columns. Minimum width is also supported. Wrapping will happen at word boundaries. Empty cells or those which do not fill the max/min width will be padded with spaces.
var columnify = require('columnify')
var columns = 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',
})
console.log(columns)
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
You can disable wrapping and instead truncate content at the maximum column width. Truncation respects word boundaries.
var columns = columnify(data, {
truncate: true,
widths: {
description: {
maxWidth: 19
}
}
})
console.log(columns)
NAME DESCRIPTION VERSION
mod1 some description 0.0.1
module-two another description 0.2.0
If your columns need some bling, you can split columns with custom characters.
var columns = columnify(data, {
columnSplitter: ' | '
})
console.log(columns)
NAME | DESCRIPTION | VERSION
mod1 | some description which happens to be far larger than the max | 0.0.1
module-two | another description larger than the max | 0.2.0
By default, all properties are converted into columns, whether or not they exist on every object or not.
To explicitly specify which columns to include, and in which order, supply a "columns" array:
var data = [{
name: 'module1',
description: 'some description',
version: '0.0.1',
}, {
name: 'module2',
description: 'another description',
version: '0.2.0',
}]
var columns = columnify(data, {
columns: ['name', 'version'] // note description not included
})
console.log(columns)
NAME VERSION
module1 0.0.1
module2 0.2.0
MIT
FAQs
Render data in text columns. Supports in-column text-wrap.
The npm package columnify receives a total of 2,621,459 weekly downloads. As such, columnify popularity was classified as popular.
We found that columnify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.