string-table
Advanced tools
+11
| { | ||
| "name": "string-table", | ||
| "main": "stringTable.js", | ||
| "version": "0.1.1", | ||
| "homepage": "https://github.com/dtao/stringTable.js", | ||
| "authors": [ | ||
| "Dan Tao <daniel.tao@gmail.com>" | ||
| ], | ||
| "description": "Formats an array of data objects as a textual table", | ||
| "license": "MIT" | ||
| } |
+1
-1
| { | ||
| "name": "string-table", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "Formats an array of data objects as a textual table", | ||
@@ -5,0 +5,0 @@ "main": "stringTable.js", |
+120
-8
| stringTable.js | ||
| ============== | ||
| A groundbreaking, innovating JavaScript library to do something that's literally never been attempted before: formatting an array of data objects as a textual table. | ||
| A groundbreaking, innovative JavaScript library to do something that's literally never been attempted before: formatting an array of data objects as a textual table. | ||
| ```javascript | ||
| var objects = [ | ||
| var users = [ | ||
| { name: 'Dan', gender: 'M', age: 29 }, | ||
@@ -13,3 +13,3 @@ { name: 'Adam', gender: 'M', age: 31 }, | ||
| var table = stringTable.create(objects); | ||
| var table = stringTable.create(users); | ||
@@ -32,3 +32,3 @@ console.log(table); | ||
| ```javascript | ||
| var table = stringTable.create(objects, options); | ||
| var table = stringTable.create(users, options); | ||
| ``` | ||
@@ -41,5 +41,117 @@ | ||
| - `headers` (e.g., `['name', 'age']`): an array of strings indicating which column headers to include | ||
| - `formatters`: an object mapping column names to formatter functions | ||
| - `outerBorder` (default: `'|'`): the character(s) used to enclose the table | ||
| - `innerBorder` (default: `'|'`): the character(s) used to delimit cells within the table | ||
| ### `headers` | ||
| An array of strings indicating which column headers to include (and in what order) | ||
| *Default: every property from the first object in the list* | ||
| #### Example | ||
| ```javascript | ||
| stringTable.create(users, { headers: ['age', 'name'] }); | ||
| /* | ||
| * Output: | ||
| * | ||
| * | age | name | | ||
| * ---------------- | ||
| * | 29 | Dan | | ||
| * | 31 | Adam | | ||
| * | 33 | Lauren | | ||
| */ | ||
| ``` | ||
| ### `capitalizeHeaders` | ||
| Whether or not to capitalize the table's column headers | ||
| *Default: `false`* | ||
| #### Example | ||
| ```javascript | ||
| stringTable.create(users, { capitalizeHeaders: true }); | ||
| /* | ||
| * Output: | ||
| * | ||
| * | Name | Gender | Age | | ||
| * ------------------------- | ||
| * | Dan | M | 29 | | ||
| * | Adam | M | 31 | | ||
| * | Lauren | F | 33 | | ||
| */ | ||
| ``` | ||
| ### `formatters` | ||
| An object mapping column names to formatter functions | ||
| *Default: none* | ||
| #### Example | ||
| ```javascript | ||
| stringTable.create(users, { | ||
| formatters: { | ||
| name: function(value) { return value.toUpperCase(); } | ||
| } | ||
| }); | ||
| /* | ||
| * Output: | ||
| * | ||
| * | name | gender | age | | ||
| * ------------------------- | ||
| * | DAN | M | 29 | | ||
| * | ADAM | M | 31 | | ||
| * | LAUREN | F | 33 | | ||
| */ | ||
| ``` | ||
| ### `outerBorder` and `innerBorder` | ||
| The character(s) used to enclose the table and to delimit cells within the table, respectively | ||
| *Defaults: `'|'` for both* | ||
| #### Example | ||
| ```javascript | ||
| stringTable.create(users, { | ||
| outerBorder: '%', | ||
| innerBorder: '$' | ||
| }); | ||
| /* | ||
| * Output: | ||
| * | ||
| * % name $ gender $ age % | ||
| * ------------------------- | ||
| * % DAN $ M $ 29 % | ||
| * % ADAM $ M $ 31 % | ||
| * % LAUREN $ F $ 33 % | ||
| */ | ||
| ``` | ||
| ### `headerSeparator` | ||
| The character used to separate the header row from the table body | ||
| *Default: `'-'`* | ||
| #### Example | ||
| ```javascript | ||
| stringTable.create(users, { headerSeparator: '*' }); | ||
| /* | ||
| * Output: | ||
| * | ||
| * | name | gender | age | | ||
| * ************************* | ||
| * | Dan | M | 29 | | ||
| * | Adam | M | 31 | | ||
| * | Lauren | F | 33 | | ||
| */ | ||
| ``` |
| stringTable = require('../stringTable.js') | ||
| describe 'stringTable', -> | ||
| juxtapose = (left, right, indentation) -> | ||
| [leftRows, rightRows] = [left.split('\n'), right.split('\n')] | ||
| output = for leftRow, i in leftRows | ||
| indent(indentation) + "#{leftRow} #{rightRows[i]}" | ||
| output.join('\n') | ||
| indent = (indentation) -> | ||
| new Array(indentation + 1).join(' ') | ||
| beforeEach -> | ||
| this.addMatchers | ||
| toMatchTable: (expectedTable) -> | ||
| this.message = -> | ||
| """ | ||
| Expected these tables to match: | ||
| #{juxtapose(this.actual, expectedTable, 5)} | ||
| """ | ||
| this.actual == expectedTable | ||
| describe 'create', -> | ||
@@ -8,3 +29,3 @@ it 'makes a nicely formatted table from a list of objects', -> | ||
| expect(stringTable.create(objects)).toEqual( | ||
| expect(stringTable.create(objects)).toMatchTable( | ||
| """ | ||
@@ -21,3 +42,3 @@ | foo | bar | | ||
| expect(stringTable.create(objects)).toEqual( | ||
| expect(stringTable.create(objects)).toMatchTable( | ||
| """ | ||
@@ -34,3 +55,3 @@ | foo | bar | | ||
| expect(stringTable.create(objects)).toEqual( | ||
| expect(stringTable.create(objects)).toMatchTable( | ||
| """ | ||
@@ -51,3 +72,3 @@ | a | b | | ||
| it 'allows you to specify which column headings to include', -> | ||
| expect(stringTable.create(objects, { headers: ['a', 'c'] })).toEqual( | ||
| expect(stringTable.create(objects, { headers: ['a', 'c'] })).toMatchTable( | ||
| """ | ||
@@ -61,2 +82,17 @@ | a | c | | ||
| it 'provides the option of capitalizing column headings', -> | ||
| things = [ | ||
| { foo: 'app', bar: 'bow' }, | ||
| { foo: 'arc', bar: 'bra' } | ||
| ] | ||
| expect(stringTable.create(things, { capitalizeHeaders: true })).toMatchTable( | ||
| """ | ||
| | Foo | Bar | | ||
| ------------- | ||
| | app | bow | | ||
| | arc | bra | | ||
| """ | ||
| ) | ||
| it 'allows you to specify custom outer and inner borders', -> | ||
@@ -67,3 +103,3 @@ options = | ||
| expect(stringTable.create(objects, options)).toEqual( | ||
| expect(stringTable.create(objects, options)).toMatchTable( | ||
| """ | ||
@@ -77,2 +113,12 @@ || a * b * c || | ||
| it 'allows you to specify a custom header separator', -> | ||
| expect(stringTable.create(objects, { headerSeparator: 'x' })).toMatchTable( | ||
| """ | ||
| | a | b | c | | ||
| xxxxxxxxxxxxxxxxxxx | ||
| | app | bow | cow | | ||
| | arc | bra | cap | | ||
| """ | ||
| ) | ||
| it 'allows you to specify a custom formatter for each column', -> | ||
@@ -83,3 +129,3 @@ options = | ||
| expect(stringTable.create(objects, options)).toEqual( | ||
| expect(stringTable.create(objects, options)).toMatchTable( | ||
| """ | ||
@@ -86,0 +132,0 @@ | a | b | c | |
+28
-8
@@ -10,7 +10,9 @@ (function(module) { | ||
| var headers = options.headers || Object.keys(records[0]), | ||
| outerBorder = options.outerBorder || '|', | ||
| innerBorder = options.innerBorder || '|', | ||
| formatters = options.formatters || {} | ||
| rows = [headers]; | ||
| var headers = options.headers || Object.keys(records[0]), | ||
| outerBorder = options.outerBorder || '|', | ||
| innerBorder = options.innerBorder || '|', | ||
| headerSeparator = options.headerSeparator || '-', | ||
| capitalizeHeaders = options.capitalizeHeaders || false, | ||
| formatters = options.formatters || {}, | ||
| rows = [createHeaderRow(headers, capitalizeHeaders)]; | ||
@@ -61,3 +63,3 @@ for (var i = 0; i < records.length; ++i) { | ||
| if (i === 0) { | ||
| formattedRows.push(createHeaderSeparator(totalWidth)); | ||
| formattedRows.push(createHeaderSeparator(totalWidth, headerSeparator)); | ||
| } | ||
@@ -80,6 +82,16 @@ } | ||
| function createHeaderSeparator(totalWidth) { | ||
| return repeat('-', totalWidth); | ||
| function createHeaderRow(headers, capitalizeHeaders) { | ||
| var row = Array.prototype.slice.call(headers, 0); | ||
| if (capitalizeHeaders) { | ||
| for (var i = 0; i < row.length; ++i) { | ||
| row[i] = capitalize(row[i]); | ||
| } | ||
| } | ||
| return row; | ||
| } | ||
| function createHeaderSeparator(totalWidth, separator) { | ||
| return repeat(separator, totalWidth); | ||
| } | ||
| function getMaxWidth(rows, columnIndex) { | ||
@@ -97,2 +109,10 @@ var maxWidth = 0; | ||
| function capitalize(value) { | ||
| if (!value) { | ||
| return value; | ||
| } | ||
| return value.charAt(0).toUpperCase() + value.substring(1); | ||
| } | ||
| function formatCell(value, width, type) { | ||
@@ -99,0 +119,0 @@ var padding = width - String(value).length; |
10593
55.57%5
25%121
30.11%155
260.47%