Fix TSV output in MaxCtrl
The output in --tsv mode could break the TSV format if newlines or tabs were included in the data.
This commit is contained in:
@ -19,6 +19,26 @@ var os = require('os')
|
|||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
var readlineSync = require('readline-sync')
|
var readlineSync = require('readline-sync')
|
||||||
|
|
||||||
|
function normalizeWhitespace(table) {
|
||||||
|
table.forEach((v) => {
|
||||||
|
if (Array.isArray(v)) {
|
||||||
|
// `table` is an array of arrays
|
||||||
|
v.forEach((k) => {
|
||||||
|
if (typeof(v[k]) == 'string') {
|
||||||
|
v[k] = v[k].replace( /\s+/g, ' ')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else if (!Array.isArray(v) && v instanceof Object) {
|
||||||
|
// `table` is an array of objects
|
||||||
|
Object.keys(v).forEach((k) => {
|
||||||
|
if (typeof(v[k]) == 'string') {
|
||||||
|
v[k] = v[k].replace( /\s+/g, ' ')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function() {
|
module.exports = function() {
|
||||||
|
|
||||||
this._ = require('lodash-getpath')
|
this._ = require('lodash-getpath')
|
||||||
@ -101,7 +121,15 @@ module.exports = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.tableToString = function(table) {
|
this.tableToString = function(table) {
|
||||||
|
|
||||||
|
if (this.argv.tsv)
|
||||||
|
{
|
||||||
|
// Convert whitespace into spaces to prevent breaking the TSV format
|
||||||
|
normalizeWhitespace(table)
|
||||||
|
}
|
||||||
|
|
||||||
str = table.toString()
|
str = table.toString()
|
||||||
|
|
||||||
if (this.argv.tsv) {
|
if (this.argv.tsv) {
|
||||||
// Based on the regex found in: https://github.com/jonschlinkert/strip-color
|
// Based on the regex found in: https://github.com/jonschlinkert/strip-color
|
||||||
str = str.replace( /\x1B\[[(?);]{0,2}(;?\d)*./g, '')
|
str = str.replace( /\x1B\[[(?);]{0,2}(;?\d)*./g, '')
|
||||||
|
Reference in New Issue
Block a user