Enables the use of variables in JSON and JSON-like configuration files
npm i --save json-vars
const jVars = require('json-vars')
const config = {
foo: {
bar: '${env:BAR|default("bar")}'
},
baz: 'foo.bar is equal to ${self:foo.bar}'
}
jVars.resolve(config).then( res => console.log(res) )
// Output => {
// foo: {
// bar: 'bar'
// },
// baz: 'foo.bar is equal to bar'
// }
Scope, Variable and Transformer's names can contain any letter,
number, -
, .
or _
.
An Argument can be any string, number or boolean. String Arguments can contain the same character set of names.
If it's needed, a Variable's name or a string Argument can be wrapped in single or double quotes, in which case any character is allowed.
The Scope indicates to json-vars
the context on which it should resolve the
Variable's name.
A Transformer can modify the value coming from the Scope before the final substitution happens. If multiple Transformers are chained, they are applied left to right.
Variables can also be placed inside a Variable's name or a string Argument, this works only for unquoted strings.
So in "${self:foo.${env:ENV_VAR}}"
the inner Variable will be resolved and
replaced before the outer variale, while in "${self:'foo.${env:ENV_VAR}'}"
the outer Variable's name will be left as is.
Once a Variable is resolved, its placeholder gets replaced with its resolved value.
There are two possible replacement methods:
- If the Variable was contained in a longer string, the resolved value is stringified and then replaced.
- If the Variable's placeholder exactly match the string that contains it, the resolved value is returned as is and its type is preserved.
{
"num": 42,
"stringified": "num is ${self:num}",
"preserved": "${self:num}"
}
// becomes
{
"num": 42,
"stringified": "num is 42",
"preserved": 42
}
Resolves the Variable against the current shell environment.
References another property of the current input object.
Recover failures and return defaultValue
, otherwise it has no effect.
To run tests:
npm test
npm run lint