Variable Replacer

npm version Testing codecov

Easily inject environment variables or provided variables to compose richer strings.

Install

Copy
npm install @universal-packages/variable-replacer

Global methods

replaceEnv(input: string, [enclosures: [string, string]])

Replace matches in the string with node env variables.

Copy
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)

// > 'NODE_ENV: test, TS_JEST: 1, JEST_WORKER_ID: 1'

Enclosures

You can provide your own enclosure characters to match for replacements.

Copy
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)

// > 'NODE_ENV: test, TS_JEST: 1, JEST_WORKER_ID: {{ JEST_WORKER_ID }}'

replaceVars(input: string, variables: Object, [enclosures: [string, string]])

Replace matches in the string with values provided in an object form.

Copy
import { replaceVars } from '@universal-packages/variable-replacer'

const string = 'key: {{ key }}, another: {{another}}'
const finalString = replaceVars(string, { key: 'key', another: 'value' })

console.log(finalString)

// > 'key: key, another: value'

Enclosures

Same as with env you can provide your own enclosure characters to match for replacements.

Copy
import { replaceVars } from '@universal-packages/variable-replacer'

const string = 'key: # key #, another: #another#'
const finalString = replaceVars(string, { key: 'key', another: 'value' }, ['#', '#'])

console.log(finalString)

// > 'key: key, another: value'

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.

Copy
import { evaluateAndReplace } from '@universal-packages/variable-replacer'

const string = 'key: <% 1 + 1 %>, another: <% 2 + 2 %>'
const finalString = evaluateAndReplace(string)

console.log(finalString)

// > 'key: 2, another: 4'

Options

  • scope Object You can provide your own scope to use in your expression.

    Copy
    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)
    
    // > 'key: 2, another: 8'
  • enclosures [string, string] You can provide your own enclosure characters to match for replacements.

    Copy
    import { evaluateAndReplace } from '@universal-packages/variable-replacer'
    
    const string = 'key: ${ 1 + 1 }$ , another: ${2+2}$'
    const finalString = evaluateAndReplace(string, ['${', '}$'])
    
    console.log(finalString)
    
    // > 'key: 2, another: 4'

evaluate(input: string, [scope: Object])

Evaluates the whole string as a JS expression and returns the actual JS result.

Copy
import { evaluate } from '@universal-packages/variable-replacer'

const string = 'a + b'
const result = evaluate(string, { a: 1, b: 2 })

console.log(result)

// > 3

Combine replacements

You can pass your string through both function to get a final string

Copy
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)

// > 'NODE_ENV: test, TS_JEST: 1, key: {{ key }}'

finalString = replaceVars(string, { key: 'value' })

console.log(finalString)

// > 'NODE_ENV: test, TS_JEST: 1, key: value'

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.

Copy
import { cleanOrphanReplaceable } from '@universal-packages/variable-replacer'

const string = 'key: {{ key }}, another: {{another}}'
const finalString = cleanOrphanReplaceable(string)

console.log(finalString)

// > 'key: , another: '

Enclosures

You can provide your own enclosure characters to match for replacements.

Copy
import { replaceVars } from '@universal-packages/variable-replacer'

const string = 'key: # key #, another: #another#'
const finalString = replaceVars(string, { key: 'key', another: 'value' }, ['#', '#'])

console.log(finalString)

// > 'key: "", another: ""'

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.