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

0.5.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. For example,

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!

Readme

Keywords

none

Package Sidebar

Install

npm i @booco-public/substitutem

Weekly Downloads

19

Version

0.5.0

License

MIT

Unpacked Size

13.7 kB

Total Files

5

Last publish

Collaborators

  • bladerunner2020