I have this string containing this block of code:
[General]
StartWithLastProfile=0
Profilist.notifications=true
Profilist.dev=true
Profilist.dev-builds=80k
Profilist.launch_on_create=true
[Profile0]
Name=Dev Profilist
IsRelative=1
Path=Profiles/j0a1zjle.Unnamed Profile 1
Profilist.tie=0
[Profile55]
Name=Main
IsRelative=1
Path=Profiles/qekfxcdm.Unnamed Profile 1
Currently I use regex to read this into an object like this:
var str = '[General]\nStartWithLastProfile=0\nProfilist.notifications=true\nProfilist.dev=true\nProfilist.dev-builds=80k\nProfilist.launch_on_create=true\n\n[Profile0]\nName=Dev Profilist\nIsRelative=1\nPath=Profiles/j0a1zjle.Unnamed Profile 1\nProfilist.tie=0\n\n[Profile55]\nName=Main\nIsRelative=1\nPath=Profiles/qekfxcdm.Unnamed Profile 1';
console.time('parse');
var linePatt = /^(?:\[(.*)\]|(.*?)=(.*))$/gm;
var match;
var objParsed = {};
var lastKey;
while(match = linePatt.exec(str)) {
console.info(match);
// Array [ "[General]", "General", undefined, undefined ]
// Array [ "StartWithLastProfile=0", undefined, "StartWithLastProfile", "0" ]
if (match[2] == undefined) {
lastKey = match[1];
objParsed[lastKey] = {};
} else {
objParsed[lastKey][match[2]] = match[3];
}
}
console.timeEnd('parse');
console.log('objParsed:', JSON.stringify(objParsed));
This gives me an object like this:
{
"General": {
"StartWithLastProfile": "0",
"Profilist.notifications": "true",
"Profilist.dev": "true",
"Profilist.dev-builds": "80k",
"Profilist.launch_on_create": "true"
},
"Profile0": {
"Name": "Dev Profilist",
"IsRelative": "1",
"Path": "Profiles/j0a1zjle.Unnamed Profile 1",
"Profilist.tie": "0"
},
"Profile55": {
"Name": "Main",
"IsRelative": "1",
"Path": "Profiles/qekfxcdm.Unnamed Profile 1"
}
}
The console.time
and console.timeEnd
tell me this takes on average from 4-7ms. My application is for a much bigger file, though, and goes from Profile0 to ProfileN the blocks repeat. Is there any faster way you would do this?