@booco-public/substitutem
TypeScript icon, indicating that this package has built-in type declarations

0.6.0 • Public • Published

substitutem - substitute substrings with context

This library exports methods that allows to replace substring by values from acontext or returned by a callback.

Installation

npm install --save @booco-public/substitutem

Usage

import { substitute } from '@booco-public/substitutem';

substitute('Hello {message}!', { message: 'World' }); // Hello World!
substitute('Subject: {booking.subject}', { 
  booking: { 
    subject : 'Hello World!'
  }
}); // Subject: Hello World!

substitute('Date: {date,dd-MM-yyyy}', {
  date: '2023-09-10T15:00:00.000Z'
},  {
  timezone: 'Europe/Berlin'
}); // Date: 10-09-2023


const context = {
  booking: {
    subject: 'Hello World!',
    start: 
  }
};

Usage with callback

import { substitute } from '@booco-public/substitutem';

substitute('Hello {message}!', (key: string) => (key === 'message' ? 'World' : `{${key}}`)); // Hello World!

Usage with async callback

import { substituteAsync } from '@booco-public/substitutem';

await substituteAsync('Hello {message}!', async (key: string) => (key === 'message' ? 'World' : `{${key}}`)); // Hello World!

Dependencies (3)

Dev Dependencies (20)

Package Sidebar

Install

npm i @booco-public/substitutem

Weekly Downloads

1

Version

0.6.0

License

MIT

Unpacked Size

31.2 kB

Total Files

11

Last publish

Collaborators

  • bladerunner2020