Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-vars

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-vars

Enables the use of variables in JSON and JSON-like configuration files

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

json-vars

Build Status

Enables the use of variables in JSON and JSON-like configuration files

Install with

npm i --save json-vars

Example usage

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'
//           }

Syntax

Variable

Variable syntax diagram

Transformer

Transformer syntax diagram

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.

Scope

The Scope indicates to json-vars the context on which it should resolve the Variable's name.

Transformer

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.

Nesting Variables

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.

Variable interpolation

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.
Example
{
  "num": 42,
  "stringified": "num is ${self:num}",
  "preserved": "${self:num}"
}
// becomes
{
  "num": 42,
  "stringified": "num is 42",
  "preserved": 42
}

Builtin Scopes and Transformers

Scopes
env

Resolves the Variable against the current shell environment.

self

References another property of the current input object.

Transformers
default(<defaultValue>)

Recover failures and return defaultValue, otherwise it has no effect.

Running the tests

To run tests:

npm test

And coding style tests

Run NSP and TSLint

npm run lint

Keywords

FAQs

Package last updated on 21 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc