Micro templating with function pipes support
This package is available in the Node Package Repository and can be easily installed with npm or yarn
$ npm i @sigyn/morphix
# or
$ yarn add @sigyn/morphix
import { morphix } from "@sigyn/morphix";
await morphix("Hello {name | capitalize}", { name: "john" });
Note
morphix()
is async because it supports async functions
async function morphix(
template: string,
data: Record<string, any> | unknown[],
options: MorphixOptions = {}
): Promise<string>
template
Type: string
Text with placeholders for data properties.
data
Type: object | unknown[]
Data to interpolate into template.
The keys should be a valid JS identifier or number (a-z, A-Z, 0-9).
options
Type: object
ignoreMissing
Type: boolean
Default: false
By default, Morphix throws a MissingValueError when a placeholder resolves to undefined. With this option set to true, it simply ignores it and leaves the placeholder as is.
transform
Type: (data: { value: unknown; key: string }) => unknown
(default: ({value}) => value)
)
Performs arbitrary operation for each interpolation. If the returned value was undefined, it behaves differently depending on the ignoreMissing option. Otherwise, the returned value will be interpolated into a string and embedded into the template.
MissingValueError Exposed for instance checking.
capitalize
Capitalize the first letter.
dnsresolve
Retrieve host of a given IP. It uses dns.reverse
.
If it fails to retrieve the host, it returns the ip instead.
interface MorphixOptions {
transform?: (data: {
value: unknown;
key: string;
}) => unknown;
ignoreMissing?: boolean;
}
This package is heavily inspired by pupa. Morphix is a fork with function support and doesn't support HTML escape.
MIT