truncate
Truncates a string to a specified length, appending a configurable suffix when the string exceeds the limit.
Import
Section titled “Import”import { truncate } from "1o1-utils";import { truncate } from "1o1-utils/truncate";Signature
Section titled “Signature”function truncate({ str, length, suffix }: TruncateParams): stringParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| str | string | Yes | The string to truncate |
| length | number | Yes | Maximum length (positive integer) |
| suffix | string | No | String appended when truncated (default: "...") |
Returns
Section titled “Returns”string
Examples
Section titled “Examples”truncate({ str: "Hello, World!", length: 5 });// => "Hello..."
truncate({ str: "Hello, World!", length: 5, suffix: "…" });// => "Hello…"
truncate({ str: "Hi", length: 10 });// => "Hi" — no truncation neededEdge Cases
Section titled “Edge Cases”- Throws if
stris not a string. - Throws if
lengthis not a positive integer. - Returns the original string if its length does not exceed
length. - The suffix is appended beyond the
lengthlimit.
Also known as
Section titled “Also known as”shorten text, ellipsis, cut string, text overflow
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use truncate to shorten long descriptions in a card component