Package Exports
- hydrate-text
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hydrate-text) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
hydrate-text
A small, dependency-free and strongly typed template engine.
Features
- Light-weight. Less than 1 KiB (actual size depends on imported functions).
- Dependency-free. Only development dependencies are installed.
- Tree-shakable. Only imported code comes to your bundle.
- ES Modules and CommonJS syntax are supported.
- Strongly typed with TypeScript. All types are exported alongside with the core functions.
- Flexible interpolation options change. Change variables' markers in each function or use a special function to configure them once for further usage.
Examples
// ES Modules syntax
import { hydrateText } from "hydrate-text";
// CommonJS syntax
const hydrateText = require("hydrate-text").hydrateText;
// 'Hello, John Doe!'
console.log(hydrateText("Hello, {username}!", { username: "John Doe" }));
// '/users/1'
console.log(
hydrateText(
"/users/:userId",
{ userId: 1 },
/*
If the interpolation options object is passed, `prefix` and `suffix`
become empty strings by default, so you don't have to set it manually for
unpaired markers.
*/
{
prefix: ":",
},
),
);
Interpolation options can be configured via configureHydrateText
function,
that returns hydrateText
function as a result.
import { configureHydrateText } from "hydrate-text";
const route = "/users/:userId";
const routeWithCustomInterpolationOptions = "/users/(userId)";
const hydrateRoute = configureHydrateText({ prefix: ":" });
// '/users/2'
console.log(hydrateRoute(route, { userId: 2 }));
// '/users/3'
console.log(
hydrateRoute(
routeWithCustomInterpolationOptions,
{ userId: 3 },
{
prefix: "(",
suffix: ")",
},
),
);
Installation
npm
npm install hydrate-text
Yarn
yarn add hydrate-text
API
type ValueType = string | number | boolean;
type Variables = Record<string, ValueType>;
interface InterpolationOptions {
prefix?: string;
suffix?: string;
}
function hydrateText(
text: string,
variables?: Variables,
interpolationOptions?: InterpolationOptions,
) {}
function configureHydrateText(
interpolationOptions?: InterpolationOptions,
) => typeof hydrateText;
Background
Why I wrote "hydrate-text" library