@aegenet/belt-interpolation
v2.3.0
Published
String interpolation
Downloads
86
Readme
@aegenet/belt-interpolation
Simple ECMAScript Interpolation
💾 Installation
yarn add @aegenet/belt-interpolation@^2.0.0
# or
npm i @aegenet/belt-interpolation@^2.0.0
📝 Usage
import { Interpolation, transform } from '@aegenet/belt-interpolation';
const interpolation = new Interpolation();
transform('Hello ${name}', {
name: 'David',
});
// 'Hello David'
transform('Hello ${firstName} ${lastName}', {
firstName: 'David',
lastName: 'Goodenough',
});
// 'Hello David Goodenough'
interpolation.transform('Hello ${firstName} ${lastName}', {
firstName: 'David',
lastName: null,
});
'Hello David '
transform('Hello ${firstName} ${lastName}', {
firstName: 'David',
});
// 'Hello David '
const interpolation = new Interpolation({
customDialects: {
spider: /(\\{0,1})¤¤([a-zA-Z0-9_\-]{1,})¤¤/,
},
});
interpolation.transform(
'Hello ¤¤firstName¤¤ ¤¤lastName¤¤',
{
firstName: 'David',
lastName: 'Goodenough',
},
{ dialect: 'spider' }
);
// 'Hello David Goodenough'
const interpolation = new Interpolation({
getValue: (ctx, propPath) => {
const props = propPath.split('.');
return String(
props.reduce((prev, curr) => {
return prev[curr];
}, ctx)
);
},
});
const result = interpolation.transform('Hello ${person.firstName} ${person.lastName}', {
person: {
firstName: 'David',
lastName: 'Goodenough',
},
}),
// result = 'Hello David Goodenough'