0.1.3 • Published 4 years ago

@dorianb/config-js v0.1.3

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

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

Typedefs

Config

new Config(name, opts)

constructor - create or retrieve a configuration

Returns: Config - the config object

ParamTypeDescription
namestringthe name of the config (also the name of the file)
optsOptionsObject

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
ParamType
keystring
valuestring

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

ParamType
keystring

Example

config.get()      // --> return the whole config
config.get('key') // --> value

config.exists(key) ⇒ boolean

exists - If a key exists

ParamType
keystring

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

ParamTypeDescription
namestringthe name of the config (also the name of the file)
optsOptionsObject

Example

const settings = Config.create('settings', {extension: '.config'}) // same as `new Config(...)`

OptionsObject : Object

Properties

NameTypeDefaultDescription
extensionstring"'json5'"The extensions to use
filenamestring"'config.json5'"The name of the default config file
folderstring"'./config'"The folder of the default config file
overwritebooleanfalseIf the file should be re-create during initialization

2020 © Dorian Beauchesne

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago