1.0.5 • Published 10 months ago

str-interpolator v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

str-interpolator

A TypeScript utility function to replace placeholders in a string with values from an object, supporting both simple and nested object paths.

Build Fast Lightweight Npm-Version Trusted No Dependency

Installation

Install the package via npm:

npm install str-interpolator

Usage

Basic Example

Use the strInterpolator function to replace placeholders in a string with corresponding values from an object.

Basic Example

Nested Object Example

The function supports nested objects, allowing you to replace placeholders with values deep within an object structure.

Nested Object Example

Function Signature

function strInterpolator<T extends Record<string, any>>(str: string, values: T): string;

Parameters

  • str: The string containing placeholders in the format ${key} or ${nested.key}.
  • values: An object with keys matching the placeholders in the string.

Returns

A new string with placeholders replaced by corresponding values from the values object.

Features

  • Simple placeholders: Replace placeholders like ${name} with corresponding values from an object.
  • Nested object support: Replace placeholders like ${(link unavailable)} by accessing nested objects.
  • TypeScript support: Fully typed for use in TypeScript projects.

Check Out My Other Packages

Explore more useful packages by visiting my npm profile. Made with ❤️ by Ashish