Variable Replacer
Easily inject environment variables or provided variables to compose richer strings.
Install
npm install @universal-packages/variable-replacer
Global methods
replaceEnv(input: string, [enclosures: [string, string]])
Replace matches in the string with node env variables.
import { replaceEnv } from '@universal-packages/variable-replacer'
const string = 'NODE_ENV: {{NODE_ENV}}, TS_JEST: {{TS_JEST}}, JEST_WORKER_ID: {{ JEST_WORKER_ID }}'
const finalString = replaceEnv(string)
console.log(finalString)
Enclosures
You can provide your own enclosure characters to match for replacements.
import { replaceEnv } from '@universal-packages/variable-replacer'
const string = 'NODE_ENV: <<NODE_ENV>>, TS_JEST: <<TS_JEST>>, JEST_WORKER_ID: {{ JEST_WORKER_ID }}'
const finalString = replaceEnv(string, ['<<', '>>'])
console.log(finalString)
replaceVars(input: string, variables: Object, [enclosures: [string, string]])
Replace matches in the string with values provided in an object form.
import { replaceVars } from '@universal-packages/variable-replacer'
const string = 'key: {{ key }}, another: {{another}}'
const finalString = replaceVars(string, { key: 'key', another: 'value' })
console.log(finalString)
Enclosures
Same as with env you can provide your own enclosure characters to match for replacements.
import { replaceVars } from '@universal-packages/variable-replacer'
const string = 'key: # key #, another: #another#'
const finalString = replaceVars(string, { key: 'key', another: 'value' }, ['#', '#'])
console.log(finalString)
evaluateAndReplace(input: string, [options: Object])
Captures what is between the enclosures and evaluates it as a JS expression. The result is then used to replace the match in the string.
import { evaluateAndReplace } from '@universal-packages/variable-replacer'
const string = 'key: <% 1 + 1 %>, another: <% 2 + 2 %>'
const finalString = evaluateAndReplace(string)
console.log(finalString)
Options
-
scope
Object
You can provide your own scope to use in your expression.
import { evaluateAndReplace } from '@universal-packages/variable-replacer'
const string = 'key: <% cpusCount / 2 %>, another: <% mem / 2 %>'
const finalString = evaluateAndReplace(string, { cpusCount: 4, mem: 16 })
console.log(finalString)
-
enclosures
[string, string]
You can provide your own enclosure characters to match for replacements.
import { evaluateAndReplace } from '@universal-packages/variable-replacer'
const string = 'key: ${ 1 + 1 }$ , another: ${2+2}$'
const finalString = evaluateAndReplace(string, ['${', '}$'])
console.log(finalString)
evaluate(input: string, [scope: Object])
Evaluates the whole string as a JS expression and returns the actual JS result.
import { evaluate } from '@universal-packages/variable-replacer'
const string = 'a + b'
const result = evaluate(string, { a: 1, b: 2 })
console.log(result)
Combine replacements
You can pass your string through both function to get a final string
import { replaceEnv, replaceVars } from '@universal-packages/variable-replacer'
const string = 'NODE_ENV: <<NODE_ENV>>, TS_JEST: <<TS_JEST>>, key: {{ key }}'
let finalString = replaceEnv(string, ['<<', '>>'])
console.log(finalString)
finalString = replaceVars(string, { key: 'value' })
console.log(finalString)
cleanOrphanReplaceable(input: string, [enclosures: [string, string]])
Cleans the string from orphan replaceable enclosures. Useful in case the variables are not provided and you don;t want the value {{ value}}
as teh actual value and instead an empty value is more suitable.
import { cleanOrphanReplaceable } from '@universal-packages/variable-replacer'
const string = 'key: {{ key }}, another: {{another}}'
const finalString = cleanOrphanReplaceable(string)
console.log(finalString)
Enclosures
You can provide your own enclosure characters to match for replacements.
import { replaceVars } from '@universal-packages/variable-replacer'
const string = 'key: # key #, another: #another#'
const finalString = replaceVars(string, { key: 'key', another: 'value' }, ['#', '#'])
console.log(finalString)
Typescript
This library is developed in TypeScript and shipped fully typed.
Contributing
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
License
MIT licensed.