1.0.0 • Published 7 months ago

@sigyn/morphix v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

🚀 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

1.0.0

7 months ago