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

@universal-packages/variable-replacer

Package Overview
Dependencies
Maintainers
0
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@universal-packages/variable-replacer

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

  • 1.8.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
increased by0.22%
Maintainers
0
Weekly downloads
 
Created
Source

Variable Replacer

npm version Testing codecov

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)

// > 'NODE_ENV: test, TS_JEST: 1, JEST_WORKER_ID: 1'
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)

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

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.

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.

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.

    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.

    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.

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

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.

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.

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.

FAQs

Package last updated on 30 Nov 2024

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