string-table
Advanced tools
+1
-1
| { | ||
| "name": "string-table", | ||
| "main": "stringTable.js", | ||
| "version": "0.1.1", | ||
| "version": "0.1.2", | ||
| "homepage": "https://github.com/dtao/stringTable.js", | ||
@@ -6,0 +6,0 @@ "authors": [ |
+1
-1
| { | ||
| "name": "string-table", | ||
| "version": "0.1.1", | ||
| "version": "0.1.2", | ||
| "description": "Formats an array of data objects as a textual table", | ||
@@ -5,0 +5,0 @@ "main": "stringTable.js", |
+26
-0
@@ -109,2 +109,28 @@ stringTable.js | ||
| ### `typeFormatters` | ||
| An object mapping data *types* (`'string'`, `'number'`, `'boolean'`, etc.) to formatter functions (has lower precedence than `formatters` option) | ||
| *Default: none* | ||
| #### Example | ||
| ```javascript | ||
| stringTable.create(users, { | ||
| typeFormatters: { | ||
| number: function(value) { return value.toFixed(2); } | ||
| } | ||
| }); | ||
| /* | ||
| * Output: | ||
| * | ||
| * | name | gender | age | | ||
| * ---------------------------- | ||
| * | Dan | M | 29.00 | | ||
| * | Adam | M | 31.00 | | ||
| * | Lauren | F | 33.00 | | ||
| */ | ||
| ``` | ||
| ### `outerBorder` and `innerBorder` | ||
@@ -111,0 +137,0 @@ |
@@ -130,1 +130,38 @@ stringTable = require('../stringTable.js') | ||
| ) | ||
| it 'allows you to specify a custom formatter for a given type', -> | ||
| numbers = [ | ||
| { name: 'one', value: 1 }, | ||
| { name: 'two', value: 2 }, | ||
| { name: 'three', value: 3 } | ||
| ] | ||
| options = | ||
| typeFormatters: | ||
| number: (value) -> value.toFixed(2) | ||
| expect(stringTable.create(numbers, options)).toMatchTable( | ||
| """ | ||
| | name | value | | ||
| ----------------- | ||
| | one | 1.00 | | ||
| | two | 2.00 | | ||
| | three | 3.00 | | ||
| """ | ||
| ) | ||
| it 'gives precedence to a column-specific formatter before a type formatter', -> | ||
| options = | ||
| formatters: | ||
| b: (value) -> value.toUpperCase(), | ||
| typeFormatters: | ||
| string: (value) -> value.substring(0, 2) | ||
| expect(stringTable.create(objects, options)).toMatchTable( | ||
| """ | ||
| | a | b | c | | ||
| ----------------- | ||
| | ap | BOW | co | | ||
| | ar | BRA | ca | | ||
| """ | ||
| ) |
+13
-6
@@ -16,6 +16,7 @@ (function(module) { | ||
| formatters = options.formatters || {}, | ||
| typeFormatters = options.typeFormatters || {}, | ||
| rows = [createHeaderRow(headers, capitalizeHeaders)]; | ||
| for (var i = 0; i < records.length; ++i) { | ||
| rows.push(createRow(records[i], headers, formatters)); | ||
| rows.push(createRow(records[i], headers, formatters, typeFormatters)); | ||
| } | ||
@@ -70,9 +71,15 @@ | ||
| function createRow(data, headers, formatters) { | ||
| function createRow(data, headers, formatters, typeFormatters) { | ||
| var row = []; | ||
| for (var i = 0; i < headers.length; ++i) { | ||
| (function(header) { | ||
| var formatter = formatters[header] || identity; | ||
| row.push(formatter(data[header])); | ||
| }(headers[i])); | ||
| (function(header, columnIndex) { | ||
| var value = data[header]; | ||
| var formatter = formatters[header] || | ||
| typeFormatters[typeof value] || | ||
| identity; | ||
| row.push(formatter(value)); | ||
| }(headers[i], i)); | ||
| } | ||
@@ -79,0 +86,0 @@ return row; |
12322
16.32%125
3.31%181
16.77%