@triskel/con-text

Text processing with context definitions

Usage no npm install needed!

<script type="module">
  import triskelConText from 'https://cdn.skypack.dev/@triskel/con-text';
</script>

README

@triskel/con-text

npm Build Status Build Status dependencies Status GitHub license

npm install -D @triskel/con-text

Examples

import createConText from '@triskel/con-text';

var TEXT = createConText();

Evaluating expressions


TEXT.eval(' foo ? foo : 'bar' ', { foo: 'foobar' });
// results: 'foobar'

TEXT.eval(' foo ? foo : 'bar' ', { foo: false });
// results: 'bar'

Using Filters


TEXT.defineFilter('amount', function (amount) {
  return parseInt(amount/100) + ',' + amount%100;
});

TEXT.eval(' price | amount ', { price: 12345 });
// results: '123,45'

/* ----------------------- */

TEXT.defineFilter('title', function (input_text) {
  return 'title: ' + input_text;
});

TEXT.eval(' foo | title ', { foo: 'bar' });
// results: 'title: bar'

TEXT.eval(' foo | title ', { foo: 'foobar' });
// results: 'title: foobar'

Interpolating text

TEXT.defineFilter('amount', function (amount) {
  return parseInt(amount/100) + ',' + amount%100;
});

TEXT.interpolate('current price: {{ price | amount }}€', { price: 12345 });
// results: 'current price: 123,45€'