项目作者: cupcakearmy

项目描述 :
YAML Settings
高级语言: JavaScript
项目地址: git://github.com/cupcakearmy/memiens.git
创建时间: 2019-03-09T13:23:07Z
项目社区:https://github.com/cupcakearmy/memiens

开源协议:MIT License

下载


Memiens 🧠

YAML Settings utility. Nested getter with optional default value and a setter.

Typescript typings included

Quickstart 🚀

  1. npm i -s memiens
  1. import Memiens from 'memiens'
  2. // Initialize by giving a location for the config file.
  3. const MySettings = new Memiens('./config.yml')
  4. // This will throw an error if `someconfigvalue` is not found in the yaml
  5. const A = MySettings.get('someconfigvalue')
  6. // This will not throw an error if `B` is not found
  7. const B = MySettings.get('B', 'this value is returned and written to the yaml if the config entry does not exist')
  8. // This will look for nested values inside the yaml
  9. const C = MySettings.get('my.nested.config.value')
  10. // Write something
  11. MySettings.set('password', 'this is a secret')
  12. // Nested
  13. MySettings.set('config.db.user', 'my_db_user')
  14. MySettings.set('config.db.port', 1234)

API 📒

.get(setting, default [optional])

Behavior

Settings can be simple or nested properties.
If a default value is not provided it will throw an error in case the value is not set. Otherwise the default value will be written into the yaml and returned to the user.

Examples
  1. # test.yml
  2. db:
  3. user: myUser
  4. password: $ecr3t
  5. port: 1234
  6. simple: a string
  1. import Memiens from 'memiens'
  2. const Settings = new Memiens('./test.yml')
  3. const simple = Settings.get('simple') // 'a string'
  4. const db = Settings.get('db') // {user: 'myUser', ...}
  5. const user = Settings.get('db.user') // 'myUser'
  6. Settings.get('db').user === Settings.get('db.user') // true
  7. Settings.get('notfound') // throws Error
  8. Settings.get('notfound', 404) // Sets 'notfound' to 404 and returns 404

.set(setting, value)

Behavior

Settings can be simple or nested properties.
Values can be string, number, boolean or a nested object of those

Examples
  1. import Memiens from 'memiens'
  2. const Settings = new Memiens('./result.yml')
  3. const config = {
  4. a: true,
  5. db: {
  6. user: 'myUser',
  7. password: '$ecr3t',
  8. port: 1234,
  9. }
  10. }
  11. Settings.set('a', config.a)
  12. Settings.set('db', config.db)
  1. # result.yml
  2. a: true
  3. db:
  4. user: myUser
  5. password: $ecr3t
  6. port: 1234