1.0.3 • Published 7 years ago

js-native-template v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

JS Native Template

Installation

yarn add js-native-template

Examples

Basic

const createParser = require('js-native-template');

const parse = createParser();

(async () => {
  const result = await parse('Sum: 1 + 1 = ${1+1}');
  // result === 'Sum: 1 + 1 = 2'
})();

With custom methods

const createParser = require('js-native-template');

const methods = {
  echo: (x) => x,
  asyncEcho: async (x) => x
}

const parse = createParser(methods);

(async () => {
  const result = await parse('Echoes: ${echo("foo").toUpperCase()} ${asyncEcho("bar")}');
  // result === 'Echoes: FOO bar'
)();

Catch errors

const createParser = require('js-native-template');

const parse = createParser();

(async () => {
  try {
  const result = await parse('Throw error: ${nonExisting}');
  } catch(err){
    // err.message === 'nonExisting is not defined at 1:16' - position of occured error is relative in parsed string
  }
})();
1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago