-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dots aren't decoded in deeply nested sections #22
Comments
+1 Example №1 [test]
lvl1.value = value1
lvl1.lvl2.value1 = 1 will return { 'lvl1.value': 'value1', 'lvl1.lvl2.value1': '1' } Example №2 [test][test1]
lvl1.value = value1
lvl1.lvl2.value1 = 1 Will return |
@rlidwka Your example is a valid weirdness. Patch welcome. @avasin Your first example is working as intended. Your second example is weird and I am not sure what the correct behavior is. Can you please post separate issues for what you expect the (2) case to do? It's not related to @rlidwka's issue. |
@isaacs hm.. i expected to get deep parsed object in the first example:
Second example behaviour is expected to be the same as described here - deep object parsing. |
I would like to access a file with variable filenames sections. It would be nice to access the object with quotes.
You need to comment lines 101-120 to do this. One possible feature should be to be able to disable this with an option when invoking |
@mycaule , that's not how ini works. All ini parsers treat dots as path separators, and not doing so would be incompatible. Instead, I would suggest to use this syntax:
This module doesn't support it, but I've seen parsers in other languages that do. I implemented it here, can provide a patch if someone is interested. |
I have never seen a dot in a Windows config file section used that way. I think it is more like a hack to do some sort of mapping between JSON and INI. |
i cannot find this in the spec anywhere… is this something that evolved as a quasi-standard? is the statement really true for all ini parsers? the idea to use a filename for the section key does seem legit. but this package makes parsing such |
This is my solution, seems to be working to flatten it back as expected: /**
* Reduce the nested object back to a flat object by concatenating object keys with a `.`
* @param obj Nested object
* @returns {{}}
*/
function flattenIniObject(obj)
{
function _flattenIniObject(obj, topLevel = [], resp = {}) {
let props = {};
for(let key in obj) {
if(typeof obj[key] == 'object') {
topLevel.push(key);
resp = { ...resp, ..._flattenIniObject(obj[key], topLevel, resp)}
}
else
props[key] = obj[key];
}
const topLevelName = topLevel.join(".");
if(topLevelName !== '' && Object.keys(props).length > 0)
resp = { ...resp, [topLevelName]: props}
topLevel.pop();
return resp;
}
return _flattenIniObject(obj);
}
let iniData = ini.parse(`; config.ini
[file1.jpg]
size=10
date=2014
[file2.jpg]
size=11
date=2013
[other-file2.jpg]
size=10
date=2012
; extra test cases
[file1]
strange=but okay
[level_1]
data=level 111
[level_1.level_2]
data=level 222
[level_1.level_2.level_3]
data=level 333
cool=yes
`)
const flat = flattenIniObject(iniData);
console.log(flat) |
The text was updated successfully, but these errors were encountered: