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
-
OptionsObject :
Object
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
-
.version ⇒
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()
JSON5
config.getContent() ⇒ getContent - get the parsed content of the file
Returns: JSON5
- the JSON5 content of the file
Example
let fileContent = config.getContent() // --> {_name: 'settings', ....}
Config
config.set(key, value) ⇒ 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})
any
config.get(key) ⇒ 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
boolean
config.exists(key) ⇒ exists - If a key exists
Param | Type |
---|---|
key | string |
Example
config.exists('key') // --> true/false
JSON
config.toJSON() ⇒ toJSON - Convert the config to a JSON compatible format
Returns: JSON
- The JSON object
Example
const JsonConfig = config.toJSON()
string
Config.version ⇒ Returns: string
- The version of the library
Example
const version = Config.version --> 0.1.1
Config
Config.create(name, opts) ⇒ 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(...)`
Object
OptionsObject : 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