@dorianb/config-js

0.1.3 • Public • Published

Config library

npm version npm version

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

Config

Typedefs

OptionsObject : Object

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

Readme

Keywords

Package Sidebar

Install

npm i @dorianb/config-js

Weekly Downloads

0

Version

0.1.3

License

MIT

Unpacked Size

16.2 kB

Total Files

8

Last publish

Collaborators

  • dorianb