
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
@pairprog/text
Advanced tools
String templating as it was meant to be
Note: This package is part of the @pairprog/reforge monorepo. Do not install both packages at the same time to avoid namespace collisions.
This is a strongly opinionated implementation of string templating. It's basically JSX for text and solves many quirks of string interpolation and formatting.
Basically turns this:
const items = ["Hello", "World"];
const text: string = t`
Value: ${items.join(", ")}
Promise: ${Promise.resolve(items.join(", "))}
Callables:
Callable: ${() => items.join(", ")}
Callable Promise: ${() => Promise.resolve(items.join(", "))}
List of items:
${() => Promise.resolve(items.map((item) => `- ${item}`))}
`;
Into this:
Value: Hello, World
Promise: Hello, World
Callables:
Callable: Hello, World
Callable Promise: Hello, World
List of items:
- Hello
- World
What's backed in here ?
npm install @pairprog/text
yarn add @pairprog/text
t
automatically detects if the template is async or not and handles it accordingly.t
is async, it resolves all values in parralel with a Promise.all
await
expression.const sync: string = t`Hello ${"World"}`;
const async: Promise<string> = t`Hello ${Promise.resolve("World")}`;
/*
Asuming retrieveUserName and retrieveUserEmail are async functions
Both queries will be resolved in parallel
*/
const async: Promise<string> = t`Hello ${retrieveUserName()} ${retrieveUserEmail()}`;
t
will automatically trim unecessary whitespaces and indentations. This allows your code to remain indented and readable while still being able to use the template.t
will auto-join array values with a newline.t
will propagate indentation to each line of the nested values.const text: string = t`
Hello
${"- Item 1\n- Item 2"}
World
`;
Will be transformed into:
Hello
- Item 1
- Item 2
World
t
will call the function and then handle it's sync
or async
through the same logic as the base template.const items = ["Hello", "World"];
const text: string = t`
This is a list of items:
${items.map((item) => `- ${item}`)}
`;
t
is able to handle non-string objects as values. As long as they have a type
property.
JSON.stringify
ed and added to the template.string & { __extra: TExtraObject }
tParse(resultingString)
property.Text.String
Text.String
import { t, tParse, type Text } from "@pairprog/text";
// @ts-expect-error - The non-matching extra object will be rejected
const textFail: Text.String<{ type: "extra"; value: string }> =
t`Hello ${{ type: "other-type" as const, value: "Hello" }} World`;
// string & { __extra: { type: "extra"; value: string } } is equivalent to Text.String<{ type: "extra"; value: string }>
const text: string & { __extra: { type: "extra"; value: string } } =
t`Hello ${{ type: "extra" as const, value: "Hello" }} World`;
console.log(text);
// Hello %STR_EXTRA%{"type":"extra","value":"Hello"}%!STR_EXTRA% World
console.log(tParse(text));
// ["Hello ", { type: "extra", value: "Hello" }, " World"]
This feature is used to build Llm messages in the reforge monorepo.
FAQs
String templating as it was meant to be
We found that @pairprog/text demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.