1.0.0 • Published 6 years ago

type-template v1.0.0

Weekly downloads
2
License
MIT
Repository
-
Last release
6 years ago

type-template

Simple, type-safe string interpolation

// Import the tpl function and some default stringifiers
import tpl, { NUMBER, STRING } from 'type-template';

// Define a custom stringifier, a fancy name for a function mapping a value to a string
const DATE = (value: Date) => value.toLocaleString();

// Create a template
const myTemplate = tpl`Something ${{num:NUMBER}} something ${{myDate:DATE}} something ${{str:STRING}}`;

// myTemplate is now a function you can use for interpolation
const myString = myTemplate({
    // These arguments are all type-checked
    num: 96,
    myDate: new Date(),
    str: 'some other thing',
});

// -> 'Something 96 something 10/8/2018, 11:56:52 AM something some other thing'