Commit ef763c8d by Tomas Dvorak

Display ASCII tables' scalars indented to the right #83

parent c465b9c7
......@@ -19,13 +19,13 @@ class SampleData {
"+------------------------------------------------------------+\n" +
"| key | types | occurrences | percents |\n" +
"| ------------------ | ------------ | ----------- | -------- |\n" +
"| _id | ObjectId | 5 | 100 |\n" +
"| name | String | 5 | 100 |\n" +
"| bio | String | 3 | 60 |\n" +
"| birthday | String | 2 | 40 |\n" +
"| pets | Array,String | 2 | 40 |\n" +
"| someBinData | BinData-old | 1 | 20 |\n" +
"| someWeirdLegacyKey | String | 1 | 20 |\n" +
"| _id | ObjectId | 5 | 100.0 |\n" +
"| name | String | 5 | 100.0 |\n" +
"| bio | String | 3 | 60.0 |\n" +
"| birthday | String | 2 | 40.0 |\n" +
"| pets | Array,String | 2 | 40.0 |\n" +
"| someBinData | BinData-old | 1 | 20.0 |\n" +
"| someWeirdLegacyKey | String | 1 | 20.0 |\n" +
"+------------------------------------------------------------+";
/**
......
......@@ -274,15 +274,25 @@ if($outputFormat === 'json') {
printjson(varietyResults); // valid formatted json output, compressed variant is printjsononeline()
} else { // output nice ascii table with results
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) {
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) {
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 = '';
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