config.js 804 Bytes
Newer Older
NGPixel's avatar
NGPixel committed
1 2 3 4
'use strict'

const _ = require('lodash')

Nick's avatar
Nick committed
5 6
const isoDurationReg = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/

NGPixel's avatar
NGPixel committed
7 8 9 10
module.exports = {
  /**
   * Parse configuration value for environment vars
   *
11 12 13 14
   * Replaces `$(ENV_VAR_NAME)` with value of `ENV_VAR_NAME` environment variable.
   *
   * Also supports defaults by if provided as `$(ENV_VAR_NAME:default)`
   *
NGPixel's avatar
NGPixel committed
15 16 17 18 19 20
   * @param {any} cfg Configuration value
   * @returns Parse configuration value
   */
  parseConfigValue (cfg) {
    return _.replace(
      cfg,
21 22
      /\$\(([A-Z0-9_]+)(?::(.+))?\)/g,
      (fm, m, d) => { return process.env[m] || d }
NGPixel's avatar
NGPixel committed
23
    )
Nick's avatar
Nick committed
24 25 26 27
  },

  isValidDurationString (val) {
    return isoDurationReg.test(val)
NGPixel's avatar
NGPixel committed
28 29
  }
}