github.com/cloudcredo/cloudrocker@v0.0.0-20160108110610-1320f8cc2dfd/sample-apps/node/node_modules/logfmt/lib/stringify.js (about) 1 exports.stringify = function(data){ 2 var line = ''; 3 4 for(var key in data) { 5 var value = data[key]; 6 var is_null = false; 7 if(value == null) { 8 is_null = true; 9 value = ''; 10 } 11 else value = value.toString(); 12 13 var needs_quoting = value.indexOf(' ') > -1 || value.indexOf('=') > -1; 14 var needs_escaping = value.indexOf('"') > -1 || value.indexOf("\\") > -1; 15 16 if(needs_escaping) value = value.replace(/["\\]/g, '\\$&'); 17 if(needs_quoting) value = '"' + value + '"'; 18 if(value === '' && !is_null) value = '""'; 19 20 line += key + '=' + value + ' '; 21 } 22 23 //trim traling space 24 return line.substring(0,line.length-1); 25 } 26