steveukx / properties

Properties reader for Node.js
MIT License
77 stars 33 forks source link

Adding a property to a pre-existing section results in a duplicated section #30

Closed steve-sellers closed 4 years ago

steve-sellers commented 4 years ago

Given a file with contents

[section1]
s1foo=foo
s1bar=bar
[section2]
s2foo=foo
s2bar=bar
[section3]
s3foo=foo
s3bar=bar

the following code

const PropertiesReader = require('properties-reader');
let properties = PropertiesReader('/path/to/input/file');
properties.set(`section2.s2new`, 'new');
await properties.save('/path/to/output/file');

will result in

[section1]
s1foo=foo
s1bar=bar
[section2]
s2foo=foo
s2bar=bar
[section3]
s3foo=foo
s3bar=bar
[section2]
s2new=new

The use case here is of course modification of an existing ini file. IMO, the expected output would be the addition of the new section property in the pre-existing section, such as

[section1]
s1foo=foo
s1bar=bar
[section2]
s2foo=foo
s2bar=bar
s2new=new
[section3]
s3foo=foo
s3bar=bar

instead of duplicating the section at the bottom with the new property. In this manner, it seems the library does not properly support the addition of new properties in pre-existing sections that are not last in the file, only modification of such section's pre-existing properties. The output above is invalid as many parsers will throw a duplicate section error.

steveukx commented 4 years ago

Hi, thank you for the detailed issue report.

You should now find that version 2.0.0 (available on npm now) resolves this for you. If you have any other problems, please let me know.