Skip to content

truncate

Truncates a string to a specified length, appending a configurable suffix when the string exceeds the limit.

import { truncate } from "1o1-utils";
import { truncate } from "1o1-utils/truncate";
function truncate({ str, length, suffix }: TruncateParams): string
NameTypeRequiredDescription
strstringYesThe string to truncate
lengthnumberYesMaximum length (positive integer)
suffixstringNoString appended when truncated (default: "...")

string

truncate({ str: "Hello, World!", length: 5 });
// => "Hello..."
truncate({ str: "Hello, World!", length: 5, suffix: "" });
// => "Hello…"
truncate({ str: "Hi", length: 10 });
// => "Hi" — no truncation needed
  • Throws if str is not a string.
  • Throws if length is not a positive integer.
  • Returns the original string if its length does not exceed length.
  • The suffix is appended beyond the length limit.

shorten text, ellipsis, cut string, text overflow

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