uniparse
v1.0.7
Published
Config parsing library with a unified API for different types of configs.
Downloads
24
Maintainers
Readme
uniparse
Config parsing library with a unified API for different types of configs.
Supported filetypes:
- Yaml
- Properties
- JSON
Usage
Install the module:
npm install uniparse
Use the module:
var uniparse = require('uniparse');
var serverConfig = '/path/to/config/file/server.properties';
uniparse.readConfig(serverConfig, function(err, config) {
// config is a plain JS object. You can easily JSON encode and decode it and no data will be lost.
config['server-port'] = 25566;
config['gamemode'] = 1;
uniparse.writeConfig(serverConfig, config, function(err) {
// Saved if there is no error
});
});
API
uniparse.readConfig(file[, options], callback)
Reads a file as a config and returns the object representation via the callback.
file
can be either a file path (String) or the file contents as either a String or a Buffer. For the latter please provide theextension
anddata
properties.extension
should be the extension of the file (with or without leading dot) anddata
should be set to true.
The options parameter is optional.
Options:
data - Boolean By default
readConfig()
assumes that the first argument is a file path, if you are prividing a String or a Buffer object with the actual config data this option needs to be set to true. When using this option, make sure to also set theextension
option.extension | ext - String This option forces a certain extension to be used. When used uniparse will use the parser for this type of file.
Example:
uniparse.readConfig('{"some": "JSON", "object": true}', {extension: 'json', data: true}, function(err, object) {});
uniparse.writeConfig(file, object[, options], callback)
Writes a config to disk.
Options:
extension | ext - String This option forces a certain extension to be used. When used uniparse will use the parser for this type of file.
extend - Boolean Will extend the config file with the object. This uses
lodash.extend(fileConfig, yourExtension)
. Extend will overwrite properties that are already defined in the original file.pretty - Boolean Returns a pretty version of the config (where applicable, like JSON). Will prettify by default.
uniparse.stringifyConfig(object[, options], callback)
Stringifies a config object. Useful when sending the config file.
Options:
extension | ext - String This option forces a certain extension to be used. When used uniparse will use the parser for this type of file.
pretty - Boolean Returns a pretty version of the config (where applicable, like JSON).
Writing your own parser
- Create a new file in the lib/parsers folder. Make sure to name it something meaningful.
- Make sure to write the following 4 methods:
- readConfig(file, options, callback) // Options: none - Reads the config from a file.
- writeConfig(file, data, options, callback) // Options: extend, pretty - Writes the config to a file
- parseConfig(string, options, callback) // Options: none - Parses the config from a string.
- stringifyConfig(object, options, callback) // Options: pretty - Converts a plain JS object to a string.
- The methods don't need to worry about undefined parameters, the main program handles those.
- Make sure you pass on the options object to the parsing library, so the user can provide extra options for that too.
- Export a property called
extensions
that includes all the extensions you support (without leading dot). - Make sure
gulp lint
doesn't throw errors. - Submit a PR.
Exceptions to GPLv2 license
If the config format you're coding is proprietary, you are not required to disclose the source.