A TypeScript library for parsing configuration files
npm install configparser-tsConfigParser-TS is a TypeScript library that provides functionality to read, write, and manipulate configuration files similar to Python's configparser module.
You can install ConfigParser-TS via npm:
``bash`
npm install configparser-ts
`typescript`
import ConfigParser from 'configparser-ts';
`typescript
const configStr =
[webserver]
host = localhost
port = 8080
[database]
user = testuser
password = testuserpwd;
const config = new ConfigParser();
config.read(configStr);
console.log(config.get('webserver', 'host')); // Output: localhost
console.log(config.get('webserver', 'port')); // Output: 8080
`
`typescript
const config = new ConfigParser();
config.readFile('path/to/config.file');
console.log(config.get('database', 'user')); // Output: testuser
`
`typescript
const config = new ConfigParser();
config.read(configStr);
config.set('webserver', 'host', '127.0.0.1');
config.write('path/to/output.file');
`
- read(configStr: string): Reads configuration from a string.
- readFile(filePath?: string): Reads configuration from a specified file or from main.config in the current directory if no file path is specified.null
- get(section: string, key: string): Retrieves the value for a given section and key. Returns if not found.
- set(section: string, key: string, value: string): Sets the value for a given section and key.
- toString(): Returns the configuration data as a string.
- write(filePath?: string): Writes the configuration data to a specified file or to the last read configuration file.
Here's an example of a configuration file that ConfigParser-TS can read:
`plaintextExample configuration
[webserver]
host = localhost
port = 8080
[database]
user = testuser
password = testuserpwd
``
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License.