UNPKG

typelit

Version:

A type-safe string templating library for TypeScript

108 lines (105 loc) 3.49 kB
'use strict'; /** * Creates a prompt variable factory function for creating strongly-typed variable references. * Used to create type-specific variable creators (e.g., `string`, `number`, `boolean`). * * @param options - Configuration options for the variable type * @param options.stringify - Custom function to convert values to strings. Useful when the default * `String()` conversion isn't suitable (e.g., for formatting numbers or dates). * If not provided, the standard `String()` function is used. * * Returns a function that creates variable references with nested paths. * * ```ts * // Basic usage with default string conversion * const stringVar = createType<string>(); * const myVar = stringVar("user", "name"); // Var<["user", "name"], string> * * // Custom string conversion for numbers * const numberVar = createType<number>({ * stringify: (n) => n.toFixed(2) // Convert numbers to strings with 2 decimal places * }); * const priceVar = numberVar("product", "price"); // Var<["product", "price"], number> * ``` */ function createType(options = {}) { return function (...names) { return { _extract: (ctx) => names.reduce((obj, n) => obj[n], ctx), stringify: options.stringify ?? String, }; }; } /** * Creates a type-safe prompt template function from a template literal and variables. * The resulting function accepts a context object and returns the formatted string. * * ```ts * const template = typelit`Hello ${typelit.string("user", "name")}!`; * const result = template({ user: { name: "Alice" } }); // "Hello Alice!" * ``` */ function typelit(strings, ...vars) { return (ctx) => vars.reduce( // @ts-expect-error type of `v` loses specificity during iteration (acc, v, i) => acc + v.stringify(v._extract(ctx)) + strings[i + 1], strings[0]); } /** * Variable creator for boolean values. * ```ts * const template = typelit`The switch is ${typelit.boolean("enabled")}` * template({ enabled: true }) // "The switch is true" * ``` */ typelit.boolean = createType(); /** * Variable creator for string values. * ```ts * const template = typelit`Hello ${typelit.string("name")}!` * template({ name: "Alice" }) // "Hello Alice!" * ``` */ typelit.string = createType(); /** * Variable creator for number values. * ```ts * const template = typelit`You are number ${typelit.number("position")} in line` * template({ position: 3 }) // "You are number 3 in line" * ``` */ typelit.number = createType(); /** * Variable creator for bigint values. * ```ts * const template = typelit`Transaction ID: ${typelit.bigint("id")}` * template({ id: 123456789n }) // "Transaction ID: 123456789" * ``` */ typelit.bigint = createType(); /** * Variable creator for Date values. * * ```ts * const template = typelit`Event date: ${typelit.date("eventDate")}` * template({ eventDate: new Date("2023-05-15") }) // "Event date: Mon May 15 2023 00:00:00 GMT+0000 (Coordinated Universal Time)" * ``` */ typelit.date = createType(); /** * Variable creator for any value type. * Stringifies the value to JSON with 2-space indentation. * * ```ts * const template = typelit`Data: ${typelit.json("data")}` * template({ data: { name: "Alice", age: 30 } }) * // "Data: { * // "name": "Alice", * // "age": 30 * // }" * ``` */ typelit.json = createType({ stringify: (obj) => JSON.stringify(obj, null, 2), }); module.exports = typelit; //# sourceMappingURL=index.cjs.map