Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/kevinhermawan/strpolate
- Owner: kevinhermawan
- License: mit
- Created: 2023-01-05T13:02:10.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-13T13:36:48.000Z (over 1 year ago)
- Last Synced: 2024-12-26T21:05:01.775Z (8 days ago)
- Topics: dynamic-strings, javascript, string, string-formatting, string-interpolation, string-template, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/strpolate
- Size: 394 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
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)