@aegenet/belt-interpolation
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

npm version

@aegenet/belt-interpolation

Simple ECMAScript Interpolation

💾 Installation

yarn add @aegenet/belt-interpolation@^1.4.0
# or
npm i @aegenet/belt-interpolation@^1.4.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'

Readme

Keywords

Package Sidebar

Install

npm i @aegenet/belt-interpolation

Weekly Downloads

4

Version

1.5.0

License

MIT

Unpacked Size

8.16 kB

Total Files

7

Last publish

Collaborators

  • agenet