column-layout
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -9,2 +9,3 @@ 'use strict'; | ||
var Columns = require('./columns'); | ||
var Rows = require('./rows'); | ||
@@ -27,2 +28,3 @@ module.exports = columnLayout; | ||
columnLayout.Columns = Columns; | ||
columnLayout.Columns = Columns; | ||
columnLayout.Rows = Rows; |
@@ -16,2 +16,3 @@ 'use strict'; | ||
var Cell = require('./cell'); | ||
var t = require('typical'); | ||
@@ -64,2 +65,27 @@ var Rows = (function (_Array) { | ||
} | ||
}, { | ||
key: 'removeEmptyColumns', | ||
value: function removeEmptyColumns(data) { | ||
var distinctColumnNames = data.reduce(function (columnNames, row) { | ||
Object.keys(row).forEach(function (key) { | ||
if (columnNames.indexOf(key) === -1) columnNames.push(key); | ||
}); | ||
return columnNames; | ||
}, []); | ||
var emptyColumns = distinctColumnNames.filter(function (columnName) { | ||
var hasValue = data.some(function (row) { | ||
var value = row[columnName]; | ||
return t.isDefined(value) && !t.isString(value) || t.isString(value) && /\S+/.test(value); | ||
}); | ||
return !hasValue; | ||
}); | ||
return data.map(function (row) { | ||
emptyColumns.forEach(function (emptyCol) { | ||
return delete row[emptyCol]; | ||
}); | ||
return row; | ||
}); | ||
} | ||
}]); | ||
@@ -66,0 +92,0 @@ |
@@ -42,5 +42,10 @@ 'use strict'; | ||
var options = _options.get(this); | ||
if (options.ignoreEmptyColumns) { | ||
data = Rows.removeEmptyColumns(data); | ||
} | ||
this.columns = Rows.getColumns(data); | ||
this.rows = new Rows(data, this.columns); | ||
var options = _options.get(this); | ||
@@ -153,5 +158,5 @@ this.columns.viewWidth = options.viewWidth; | ||
cellValue = cellValue || ''; | ||
return (padding.left || '') + cellValue.padRight(width - padding.length() + ansiLength) + (padding.right || ''); | ||
return (padding.left || '') + cellValue.padEnd(width - padding.length() + ansiLength) + (padding.right || ''); | ||
} | ||
module.exports = Table; |
@@ -9,2 +9,3 @@ 'use strict' | ||
var Columns = require('./columns') | ||
var Rows = require('./rows') | ||
@@ -17,24 +18,28 @@ /** | ||
/** | ||
Returns JSON data formatted in columns. | ||
@param {array} - input data | ||
@param [options] {object} - optional settings | ||
@param [options.viewWidth] {number} - maximum width of layout | ||
@param [options.nowrap] {boolean} - disable wrapping on all columns | ||
@param [options.break] {boolean} - enable word-breaking on all columns | ||
@param [options.columns] {module:column-layout~columnOption} - array of column options | ||
@param [options.padding] {object} - Padding values to set on each column. Per-column overrides can be set in the `options.columns` array. | ||
@param [options.padding.left] {string} | ||
@param [options.padding.right] {string} | ||
@returns {string} | ||
@alias module:column-layout | ||
@example | ||
> columnFormat = require("column-format") | ||
> jsonData = [{ | ||
col1: "Some text you wish to read in column layout", | ||
col2: "And some more text in column two. " | ||
}] | ||
> columnFormat(jsonData, { viewWidth: 30 }) | ||
' Some text you And some more \n wish to read text in \n in column column two. \n layout \n' | ||
*/ | ||
* Returns JSON data formatted in columns. | ||
* | ||
* @param {object[]} - input data | ||
* @param [options] {object} - optional settings | ||
* @param [options.viewWidth] {number} - maximum width of layout | ||
* @param [options.nowrap] {boolean} - disable wrapping on all columns | ||
* @param [options.break] {boolean} - enable word-breaking on all columns | ||
* @param [options.columns] {module:column-layout~columnOption} - array of column options | ||
* @param [options.ignoreEmptyColumns] {boolean} | ||
* @param [options.padding] {object} - Padding values to set on each column. Per-column overrides can be set in the `options.columns` array. | ||
* @param [options.padding.left] {string} | ||
* @param [options.padding.right] {string} | ||
* @returns {string} | ||
* @alias module:column-layout | ||
* @example | ||
* > columnFormat = require("column-format") | ||
* > jsonData = [{ | ||
* col1: "Some text you wish to read in column layout", | ||
* col2: "And some more text in column two. " | ||
* }] | ||
* > console.log(columnFormat(jsonData, { viewWidth: 30 })) | ||
* Some text you And some more | ||
* wish to read text in | ||
* in column column two. | ||
* layout | ||
*/ | ||
function columnLayout (data, options) { | ||
@@ -47,2 +52,4 @@ var table = new Table(data, options) | ||
* Identical to {@link module:column-layout} with the exception of the rendered result being returned as an array of lines, rather that a single string. | ||
* @param {object[]} - input data | ||
* @param [options] {object} - optional settings | ||
* @returns {Array} | ||
@@ -66,2 +73,7 @@ * @example | ||
/** | ||
* @param {object[]} - input data | ||
* @param [options] {object} - optional settings | ||
* @returns {Table} | ||
*/ | ||
columnLayout.table = function (data, options) { | ||
@@ -74,2 +86,3 @@ return new Table(data, options) | ||
* @typedef module:column-layout~columnOption | ||
* @property name {string} - column name, must match a property name in the input | ||
* @property [width] {number} - column width | ||
@@ -86,1 +99,2 @@ * @property [minWidth] {number} - column min width | ||
columnLayout.Columns = Columns | ||
columnLayout.Rows = Rows |
@@ -7,2 +7,3 @@ 'use strict' | ||
const Cell = require('./cell') | ||
const t = require('typical') | ||
@@ -53,2 +54,24 @@ /** | ||
} | ||
static removeEmptyColumns (data) { | ||
const distinctColumnNames = data.reduce((columnNames, row) => { | ||
Object.keys(row).forEach(key => { | ||
if (columnNames.indexOf(key) === -1) columnNames.push(key) | ||
}) | ||
return columnNames | ||
}, []) | ||
const emptyColumns = distinctColumnNames.filter(columnName => { | ||
const hasValue = data.some(row => { | ||
const value = row[columnName] | ||
return (t.isDefined(value) && !t.isString(value)) || (t.isString(value) && /\S+/.test(value)) | ||
}) | ||
return !hasValue | ||
}) | ||
return data.map(row => { | ||
emptyColumns.forEach(emptyCol => delete row[emptyCol]) | ||
return row | ||
}) | ||
} | ||
} | ||
@@ -55,0 +78,0 @@ |
@@ -39,5 +39,11 @@ 'use strict' | ||
load (data) { | ||
let options = _options.get(this) | ||
/* remove empty columns */ | ||
if (options.ignoreEmptyColumns) { | ||
data = Rows.removeEmptyColumns(data) | ||
} | ||
this.columns = Rows.getColumns(data) | ||
this.rows = new Rows(data, this.columns) | ||
let options = _options.get(this) | ||
@@ -141,3 +147,3 @@ /* load default column properties from options */ | ||
return (padding.left || '') + | ||
cellValue.padRight(width - padding.length() + ansiLength) + | ||
cellValue.padEnd(width - padding.length() + ansiLength) + | ||
(padding.right || '') | ||
@@ -144,0 +150,0 @@ } |
{ | ||
"name": "column-layout", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Pretty-print JSON data in columns.", | ||
@@ -24,3 +24,3 @@ "repository": "https://github.com/75lb/column-layout.git", | ||
"test": "tape test/*.js", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js -p list -c list > README.md; echo", | ||
"es5": "babel --no-comments lib --out-dir es5", | ||
@@ -30,18 +30,18 @@ "cover": "istanbul cover tape -- test/*.js && cat coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf coverage; echo" | ||
"dependencies": { | ||
"ansi-escape-sequences": "^2.1.0", | ||
"ansi-escape-sequences": "^2.2.2", | ||
"array-back": "^1.0.2", | ||
"collect-json": "^1", | ||
"command-line-args": "^2", | ||
"core-js": "^1.2.2", | ||
"deep-extend": "~0.4.0", | ||
"feature-detect-es6": "^1.0.0", | ||
"collect-json": "^1.0.7", | ||
"command-line-args": "^2.1.6", | ||
"core-js": "^2.1", | ||
"deep-extend": "~0.4.1", | ||
"feature-detect-es6": "^1.2.0", | ||
"object-tools": "^2", | ||
"typical": "^2.2.0", | ||
"typical": "^2.4.2", | ||
"wordwrapjs": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.8.34", | ||
"babel": "^5.8.35", | ||
"coveralls": "^2.11.4", | ||
"jsdoc-to-markdown": "^1.1.1", | ||
"tape": "^4.0.0" | ||
"jsdoc-to-markdown": "^1.3.3", | ||
"tape": "^4.4.0" | ||
}, | ||
@@ -48,0 +48,0 @@ "standard": { |
@@ -112,7 +112,6 @@ [![view on npm](http://img.shields.io/npm/v/column-layout.svg)](https://www.npmjs.org/package/column-layout) | ||
* [column-layout](#module_column-layout) | ||
* [columnLayout(data, [options])](#exp_module_column-layout--columnLayout) ⇒ <code>string</code> ⏏ | ||
* _static_ | ||
* [.lines()](#module_column-layout--columnLayout.lines) ⇒ <code>Array</code> | ||
* _inner_ | ||
* [~columnOption](#module_column-layout--columnLayout..columnOption) | ||
* [columnLayout(data, [options])](#exp_module_column-layout--columnLayout) ⇒ <code>string</code> ⏏ | ||
* [.lines(data, [options])](#module_column-layout--columnLayout.lines) ⇒ <code>Array</code> | ||
* [.table(data, [options])](#module_column-layout--columnLayout.table) ⇒ <code>[Table](#Table)</code> | ||
* [~columnOption](#module_column-layout--columnLayout..columnOption) | ||
@@ -124,14 +123,14 @@ <a name="exp_module_column-layout--columnLayout"></a> | ||
**Kind**: Exported function | ||
**Params** | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| data | <code>array</code> | input data | | ||
| [options] | <code>object</code> | optional settings | | ||
| [options.viewWidth] | <code>number</code> | maximum width of layout | | ||
| [options.nowrap] | <code>boolean</code> | disable wrapping on all columns | | ||
| [options.break] | <code>boolean</code> | enable word-breaking on all columns | | ||
| [options.columns] | <code>[columnOption](#module_column-layout--columnLayout..columnOption)</code> | array of column options | | ||
| [options.padding] | <code>object</code> | Padding values to set on each column. Per-column overrides can be set in the `options.columns` array. | | ||
| [options.padding.left] | <code>string</code> | | | ||
| [options.padding.right] | <code>string</code> | | | ||
- data <code>Array.<object></code> - input data | ||
- [options] <code>object</code> - optional settings | ||
- [.viewWidth] <code>number</code> - maximum width of layout | ||
- [.nowrap] <code>boolean</code> - disable wrapping on all columns | ||
- [.break] <code>boolean</code> - enable word-breaking on all columns | ||
- [.columns] <code>[columnOption](#module_column-layout--columnLayout..columnOption)</code> - array of column options | ||
- [.ignoreEmptyColumns] <code>boolean</code> | ||
- [.padding] <code>object</code> - Padding values to set on each column. Per-column overrides can be set in the `options.columns` array. | ||
- [.left] <code>string</code> | ||
- [.right] <code>string</code> | ||
@@ -145,10 +144,18 @@ **Example** | ||
}] | ||
> columnFormat(jsonData, { viewWidth: 30 }) | ||
' Some text you And some more \n wish to read text in \n in column column two. \n layout \n' | ||
> console.log(columnFormat(jsonData, { viewWidth: 30 })) | ||
Some text you And some more | ||
wish to read text in | ||
in column column two. | ||
layout | ||
``` | ||
<a name="module_column-layout--columnLayout.lines"></a> | ||
#### columnLayout.lines() ⇒ <code>Array</code> | ||
#### columnLayout.lines(data, [options]) ⇒ <code>Array</code> | ||
Identical to [column-layout](#module_column-layout) with the exception of the rendered result being returned as an array of lines, rather that a single string. | ||
**Kind**: static method of <code>[columnLayout](#exp_module_column-layout--columnLayout)</code> | ||
**Params** | ||
- data <code>Array.<object></code> - input data | ||
- [options] <code>object</code> - optional settings | ||
**Example** | ||
@@ -167,2 +174,10 @@ ```js | ||
``` | ||
<a name="module_column-layout--columnLayout.table"></a> | ||
#### columnLayout.table(data, [options]) ⇒ <code>[Table](#Table)</code> | ||
**Kind**: static method of <code>[columnLayout](#exp_module_column-layout--columnLayout)</code> | ||
**Params** | ||
- data <code>Array.<object></code> - input data | ||
- [options] <code>object</code> - optional settings | ||
<a name="module_column-layout--columnLayout..columnOption"></a> | ||
@@ -175,2 +190,3 @@ #### columnLayout~columnOption | ||
| --- | --- | --- | | ||
| name | <code>string</code> | column name, must match a property name in the input | | ||
| width | <code>number</code> | column width | | ||
@@ -188,2 +204,2 @@ | minWidth | <code>number</code> | column min width | | ||
© 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). | ||
© 2015-16 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
'use strict' | ||
var test = require('tape') | ||
var columnLayout = require('../') | ||
var Rows = require('../').Rows | ||
test('removeEmptyColumns', function (t) { | ||
var input = [ | ||
{ "name": "Lloyd", "age": "" }, | ||
{ "name": "Roger", "age": " " }, | ||
{ "name": "Amir" }, | ||
{ "name": "Frank" }, | ||
{ "name": "Amy" } | ||
] | ||
t.deepEqual( | ||
Rows.removeEmptyColumns(input), | ||
[ | ||
{ "name": "Lloyd" }, | ||
{ "name": "Roger" }, | ||
{ "name": "Amir" }, | ||
{ "name": "Frank" }, | ||
{ "name": "Amy" } | ||
] | ||
) | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
98317
43
2154
200
- Removedcore-js@1.2.7(transitive)
Updatedansi-escape-sequences@^2.2.2
Updatedcollect-json@^1.0.7
Updatedcommand-line-args@^2.1.6
Updatedcore-js@^2.1
Updateddeep-extend@~0.4.1
Updatedfeature-detect-es6@^1.2.0
Updatedtypical@^2.4.2