Commit ef763c8d by Tomas Dvorak

Display ASCII tables' scalars indented to the right #83

parent c465b9c7
...@@ -16,17 +16,17 @@ class SampleData { ...@@ -16,17 +16,17 @@ class SampleData {
* against this table, to check correct formatting. * against this table, to check correct formatting.
*/ */
public static final String EXPECTED_DATA_ASCII_TABLE = public static final String EXPECTED_DATA_ASCII_TABLE =
"+------------------------------------------------------------+\n" + "+------------------------------------------------------------+\n" +
"| key | types | occurrences | percents |\n" + "| key | types | occurrences | percents |\n" +
"| ------------------ | ------------ | ----------- | -------- |\n" + "| ------------------ | ------------ | ----------- | -------- |\n" +
"| _id | ObjectId | 5 | 100 |\n" + "| _id | ObjectId | 5 | 100.0 |\n" +
"| name | String | 5 | 100 |\n" + "| name | String | 5 | 100.0 |\n" +
"| bio | String | 3 | 60 |\n" + "| bio | String | 3 | 60.0 |\n" +
"| birthday | String | 2 | 40 |\n" + "| birthday | String | 2 | 40.0 |\n" +
"| pets | Array,String | 2 | 40 |\n" + "| pets | Array,String | 2 | 40.0 |\n" +
"| someBinData | BinData-old | 1 | 20 |\n" + "| someBinData | BinData-old | 1 | 20.0 |\n" +
"| someWeirdLegacyKey | String | 1 | 20 |\n" + "| someWeirdLegacyKey | String | 1 | 20.0 |\n" +
"+------------------------------------------------------------+"; "+------------------------------------------------------------+";
/** /**
* Java representation of sample collection provided in variety README:<p> * Java representation of sample collection provided in variety README:<p>
......
...@@ -274,15 +274,25 @@ if($outputFormat === 'json') { ...@@ -274,15 +274,25 @@ if($outputFormat === 'json') {
printjson(varietyResults); // valid formatted json output, compressed variant is printjsononeline() printjson(varietyResults); // valid formatted json output, compressed variant is printjsononeline()
} else { // output nice ascii table with results } else { // output nice ascii table with results
var table = [['key', 'types', 'occurrences', 'percents'], ['', '', '', '']]; // header + delimiter rows var table = [['key', 'types', 'occurrences', 'percents'], ['', '', '', '']]; // header + delimiter rows
var significantDigits = function(value) {
return value.toExponential()
.replace(/e[\+\-0-9]*$/, '') // remove exponential notation
.replace( /^0\.?0*|\./, '') // remove decimal point and leading zeros
.length;
};
var maxDigits = Math.max.apply(null, varietyResults.map(function(value){return significantDigits(value.percentContaining);}));
varietyResults.forEach(function(key) { varietyResults.forEach(function(key) {
table.push([key._id.key, key.value.types.toString(), key.totalOccurrences.toString(), key.percentContaining.toString()]); table.push([key._id.key, key.value.types.toString(), key.totalOccurrences.toString(), key.percentContaining.toFixed(maxDigits).toString()]);
}); });
var colMaxWidth = function(arr, index) { var colMaxWidth = function(arr, index) {
return Math.max.apply(null, arr.map(function(row){return row[index].toString().length;})); return Math.max.apply(null, arr.map(function(row){return row[index].toString().length;}));
}; };
var pad = function(width, string, symbol) { return width <= string.length ? string : pad(width, string + symbol, symbol); }; var pad = function(width, string, symbol) { return width <= string.length ? string : pad(width, isNaN(string) ? string + symbol : symbol + string, symbol); };
var output = ''; var output = '';
table.forEach(function(row, ri){ table.forEach(function(row, ri){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment