commit nodejs-columnify for openSUSE:Factory

Hello community, here is the log from the commit of package nodejs-columnify for openSUSE:Factory checked in at 2015-07-02 22:33:34 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/nodejs-columnify (Old) and /work/SRC/openSUSE:Factory/.nodejs-columnify.new (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Package is "nodejs-columnify" Changes: -------- --- /work/SRC/openSUSE:Factory/nodejs-columnify/nodejs-columnify.changes 2015-04-27 13:00:34.000000000 +0200 +++ /work/SRC/openSUSE:Factory/.nodejs-columnify.new/nodejs-columnify.changes 2015-07-02 22:33:35.000000000 +0200 @@ -1,0 +2,6 @@ +Fri Apr 24 11:58:29 UTC 2015 - hvogel@suse.com + +- Update to version 1.5.1 +- drop columnify-deps.diff, not needed anymore + +------------------------------------------------------------------- Old: ---- columnify-1.2.1.tgz columnify-deps.diff New: ---- columnify-1.5.1.tgz ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ nodejs-columnify.spec ++++++ --- /var/tmp/diff_new_pack.smI9kk/_old 2015-07-02 22:33:35.000000000 +0200 +++ /var/tmp/diff_new_pack.smI9kk/_new 2015-07-02 22:33:35.000000000 +0200 @@ -19,14 +19,13 @@ %define base_name columnify Name: nodejs-columnify -Version: 1.2.1 +Version: 1.5.1 Release: 0 Summary: Render data in text columns, supports in-column text-wrap License: MIT Group: Development/Languages/Other Url: https://github.com/timoxley/columnify Source: http://registry.npmjs.org/%{base_name}/-/%{base_name}-%{version}.tgz -Patch: columnify-deps.diff BuildRequires: nodejs-packaging BuildRoot: %{_tmppath}/%{name}-%{version}-build BuildArch: noarch @@ -40,7 +39,6 @@ %prep %setup -q -n package -%patch -p1 %build ++++++ columnify-1.2.1.tgz -> columnify-1.5.1.tgz ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/package/Makefile new/package/Makefile --- old/package/Makefile 1970-01-01 01:00:00.000000000 +0100 +++ new/package/Makefile 2015-03-06 12:33:21.000000000 +0100 @@ -0,0 +1,9 @@ + +all: columnify.js + +prepublish: all + +columnify.js: index.js package.json + babel index.js > columnify.js + +.PHONY: all prepublish diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/package/Readme.md new/package/Readme.md --- old/package/Readme.md 2014-08-11 05:25:58.000000000 +0200 +++ new/package/Readme.md 2015-04-08 06:47:50.000000000 +0200 @@ -1,6 +1,13 @@ # columnify -[](https://travis-ci.org/timoxley/columnify) +[](https://nodei.co/npm-dl/columnify/) +[](https://nodei.co/npm/columnify/) + +[](https://travis-ci.org/timoxley/columnify) +[](https://npmjs.org/package/columnify) +[](LICENSE) +[](https://david-dm.org/timoxley/columnify) +[](https://david-dm.org/timoxley/columnify#info=devDependencies) Create text-based columns suitable for console output from objects or arrays of objects. @@ -36,7 +43,6 @@ Objects are converted to a list of key/value pairs: ```javascript - var data = { "commander@0.6.1": 1, "minimatch@0.2.14": 3, @@ -100,15 +106,58 @@ module2 0.2.0 ``` -### Wrapping Column Cells +### Filtering & Ordering Columns + +By default, all properties are converted into columns, whether or not +they exist on every object or not. -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. +To explicitly specify which columns to include, and in which order, +supply a "columns" or "include" array ("include" is just an alias). ```javascript +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'] +}) + +console.log(columns) +``` +#### Output: +``` +NAME VERSION +module1 0.0.1 +module2 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 the `config` option to specify individual columns: + +```javascript +var columns = columnify(data, { + optionName: optionValue, + config: { + columnName: {optionName: optionValue}, + columnName: {optionName: optionValue}, + } +}) +``` + +### Maximum and Minimum Column Widths +As with all options, you can define the `maxWidth` and `minWidth` globally, or for specified columns. By default, wrapping will happen at word boundaries. Empty cells or those which do not fill the `minWidth` will be padded with spaces. + +```javascript var columns = columnify([{ name: 'mod1', description: 'some description which happens to be far larger than the max', @@ -117,24 +166,41 @@ 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 +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 ``` -### Truncating Column Cells +#### Maximum Line Width + +You can set a hard maximum line width using the `maxLineWidth` option. +Beyond this value data is unceremoniously truncated with no truncation +marker. + +This can either be a number or 'auto' to set the value to the width of +stdout. + +Setting this value to 'auto' prevent TTY-imposed line-wrapping when +lines exceed the screen width. + +#### Truncating Column Cells Instead of Wrapping You can disable wrapping and instead truncate content at the maximum -column width. Truncation respects word boundaries. A truncation marker, -`…` will appear next to the last word in any truncated line. +column width by using the `truncate` option. Truncation respects word boundaries. A truncation marker, `…`, will appear next to the last word in any truncated line. ```javascript var columns = columnify(data, { @@ -155,43 +221,9 @@ module-two another description… 0.2.0 ``` -### Filtering & Ordering Columns - -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" or "include" array ("include" is just an alias). - -```javascript -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) -``` - -#### Output: -``` -NAME VERSION -module1 0.0.1 -module2 0.2.0 -``` - - -## Other Configuration Options ### Align Right/Center +You can set the alignment of the column data by using the `align` option. ```js var data = { @@ -211,10 +243,12 @@ debug@0.8.1 1 ``` -Align Center works in a similar way. +`align: 'center'` works in a similar way. + +### Padding Character -### Padding +Set a character to fill whitespace within columns with the `paddingChr` option. ```js var data = { @@ -232,7 +266,7 @@ veryVeryVeryVeryVeryLongKey shortVal................... ``` -### Preserve existing newlines +### Preserve Existing Newlines By default, `columnify` sanitises text by replacing any occurance of 1 or more whitespace characters with a single space. @@ -284,7 +318,7 @@ ### Custom Truncation Marker You can change the truncation marker to something other than the default -`…`. +`…` by using the `truncateMarker` option. ```javascript var columns = columnify(data, { @@ -309,10 +343,9 @@ ### Custom Column Splitter If your columns need some bling, you can split columns with custom -characters. +characters by using the `columnSplitter` option. ```javascript - var columns = columnify(data, { columnSplitter: ' | ' }) @@ -326,6 +359,57 @@ module-two | another description larger than the max | 0.2.0 ``` +### Control Header Display + +Control whether column headers are displayed by using the `showHeaders` option. + +```javascript +var columns = columnify(data, { + showHeaders: false +}) +``` + +This also works well for hiding a single column header, like an `id` column: +```javascript +var columns = 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` and `headerTransform`. Both of these take a function and need to return a valid string. + +```javascript +var columns = columnify([{ + name: 'mod1', + description: 'SOME DESCRIPTION TEXT.' +}, { + name: 'module-two', + description: 'SOME SLIGHTLY LONGER DESCRIPTION TEXT.' +}], { + dataTransform: function(data) { + return data.toLowerCase() + }, + config: { + name: { + headingTransform: function(heading) { + heading = "module " + heading + return "*" + heading.toUpperCase() + "*" + } + } + } +}) +``` +#### Output: +``` +*MODULE NAME* DESCRIPTION +mod1 some description text. +module-two some slightly longer description text. +``` + + ## Multibyte Character Support `columnify` uses [mycoboco/wcwidth.js](https://github.com/mycoboco/wcwidth.js) to calculate length of multibyte characters: @@ -362,6 +446,25 @@ 这是一个很长的名字的模块 这真的是一个描述的内容这个描述很长 0.3.3 ``` +## Contributions + +``` + project : columnify + repo age : 1 year, 2 months + active : 32 days + commits : 120 + files : 54 + authors : + 90 Tim Oxley 75.0% + 8 Tim 6.7% + 7 Arjun Mehta 5.8% + 6 Dany 5.0% + 5 Wei Gao 4.2% + 2 Dany Shaanan 1.7% + 1 Seth Miller 0.8% + 1 Isaac Z. Schlueter 0.8% +``` + ## License MIT diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/package/columnify.js new/package/columnify.js --- old/package/columnify.js 1970-01-01 01:00:00.000000000 +0100 +++ new/package/columnify.js 2015-04-08 06:58:04.000000000 +0200 @@ -0,0 +1,309 @@ +'use strict'; + +var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }; + +var wcwidth = require('./width'); + +var _require = require('./utils'); + +var padRight = _require.padRight; +var padCenter = _require.padCenter; +var padLeft = _require.padLeft; +var splitIntoLines = _require.splitIntoLines; +var splitLongWords = _require.splitLongWords; +var truncateString = _require.truncateString; + +var DEFAULT_HEADING_TRANSFORM = function DEFAULT_HEADING_TRANSFORM(key) { + return key.toUpperCase(); +}; + +var DEFAULT_DATA_TRANSFORM = function DEFAULT_DATA_TRANSFORM(cell, column, index) { + return cell; +}; + +var DEFAULTS = Object.freeze({ + maxWidth: Infinity, + minWidth: 0, + columnSplitter: ' ', + truncate: false, + truncateMarker: '…', + preserveNewLines: false, + paddingChr: ' ', + showHeaders: true, + headingTransform: DEFAULT_HEADING_TRANSFORM, + dataTransform: DEFAULT_DATA_TRANSFORM +}); + +module.exports = function (items) { + var options = arguments[1] === undefined ? {} : arguments[1]; + + var columnConfigs = options.config || {}; + delete options.config; // remove config so doesn't appear on every column. + + var maxLineWidth = options.maxLineWidth || Infinity; + if (maxLineWidth === 'auto') maxLineWidth = process.stdout.columns || Infinity; + delete options.maxLineWidth; // this is a line control option, don't pass it to column + + // Option defaults inheritance: + // options.config[columnName] => options => DEFAULTS + options = mixin({}, DEFAULTS, options); + + options.config = options.config || Object.create(null); + + options.spacing = options.spacing || '\n'; // probably useless + options.preserveNewLines = !!options.preserveNewLines; + options.showHeaders = !!options.showHeaders; + options.columns = options.columns || options.include; // alias include/columns, prefer columns if supplied + var columnNames = options.columns || []; // optional user-supplied columns to include + + items = toArray(items, columnNames); + + // if not suppled column names, automatically determine columns from data keys + if (!columnNames.length) { + items.forEach(function (item) { + for (var columnName in item) { + if (columnNames.indexOf(columnName) === -1) columnNames.push(columnName); + } + }); + } + + // initialize column defaults (each column inherits from options.config) + var columns = columnNames.reduce(function (columns, columnName) { + var column = Object.create(options); + columns[columnName] = mixin(column, columnConfigs[columnName]); + return columns; + }, Object.create(null)); + + // sanitize column settings + columnNames.forEach(function (columnName) { + var column = columns[columnName]; + column.name = columnName; + column.maxWidth = Math.ceil(column.maxWidth); + column.minWidth = Math.ceil(column.minWidth); + column.truncate = !!column.truncate; + column.align = column.align || 'left'; + }); + + // sanitize data + items = items.map(function (item) { + var result = Object.create(null); + columnNames.forEach(function (columnName) { + // null/undefined -> '' + result[columnName] = item[columnName] != null ? item[columnName] : ''; + // toString everything + result[columnName] = '' + result[columnName]; + if (columns[columnName].preserveNewLines) { + // merge non-newline whitespace chars + result[columnName] = result[columnName].replace(/[^\S\n]/gmi, ' '); + } else { + // merge all whitespace chars + result[columnName] = result[columnName].replace(/\s/gmi, ' '); + } + }); + return result; + }); + + // transform data cells + columnNames.forEach(function (columnName) { + var column = columns[columnName]; + items = items.map(function (item, index) { + var col = Object.create(column); + item[columnName] = column.dataTransform(item[columnName], col, index); + + var changedKeys = Object.keys(col); + // disable default heading transform if we wrote to column.name + if (changedKeys.indexOf('name') !== -1) { + if (column.headingTransform !== DEFAULT_HEADING_TRANSFORM) return; + column.headingTransform = function (heading) { + return heading; + }; + } + changedKeys.forEach(function (key) { + return column[key] = col[key]; + }); + return item; + }); + }); + + // add headers + var headers = {}; + if (options.showHeaders) { + columnNames.forEach(function (columnName) { + var column = columns[columnName]; + + if (!column.showHeaders) { + headers[columnName] = ''; + return; + } + + headers[columnName] = column.headingTransform(column.name); + }); + items.unshift(headers); + } + // get actual max-width between min & max + // based on length of data in columns + columnNames.forEach(function (columnName) { + var column = columns[columnName]; + column.width = items.map(function (item) { + return item[columnName]; + }).reduce(function (min, cur) { + return Math.max(min, Math.min(column.maxWidth, Math.max(column.minWidth, wcwidth(cur)))); + }, 0); + }); + + // split long words so they can break onto multiple lines + columnNames.forEach(function (columnName) { + var column = columns[columnName]; + items = items.map(function (item) { + item[columnName] = splitLongWords(item[columnName], column.width, column.truncateMarker); + return item; + }); + }); + + // wrap long lines. each item is now an array of lines. + columnNames.forEach(function (columnName) { + var column = columns[columnName]; + items = items.map(function (item, index) { + var cell = item[columnName]; + item[columnName] = splitIntoLines(cell, column.width); + + // if truncating required, only include first line + add truncation char + if (column.truncate && item[columnName].length > 1) { + item[columnName] = splitIntoLines(cell, column.width - wcwidth(column.truncateMarker)); + var firstLine = item[columnName][0]; + if (!endsWith(firstLine, column.truncateMarker)) item[columnName][0] += column.truncateMarker; + item[columnName] = item[columnName].slice(0, 1); + } + return item; + }); + }); + + // recalculate column widths from truncated output/lines + columnNames.forEach(function (columnName) { + var column = columns[columnName]; + column.width = items.map(function (item) { + return item[columnName].reduce(function (min, cur) { + return Math.max(min, Math.min(column.maxWidth, Math.max(column.minWidth, wcwidth(cur)))); + }, 0); + }).reduce(function (min, cur) { + return Math.max(min, Math.min(column.maxWidth, Math.max(column.minWidth, cur))); + }, 0); + }); + + var rows = createRows(items, columns, columnNames, options.paddingChr); // merge lines into rows + // conceive output + return rows.reduce(function (output, row) { + return output.concat(row.reduce(function (rowOut, line) { + return rowOut.concat(line.join(options.columnSplitter)); + }, [])); + }, []).map(function (line) { + return truncateString(line, maxLineWidth); + }).join(options.spacing); +}; + +/** + * Convert wrapped lines into rows with padded values. + * + * @param Array items data to process + * @param Array columns column width settings for wrapping + * @param Array columnNames column ordering + * @return Array items wrapped in arrays, corresponding to lines + */ + +function createRows(items, columns, columnNames, paddingChr) { + return items.map(function (item) { + var row = []; + var numLines = 0; + columnNames.forEach(function (columnName) { + numLines = Math.max(numLines, item[columnName].length); + }); + + var _loop = function (i) { + row[i] = row[i] || []; + columnNames.forEach(function (columnName) { + var column = columns[columnName]; + var val = item[columnName][i] || ''; // || '' ensures empty columns get padded + if (column.align === 'right') row[i].push(padLeft(val, column.width, paddingChr));else if (column.align === 'center' || column.align === 'centre') row[i].push(padCenter(val, column.width, paddingChr));else row[i].push(padRight(val, column.width, paddingChr)); + }); + }; + + // combine matching lines of each rows + for (var i = 0; i < numLines; i++) { + _loop(i); + } + return row; + }); +} + +/** + * Object.assign + * + * @return Object Object with properties mixed in. + */ + +function mixin() { + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + if (Object.assign) { + return Object.assign.apply(Object, _toConsumableArray(args)); + }return ObjectAssign.apply(undefined, _toConsumableArray(args)); +} + +function ObjectAssign(target, firstSource) { + 'use strict'; + if (target === undefined || target === null) throw new TypeError('Cannot convert first argument to object'); + + var to = Object(target); + + var hasPendingException = false; + var pendingException; + + for (var i = 1; i < arguments.length; i++) { + var nextSource = arguments[i]; + if (nextSource === undefined || nextSource === null) continue; + + var keysArray = Object.keys(Object(nextSource)); + for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { + var nextKey = keysArray[nextIndex]; + try { + var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); + if (desc !== undefined && desc.enumerable) to[nextKey] = nextSource[nextKey]; + } catch (e) { + if (!hasPendingException) { + hasPendingException = true; + pendingException = e; + } + } + } + + if (hasPendingException) throw pendingException; + } + return to; +} + +/** + * Adapted from String.prototype.endsWith polyfill. + */ + +function endsWith(target, searchString, position) { + position = position || target.length; + position = position - searchString.length; + var lastIndex = target.lastIndexOf(searchString); + return lastIndex !== -1 && lastIndex === position; +} + +function toArray(items, columnNames) { + if (Array.isArray(items)) { + return items; + }var rows = []; + for (var key in items) { + var item = {}; + item[columnNames[0] || 'key'] = key; + item[columnNames[1] || 'value'] = items[key]; + rows.push(item); + } + return rows; +} + diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/package/index.js new/package/index.js --- old/package/index.js 2014-08-11 05:25:58.000000000 +0200 +++ new/package/index.js 2015-04-08 06:47:50.000000000 +0200 @@ -1,15 +1,20 @@ "use strict" -var wcwidth = require('./width') -var utils = require('./utils') -var padRight = utils.padRight -var padCenter = utils.padCenter -var padLeft = utils.padLeft -var splitIntoLines = utils.splitIntoLines -var splitLongWords = utils.splitLongWords -var truncateString = utils.truncateString +const wcwidth = require('./width') +const { + padRight, + padCenter, + padLeft, + splitIntoLines, + splitLongWords, + truncateString +} = require('./utils') -var DEFAULTS = { +const DEFAULT_HEADING_TRANSFORM = key => key.toUpperCase() + +const DEFAULT_DATA_TRANSFORM = (cell, column, index) => cell + +const DEFAULTS = Object.freeze({ maxWidth: Infinity, minWidth: 0, columnSplitter: ' ', @@ -18,65 +23,63 @@ preserveNewLines: false, paddingChr: ' ', showHeaders: true, - headingTransform: function(key) { - return key.toUpperCase() - }, - dataTransform: function(cell, column, index) { - return cell - } -} + headingTransform: DEFAULT_HEADING_TRANSFORM, + dataTransform: DEFAULT_DATA_TRANSFORM +}) -module.exports = function(items, options) { +module.exports = function(items, options = {}) { - options = options || {} - - var columnConfigs = options.config || {} + let columnConfigs = options.config || {} delete options.config // remove config so doesn't appear on every column. - var maxLineWidth = options.maxLineWidth || Infinity + let maxLineWidth = options.maxLineWidth || Infinity + if (maxLineWidth === 'auto') maxLineWidth = process.stdout.columns || Infinity delete options.maxLineWidth // this is a line control option, don't pass it to column // Option defaults inheritance: // options.config[columnName] => options => DEFAULTS - options = mixin(options, DEFAULTS) + options = mixin({}, DEFAULTS, options) + options.config = options.config || Object.create(null) options.spacing = options.spacing || '\n' // probably useless options.preserveNewLines = !!options.preserveNewLines options.showHeaders = !!options.showHeaders; options.columns = options.columns || options.include // alias include/columns, prefer columns if supplied - var columnNames = options.columns || [] // optional user-supplied columns to include + let columnNames = options.columns || [] // optional user-supplied columns to include items = toArray(items, columnNames) // if not suppled column names, automatically determine columns from data keys if (!columnNames.length) { items.forEach(function(item) { - for (var columnName in item) { + for (let columnName in item) { if (columnNames.indexOf(columnName) === -1) columnNames.push(columnName) } }) } // initialize column defaults (each column inherits from options.config) - var columns = columnNames.reduce(function(columns, columnName) { - var column = Object.create(options) + let columns = columnNames.reduce((columns, columnName) => { + let column = Object.create(options) columns[columnName] = mixin(column, columnConfigs[columnName]) return columns }, Object.create(null)) // sanitize column settings - columnNames.forEach(function(columnName) { - var column = columns[columnName] + columnNames.forEach(columnName => { + let column = columns[columnName] + column.name = columnName column.maxWidth = Math.ceil(column.maxWidth) column.minWidth = Math.ceil(column.minWidth) column.truncate = !!column.truncate + column.align = column.align || 'left' }) // sanitize data - items = items.map(function(item) { - var result = Object.create(null) - columnNames.forEach(function(columnName) { + items = items.map(item => { + let result = Object.create(null) + columnNames.forEach(columnName => { // null/undefined -> '' result[columnName] = item[columnName] != null ? item[columnName] : '' // toString everything @@ -93,83 +96,98 @@ }) // transform data cells - columnNames.forEach(function(columnName) { - var column = columns[columnName] - items = items.map(function(item, index) { - item[columnName] = column.dataTransform(item[columnName], column, index) + columnNames.forEach(columnName => { + let column = columns[columnName] + items = items.map((item, index) => { + let col = Object.create(column) + item[columnName] = column.dataTransform(item[columnName], col, index) + + let changedKeys = Object.keys(col) + // disable default heading transform if we wrote to column.name + if (changedKeys.indexOf('name') !== -1) { + if (column.headingTransform !== DEFAULT_HEADING_TRANSFORM) return + column.headingTransform = heading => heading + } + changedKeys.forEach(key => column[key] = col[key]) return item }) }) // add headers - var headers = {} + let headers = {} if(options.showHeaders) { - columnNames.forEach(function(columnName) { - var column = columns[columnName] - headers[columnName] = column.headingTransform(columnName) + columnNames.forEach(columnName => { + let column = columns[columnName] + + if(!column.showHeaders){ + headers[columnName] = ''; + return; + } + + headers[columnName] = column.headingTransform(column.name) }) items.unshift(headers) } // get actual max-width between min & max // based on length of data in columns - columnNames.forEach(function(columnName) { - var column = columns[columnName] - column.width = items.map(function(item) { - return item[columnName] - }).reduce(function(min, cur) { + columnNames.forEach(columnName => { + let column = columns[columnName] + column.width = items + .map(item => item[columnName]) + .reduce((min, cur) => { return Math.max(min, Math.min(column.maxWidth, Math.max(column.minWidth, wcwidth(cur)))) }, 0) }) // split long words so they can break onto multiple lines - columnNames.forEach(function(columnName) { - var column = columns[columnName] - items = items.map(function(item) { + columnNames.forEach(columnName => { + let column = columns[columnName] + items = items.map(item => { item[columnName] = splitLongWords(item[columnName], column.width, column.truncateMarker) return item }) }) // wrap long lines. each item is now an array of lines. - columnNames.forEach(function(columnName) { - var column = columns[columnName] - items = items.map(function(item, index) { - var cell = item[columnName] + columnNames.forEach(columnName => { + let column = columns[columnName] + items = items.map((item, index) => { + let cell = item[columnName] item[columnName] = splitIntoLines(cell, column.width) // if truncating required, only include first line + add truncation char if (column.truncate && item[columnName].length > 1) { - item[columnName] = splitIntoLines(cell, column.width - wcwidth(column.truncateMarker)) - var firstLine = item[columnName][0] - if (!endsWith(firstLine, column.truncateMarker)) item[columnName][0] += column.truncateMarker - item[columnName] = item[columnName].slice(0, 1) + item[columnName] = splitIntoLines(cell, column.width - wcwidth(column.truncateMarker)) + let firstLine = item[columnName][0] + if (!endsWith(firstLine, column.truncateMarker)) item[columnName][0] += column.truncateMarker + item[columnName] = item[columnName].slice(0, 1) } return item }) }) // recalculate column widths from truncated output/lines - columnNames.forEach(function(columnName) { - var column = columns[columnName] - column.width = items.map(function(item) { - return item[columnName].reduce(function(min, cur) { + columnNames.forEach(columnName => { + let column = columns[columnName] + column.width = items.map(item => { + return item[columnName].reduce((min, cur) => { return Math.max(min, Math.min(column.maxWidth, Math.max(column.minWidth, wcwidth(cur)))) }, 0) - }).reduce(function(min, cur) { + }).reduce((min, cur) => { return Math.max(min, Math.min(column.maxWidth, Math.max(column.minWidth, cur))) }, 0) }) - var rows = createRows(items, columns, columnNames, options.paddingChr) // merge lines into rows + let rows = createRows(items, columns, columnNames, options.paddingChr) // merge lines into rows // conceive output - return rows.reduce(function(output, row) { - return output.concat(row.reduce(function(rowOut, line) { + return rows.reduce((output, row) => { + return output.concat(row.reduce((rowOut, line) => { return rowOut.concat(line.join(options.columnSplitter)) }, [])) - }, []).map(function(line) { - return truncateString(line, maxLineWidth) - }).join(options.spacing) + }, []) + .map(line => truncateString(line, maxLineWidth)) + .join(options.spacing) } /** @@ -182,20 +200,20 @@ */ function createRows(items, columns, columnNames, paddingChr) { - return items.map(function(item) { - var row = [] - var numLines = 0 - columnNames.forEach(function(columnName) { + return items.map(item => { + let row = [] + let numLines = 0 + columnNames.forEach(columnName => { numLines = Math.max(numLines, item[columnName].length) }) // combine matching lines of each rows - for (var i = 0; i < numLines; i++) { + for (let i = 0; i < numLines; i++) { row[i] = row[i] || [] - columnNames.forEach(function(columnName) { - var column = columns[columnName] - var val = item[columnName][i] || '' // || '' ensures empty columns get padded - if (column.align == 'right') row[i].push(padLeft(val, column.width, paddingChr)) - else if (column.align == 'center') row[i].push(padCenter(val, column.width, paddingChr)) + columnNames.forEach(columnName => { + let column = columns[columnName] + let val = item[columnName][i] || '' // || '' ensures empty columns get padded + if (column.align === 'right') row[i].push(padLeft(val, column.width, paddingChr)) + else if (column.align === 'center' || column.align === 'centre') row[i].push(padCenter(val, column.width, paddingChr)) else row[i].push(padRight(val, column.width, paddingChr)) }) } @@ -204,22 +222,50 @@ } /** - * Generic source->target mixin. - * Copy properties from `source` into `target` if target doesn't have them. - * Destructive. Modifies `target`. + * Object.assign * - * @param target Object target for mixin properties. - * @param source Object source of mixin properties. - * @return Object `target` after mixin applied. + * @return Object Object with properties mixed in. */ -function mixin(target, source) { - source = source || {} - for (var key in source) { - if (target.hasOwnProperty(key)) continue - target[key] = source[key] +function mixin(...args) { + if (Object.assign) return Object.assign(...args) + return ObjectAssign(...args) +} + +function ObjectAssign(target, firstSource) { + "use strict"; + if (target === undefined || target === null) + throw new TypeError("Cannot convert first argument to object"); + + var to = Object(target); + + var hasPendingException = false; + var pendingException; + + for (var i = 1; i < arguments.length; i++) { + var nextSource = arguments[i]; + if (nextSource === undefined || nextSource === null) + continue; + + var keysArray = Object.keys(Object(nextSource)); + for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { + var nextKey = keysArray[nextIndex]; + try { + var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); + if (desc !== undefined && desc.enumerable) + to[nextKey] = nextSource[nextKey]; + } catch (e) { + if (!hasPendingException) { + hasPendingException = true; + pendingException = e; + } + } + } + + if (hasPendingException) + throw pendingException; } - return target + return to; } /** @@ -229,16 +275,16 @@ function endsWith(target, searchString, position) { position = position || target.length; position = position - searchString.length; - var lastIndex = target.lastIndexOf(searchString); + let lastIndex = target.lastIndexOf(searchString); return lastIndex !== -1 && lastIndex === position; } function toArray(items, columnNames) { if (Array.isArray(items)) return items - var rows = [] - for (var key in items) { - var item = {} + let rows = [] + for (let key in items) { + let item = {} item[columnNames[0] || 'key'] = key item[columnNames[1] || 'value'] = items[key] rows.push(item) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/package/package.json new/package/package.json --- old/package/package.json 2014-08-11 06:22:47.000000000 +0200 +++ new/package/package.json 2015-04-08 06:57:48.000000000 +0200 @@ -1,18 +1,21 @@ { "name": "columnify", - "version": "1.2.1", - "description": "Render data in text columns, supports in-column text-wrap.", - "main": "index.js", + "version": "1.5.1", + "description": "Render data in text columns. supports in-column text-wrap.", + "main": "columnify.js", "scripts": { "pretest": "npm prune", - "test": "faucet" + "test": "make prepublish && tape test/*.js | tap-spec", + "bench": "npm test && node bench", + "prepublish": "make prepublish" }, "author": "Tim Oxley", "license": "MIT", "devDependencies": { - "chalk": "^0.4.0", - "faucet": "0.0.1", - "tape": "~2.12.3" + "babel": "^5.0.10", + "chalk": "^1.0.0", + "tap-spec": "^3.0.0", + "tape": "^4.0.0" }, "repository": { "type": "git", @@ -32,7 +35,7 @@ }, "homepage": "https://github.com/timoxley/columnify", "dependencies": { - "strip-ansi": "^1.0.0", + "strip-ansi": "^2.0.1", "wcwidth": "^1.0.0" }, "directories": {
participants (1)
-
root@hilbert.suse.de