Skip to content

Latest commit

 

History

History

morphix

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Morphix

Micro templating with function pipes support

npm version size ossf scorecard license

🚀 Getting Started

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

📚 Usage

import { morphix } from "@sigyn/morphix";

await morphix("Hello {name | capitalize}", { name: "john" });

Note

morphix() is async because it supports async functions

🌐 API

morphix

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.

📦 Functions

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.

🖋️ Interfaces

interface MorphixOptions {
    transform?: (data: {
        value: unknown;
        key: string;
    }) => unknown;
    ignoreMissing?: boolean;
}

Credits

This package is heavily inspired by pupa. Morphix is a fork with function support and doesn't support HTML escape.

License

MIT