Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kevinhermawan/strpolate

Fast string interpolation with built-in type validation
https://github.com/kevinhermawan/strpolate

dynamic-strings javascript string string-formatting string-interpolation string-template typescript

Last synced: 4 days ago
JSON representation

Fast string interpolation with built-in type validation

Awesome Lists containing this project

README

        

# Strpolate

![Minified size](https://img.shields.io/bundlephobia/min/strpolate) ![Test coverage](https://img.shields.io/codecov/c/github/kevinhermawan/strpolate) ![Monthly download](https://img.shields.io/npm/dm/strpolate)

Strpolate is a JavaScript library that offers fast string interpolation by replacing placeholders in the string with corresponding values from an object. It also includes type validation to ensure that the values being inserted match the expected data type of the placeholder.

## Features

- Fast string interpolation
- Built-in type validation
- Supports Deno via NPM
- Zero dependencies

## Installation

To install `strpolate`, run the following command:

**NPM**

```
npm install strpolate
```

**Yarn**

```
yarn add strpolate
```

**pnpm**

```
pnpm add strpolate
```

## Usage

You can use the following type specifiers in the placeholders: `%s` or `%string` for string values, `%n` or `%number` for number values, and `%b` or `%boolean` for boolean values. If no type specifier is provided, the default type is assumed to be `string`.

```ts
import strpolate from "strpolate";
// import strpolate from "npm:strpolate"; // (for Deno)

const result = strpolate(
"The value of message is %{message}, the value of count is %number{count}, and the value of enabled is %boolean{enabled}",
{ message: "Hello, world!", count: 5, enabled: true }
);

// result is "The value of message is Hello, world!, the value of count is 5, and the value of enabled is true"
```

If a placeholder is missing a corresponding value in the `values` object, or if the value has the wrong type, `strpolate` will throw an error.

```ts
import strpolate from "strpolate";
// import strpolate from "npm:strpolate"; // (for Deno)

// Throws an error because the 'count' placeholder is missing a corresponding value in the 'values' object
strpolate(
"The value of message is %{message}, and the value of count is %number{count}",
{ message: "Hello, world!" }
);

// Throws an error because the value for the 'count' placeholder is a string, but the placeholder expects a number
strpolate(
"The value of message is %{message}, and the value of count is %number{count}",
{ message: "Hello, world!", count: "5" }
);
```

## Syntax

```ts
strpolate(string: string, values: Record): string
```

## License

[MIT License](/LICENSE)