GT2/GT2-iOS/node_modules/logfmt/lib/stringify.js

27 lines
664 B
JavaScript
Raw Normal View History

2018-02-12 17:26:06 +00:00
exports.stringify = function(data){
var line = '';
for(var key in data) {
var value = data[key];
var is_null = false;
if(value == null) {
is_null = true;
value = '';
}
else value = value.toString();
var needs_quoting = value.indexOf(' ') > -1 || value.indexOf('=') > -1;
var needs_escaping = value.indexOf('"') > -1 || value.indexOf("\\") > -1;
if(needs_escaping) value = value.replace(/["\\]/g, '\\$&');
if(needs_quoting) value = '"' + value + '"';
if(value === '' && !is_null) value = '""';
line += key + '=' + value + ' ';
}
//trim traling space
return line.substring(0,line.length-1);
}