Socket
Socket
Sign inDemoInstall

@danmasta/interpolate

Package Overview
Dependencies
10
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @danmasta/interpolate

Simple string interpolation helper


Version published
Weekly downloads
21
increased by2000%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Interpolate

Simple template string interpolation for strings and files

Features:

  • Easy to use
  • Interpolate template strings: {{VAR}}
  • Parses strings and files
  • Can interpolate from parameters and/or environment variables
  • Supports nested paths from complex parameters
  • Includes cli tool and node api

About

We needed a way to interpolate template variables from strings and files at build and/or run time. I wanted both a node api and cli tool to use for easy integration with jenkins, docker, or kubernetes, and also wanted the ability to interpolate with environment variables or manually specified parameters.

Usage

Add interpolate as a dependency for your app and install via npm

npm install @danmasta/interpolate --save

Require the package in your app

const interpolate = require('@danmasta/interpolate');

Options

namealiastypedescription
inputistringDirectory or file path to use when reading files. Default is undefined
outputostringDirectory to write parsed files to. Default is undefined
srcstringGlob pattern string to filter input file list, ex: **/*.yml. Default is undefined
stringsstringText string to parse. Default is undefined
stdin-booleanRead input from stdin. Default is false
envebooleanIf true will also interpolate with environment variables. Default is false
paramspobject|stringObject of key,value pairs to use for parameter matching. If string, it should either be a stringified json object, or a comma-separated key,value list: "key1=1,key2=2". Default is undefined
warnwbooleanIf true will write a message to stderr when a parameter is not found. Default is true
throwtbooleanIf true will throw an error when a parameter is not found. Default is false
defaultdstringDefault value to use when a parameter is not found. Default is ''
replaceMissingrstringIf false will not replace variables that are undefined. Default is false
formatfstringEnables or disables formatter functions. Default is true
delimiterstringWhat delimiter to use for parsing formatter names. Default is '|'
helphbooleanView the cli help menu

Methods

NameDescription
parse(str, opts)Parses a string with optional opts
file({ input, output, src })Parses a file or directory based on opts.input. Files are parsed then written to opts.output

Envstr

This package includes an extra cli utility called envstr. It formats json or table style data into key=val pairs that can be easily consumed in bash scripts or exported as environment variables.

Options

namealiastypedescription
stringsstringText string to parse. Default is undefined
stdin-booleanRead input from stdin. Default is undefined
jsonjbooleanHandle input as json. Default is false
keykstringIf input is json, parse data at specified key. Default is undefined
quotesqbooleanIf true add quotes around env values. Default is false
newlinenstringWhich character to use as newline delimeter. Default is '\n'
includeistringWhich keys to include in output: key1,key2
excludeestringWhich keys to exclude from output: key3,key4
capscstringIf true capitalizes the output key name. Default is false
exportxstringIf true adds the 'export' keyword in front of each output key. Default is false
helphbooleanView the cli help menu

Examples

Parse a text string

let params = {
    SRC: './src',
    BUILD: './build'
};

let str = 'input: {{SRC}} -> output: {{BUILD}}';

console.log(interpolate(str, { params }));

Parse a directory of files via cli

interpolate --env -i deploy -o build/deploy --src **/*.(yml|yaml)

Convert json data to env str format

envstr -s '{"KEY1":true,"KEY2":false}' --json --quotes

Testing

Tests are currently run using mocha and chai. To execute tests run npm run test. To generate unit test coverage reports run npm run coverage

Contact

If you have any questions feel free to get in touch

Keywords

FAQs

Last updated on 03 Feb 2024

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc