interpolate-json
Interpolate a Javascript Object (json) or string with (another) json - Advanced (or Substitution, as others may like to call it).
Minimalist & lightweight ;) approach to handle interpolation, which packs way more punch than simple string parameter replacement.
Supports:
${string}
interpolation${json}
interpolation${multi.level}
json notation- single
${= JavaScript.expression() =}
evaluation - custom
{{parameter_boundary}}
declaration
Install
npm install interpolate-json
yarn add interpolate-json
Usage
Declaration
const interpolation = require('interpolate-json').interpolation;
const { interpolation } = require('interpolate-json');
string
let someString = 'I want to be ${character} in ${business.type} by being a ${business.post}';
let values = {
character: 'a Hero',
business: {
type: 'saving people',
post: 'Doctor',
},
};
someString = interpolation.expand(someString, values);
console.log(someString);
let someString = "Hi, my name is '${USER_NAME}'. I'm ${USER_AGE}";
console.log(interpolation.expand(someString, process.env));
json
let myJson = {
port: '8080',
server: 'www.example.com',
user: 'abcd',
password: 'P@ss#ord',
url: 'https://${user}:${= encodeURIComponent(${password}) =}@${server}:${port}'
};
console.log(interpolation.expand(myJson));
{
"port": "8080",
"server": "www.example.com",
"user": "abcd",
"password": "P@ss#ord",
"url": "https://abcd:P%40ss%23ord@www.example.com:8080"
}
let myJson = {
port: '${PORT}',
server: 'www.example.com',
user: '${=${USER_NAME}.toLowerCase()=}',
password: '${USER_PASSWORD}',
url: 'https://${user}:${= encodeURIComponent(${password}) =}@${server}:${port}'
};
console.log(interpolation.expand(myJson));
{
"port": "8080",
"server": "www.example.com",
"user": "john",
"password": "P@ss#ord",
"url": "https://john:P%40ss%23ord@www.example.com:8080"
}
Notice that ${= =}
notation. It's a cool way to use JavaScript expression (not expressions, yet, just a single line).
Definition
Syntax: interpolation.expand(obj, values = null, options = null);
The expand
function takes 3 parameters
obj
The object to be interpolated. For string
type, values
must be provided. In case of json
type, it can interpolate itself if the required values are all present.
values
The values for the interpolated parameter placeholders (i.e. ${param-name}
). In case of json
type obj
, the values
override any of the existing obj
properties (like, overriding with Environment variables). If any of the parameters is not present, it's replaced by empty string (''
).
options
{
prefix: '${',
suffix: '}',
subKeyPointer: '.',
funcSpecifier: '=',
escapeSpecifier: '*'
}
more in Configurations
returns:
Based upon the type of the obj
. In case of any unsupported types, original obj
will be returned.
[Note: it does not change the actual obj
]
Configurations
The options
setup. Each section can be individually set through Environment Variables INTERPOLATE_OPTION_[CONFIGNAME] (or you can also set it inside values
or json
type obj
. See an extreme Example)
prefix
- type:
string
- default:
${
- Environment Variable override:
INTERPOLATE_OPTION_PREFIX
The prefix notation for an interpolation parameter.
suffix
- type:
string
- default:
}
- Environment Variable override:
INTERPOLATE_OPTION_SUFFIX
The suffix notation for an interpolation parameter.
subKeyPointer
- type:
string
- default:
.
- Environment Variable override:
INTERPOLATE_OPTION_SUBKEYPOINTER
The json object tree sub-node pointer for interpolation parameter. The possible value is restricted to dot(.
), hash(#
), underscore(_
) & colon(:
) (or it's multiple, like: ::
etc)
let json = {
a: 'A',
b: 'B',
c: {
d: 'D',
e: 'E',
f: {
g: 'G',
},
},
};
{
reference: '${c.d}';
}
{
reference: '${c#f#g}';
}
funcSpecifier *(read-only)
- type:
string
- fixed value:
=
The notation after prefix
& before suffix
to describe a function expression boundary. (e.g. ${= Func(${param1}, ${param2}) =}
).
escapeSpecifier *(read-only)
- type:
string
- fixed value:
*
The notation after prefix
to escape string expression for certain data-types (like number, boolean etc.).
This option is only applicable to json
type obj
let json = {
myKey: '${*keyValue}',
isKey: '${*boolValue}',
};
interpolatedJson = {
myKey: 123.45,
isKey: false,
};
Methods
const interpolation = require('interpolate-json');
expand()
Described so far since Declaration
& Definition
.
const interpolation = require('interpolate-json').interpolation;
interpolation.expand(obj, value);
const { interpolation } = require('interpolate-json');
interpolation.expand(obj, value);
debug()
Globally turn on debug
flag. If set to true
, it'll write console output of detailed operations to help debug why certain things are (not) working as expected.
Can also be turned on via setting Environment Variable INTERPOLATE_OPTION_DEBUG
to true
const interpolation = require('interpolate-json').interpolation;
interpolation.debug();
interpolation.debug(false);