@booco-public/substitutem
v0.6.0
Published
This library exports methods that allows to replace substring by values from acontext or returned by a callback.
Downloads
10
Readme
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!