@dorianb/config-js
v0.1.3
Published
A simple library for NodeJS to deal with config files
Downloads
3
Readme
Config library
Installation
To install the package, just run :
bash npm install --save @dorianb/config-js
Then in the .js
file :
const Config = require('@dorianb/config-js')
const config = new Config('path/to/the/config')
if (config.exists('key')) {
let value = config.get('key')
config.set('key', value)
}
Documentation
Classes
Typedefs
Config
- Config
- new Config(name, opts)
- instance
- .save()
- .getContent() ⇒ JSON5
- .set(key, value) ⇒ Config
- .get(key) ⇒ any
- .exists(key) ⇒ boolean
- .toJSON() ⇒ JSON
- static
- .version ⇒ string
- .create(name, opts) ⇒ Config
new Config(name, opts)
constructor - create or retrieve a configuration
Returns: Config - the config object
| Param | Type | Description | | --- | --- | --- | | name | string | the name of the config (also the name of the file) | | opts | OptionsObject | |
Example
const keybindings = new Config('keybindings')
const settings = new Config('settings.json')
config.save()
save - Save the config file
Example
config.save()
config.getContent() ⇒ JSON5
getContent - get the parsed content of the file
Returns: JSON5 - the JSON5 content of the file
Example
let fileContent = config.getContent() // --> {_name: 'settings', ....}
config.set(key, value) ⇒ Config
set - Set an config value
Chainable
Throws:
- ConfigKeyError
| Param | Type | | --- | --- | | key | string | | value | string |
Example
config.set('property', 'some value')
config.set(randomObject)
config
.set({position: {x:1, y:5}})
.set('speed', {vx: 2, vy: 0})
config.get(key) ⇒ any
get - Get a key of the config
Returns: any - the value associated with the key
| Param | Type | | --- | --- | | key | string |
Example
config.get() // --> return the whole config
config.get('key') // --> value
config.exists(key) ⇒ boolean
exists - If a key exists
| Param | Type | | --- | --- | | key | string |
Example
config.exists('key') // --> true/false
config.toJSON() ⇒ JSON
toJSON - Convert the config to a JSON compatible format
Returns: JSON - The JSON object
Example
const JsonConfig = config.toJSON()
Config.version ⇒ string
Returns: string - The version of the library
Example
const version = Config.version --> 0.1.1
Config.create(name, opts) ⇒ Config
Chainable
Returns: Config - the config object
| Param | Type | Description | | --- | --- | --- | | name | string | the name of the config (also the name of the file) | | opts | OptionsObject | |
Example
const settings = Config.create('settings', {extension: '.config'}) // same as `new Config(...)`
OptionsObject : Object
Properties
| Name | Type | Default | Description | | --- | --- | --- | --- | | [extension] | string | "'json5'" | The extensions to use | | [filename] | string | "'config.json5'" | The name of the default config file | | [folder] | string | "'./config'" | The folder of the default config file | | [overwrite] | boolean | false | If the file should be re-create during initialization |
2020 © Dorian Beauchesne