All Projects → vasilii-kovalev → hydrate-text

vasilii-kovalev / hydrate-text

Licence: MIT license
A small, dependency-free and strongly typed template engine.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to hydrate-text

Pupa
Simple micro templating
Stars: ✭ 231 (+413.33%)
Mutual labels:  template-engine, string
AgileStringDecryptor
a dynamic Agile.NET string decryptor that relies on invoke by wwh1004 | Version : 6.X
Stars: ✭ 24 (-46.67%)
Mutual labels:  string
morestachio
Lightweight, powerful, flavorful, template engine.
Stars: ✭ 45 (+0%)
Mutual labels:  template-engine
goin
`in` operator for go
Stars: ✭ 17 (-62.22%)
Mutual labels:  string
tenjin
📝 A template engine.
Stars: ✭ 15 (-66.67%)
Mutual labels:  template-engine
ftmpl
Fast typesafe templating for golang
Stars: ✭ 61 (+35.56%)
Mutual labels:  template-engine
comment-mark
Interpolate strings with HTML comment markers!
Stars: ✭ 21 (-53.33%)
Mutual labels:  string
simple-template
Text templating processor for SWI-Prolog.
Stars: ✭ 29 (-35.56%)
Mutual labels:  template-engine
compact str
A memory efficient string type that can store up to 24* bytes on the stack
Stars: ✭ 322 (+615.56%)
Mutual labels:  string
DigitText
The module allows to translate numbers into a text equivalent. This is important in the billing.
Stars: ✭ 22 (-51.11%)
Mutual labels:  string
frep
Generate file using template from environment, arguments, json/yaml/toml config files
Stars: ✭ 135 (+200%)
Mutual labels:  template-engine
pretty-hex
A Rust library providing pretty hex dump.
Stars: ✭ 34 (-24.44%)
Mutual labels:  zero-dependency
string-math
Evaluates a math expression from a string. Supports variables and custom operators.
Stars: ✭ 14 (-68.89%)
Mutual labels:  string
titef
🌠 A tiny, lightning-fast, zero-dependecies JavaScript test framework 🌠
Stars: ✭ 19 (-57.78%)
Mutual labels:  zero-dependency
TemplateEngine
Design and build web applications with components using only your web browser
Stars: ✭ 41 (-8.89%)
Mutual labels:  template-engine
opentbs
With OpenTBS you can merge OpenOffice - LibreOffice and Ms Office documents with PHP using the TinyButStrong template engine. Simple use OpenOffice - LibreOffice or Ms Office to edit your templates : DOCX, XLSX, PPTX, ODT, OSD, ODP and other formats. That is the Natural Template philosophy.
Stars: ✭ 48 (+6.67%)
Mutual labels:  template-engine
as-string-sink
An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript
Stars: ✭ 23 (-48.89%)
Mutual labels:  string
dry
Dry is a new template engine and language, and is a superset of Shopify's Liquid, with first-class support for advanced inheritance features, and more. From the creators of Enquirer, Assemble, Remarkable, and Micromatch.
Stars: ✭ 66 (+46.67%)
Mutual labels:  template-engine
Textrude
Code generation from YAML/JSON/CSV models via SCRIBAN templates
Stars: ✭ 79 (+75.56%)
Mutual labels:  template-engine
strutil
Golang metrics for calculating string similarity and other string utility functions
Stars: ✭ 114 (+153.33%)
Mutual labels:  string

hydrate-text

A small, dependency-free and strongly typed template engine.

Version Downloads Depend packages

Minified size Minified and gzipped size

Types Code Coverage Known Vulnerabilities

Features

  • Light-weight. Less than 1 KiB (actual size depends on imported functions).
  • Dependency-free. Only bundled JavaScript files and TypeScript type declarations are included.
  • Tree-shakable. Only imported code comes to your bundle.
  • 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

import { hydrateText } from "hydrate-text";

// "Hello, John!"
console.log(
  hydrateText("Hello, {username}!", {
    username: "John",
  }),
);

// "/users/42"
console.log(
  hydrateText(
    "/users/:id",
    { id: 42 },
    {
      prefix: ":",
      suffix: "",
    },
  ),
);

TypeScript checks that all the variables defined in the given string are provided.

console.log(
  hydrateText(
    "Hello, {username}!",
    // No errors
    {
      username: "John",
    },
  ),
);

console.log(
  hydrateText(
    "Hello, {username}!",
    // Error: `username` is missing
    {},
  ),
);

Interpolation options can be configured via configureHydrateText function, that returns hydrateText function as a result.

import { configureHydrateText } from "hydrate-text";

const hydrateRoute = configureHydrateText({ prefix: ":" });

// "/users/42"
console.log(hydrateRoute("/users/:id", { id: 42 }));

// "/users/42"
console.log(
  hydrateRoute(
    "/users/(id)",
    { id: 42 },
    {
      prefix: "(",
      suffix: ")",
    },
  ),
);

Check out other correct and incorrect usage examples.

Installation

npm

npm install hydrate-text

Yarn

yarn add hydrate-text

API (simplified)

type ValueType = string | boolean | number | bigint;

interface InterpolationOptions {
  prefix: string;
  suffix: string;
}

function hydrateText(
  text: string,
  variables?: Record<string, ValueType>,
  interpolationOptions?: InterpolationOptions,
) {}

function configureHydrateText(
  interpolationOptions: InterpolationOptions,
) => typeof hydrateText;

Check out the "Types" section in the source file for more information.

Known issues

  • SyntaxError: Unexpected token 'export'.

    The problem appears when running tests using Jest. That's because of lack of CommonJS support. A solution can be found here. Not supporting CommonJS is intended, since all the industry is moving towards ES modules, and CommonJS will be retired at some point.

Background

Why I wrote "hydrate-text" library

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].