user4022619
user4022619

Reputation:

Nodejs .ini files titles

.ini file:

[1]
1X = true
[2]
2X = true
[5] 
3X = true

How can i get titles [1], [2] and [5] from .ini files with ini module : https://www.npmjs.org/package/ini

Upvotes: 1

Views: 2843

Answers (1)

mscdex
mscdex

Reputation: 106736

The example in the ini readme shows how to access them. If you want a list of them, just call Object.keys() on the return value:

var fs = require('fs'),
    ini = require('ini');

var config = ini.parse(fs.readFileSync('./config.ini', 'utf8'));
console.dir(Object.keys(config));

If you want to be strict, you can check each property to make sure the corresponding value is an object (otherwise it is a key=value outside of a section). This would prevent the property scope (from the ini readme example) from being treated as a section. Example:

var sections = [],
    keys = Object.keys(config);
for (var i = 0; i < keys.length; ++i) {
  if (typeof config[keys[i]] === 'object')
    sections.push(keys[i]);
}
console.dir(sections);

Upvotes: 3

Related Questions