1.0.6 • Published 8 months ago

strpolate v1.0.6

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

Strpolate

Minified size Test coverage Monthly download

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.

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.

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

strpolate(string: string, values: Record<string, string | number | boolean | undefined>): string

License

MIT License

1.0.2

9 months ago

1.0.1

10 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.0

1 year ago

0.0.3

1 year ago

0.0.4

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago