Skip to content

ts-ign0re/stringinject

 
 

Repository files navigation

@tronin/stringinject

CI npm version TypeScript

Inject an array, or an object of items in to a string replacing selected values.

Install

npm install @tronin/stringinject

Usages

Arrays

import stringInject from '@tronin/stringinject';

If you pass stringInject a first parameter of a string which includes placeholder values in the format of {0}, {1}, {2} etc. A second parameter which is an Array of 3 string items. It will replace the placeholders, with the Array items in the order specified.

var string = stringInject("This is a {0} string for {1}", ["test", "stringInject"]);

// This is a test string for stringInject

Note

  • Package is ESM-only and includes TypeScript types.
  • In CommonJS, use dynamic import: const { default: stringInject } = await import('@tronin/stringinject').

Objects

You can also pass in an object with keys that exist as placeholders within the string. It will then find the placeholder based on your key and replace it with the value of that key from your object.

var str = stringInject("My username is {username} on {platform}", { username: "tjcafferkey", platform: "GitHub" });

// My username is tjcafferkey on Github

About

Inject an array of items in to a string replacing selected values.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • JavaScript 57.0%
  • TypeScript 43.0%