27 lines
664 B
JavaScript
27 lines
664 B
JavaScript
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);
|
|
}
|
|
|