GT2/GT2-Android/node_modules/logfmt/lib/logfmt_parser.js

73 lines
1.7 KiB
JavaScript
Raw Normal View History

exports.debug = false;
exports.parse = function(line) {
var key = '';
var value = '';
var is_number = true;
var in_key = false;
var in_value = false;
var in_quote = false;
var had_quote = false;
var object = {};
var debug = exports.debug;
if(line[line.length - 1] == '\n'){
line = line.slice(0,line.length - 1)
}
for(var i=0; i <= line.length; i++){
if((line[i] == ' ' && !in_quote) || i == line.length){
if(in_key && key.length > 0){
object[key] = true;
}else if(in_value){
if(value == 'true') value = true;
else if(value == 'false') value = false;
else if(value === '' && !had_quote) value = null;
object[key] = value;
value = '';
}
if(i == line.length) break;
else {
in_key = false;
in_value = false;
in_quote = false;
had_quote = false;
}
}
if(line[i] == '=' && !in_quote){
if(debug) console.log('split')
//split
in_key = false;
in_value = true;
}
else if(line[i] == '\\'){
i ++ ;
value += line[i];
if(debug) console.log('escape: ' + line[i])
}
else if(line[i] == '"'){
had_quote = true;
in_quote = !in_quote;
if(debug) console.log('in quote: ' + in_quote)
}
else if(line[i] != ' ' && !in_value && !in_key){
if(debug) console.log('start key with: ' + line[i])
in_key = true;
key = line[i];
}
else if(in_key){
if(debug) console.log('add to key: ' + line[i])
key += line[i]
}
else if(in_value){
if(debug) console.log('add to value: ' + line[i])
value += line[i];
}
}
return object;
}