Skip to content

capitalize

Capitalizes the first character of a string, with an option to preserve or lowercase the remaining characters.

import { capitalize } from "1o1-utils";
import { capitalize } from "1o1-utils/capitalize";
function capitalize({ str, preserveRest }: CapitalizeParams): string
NameTypeRequiredDescription
strstringYesThe string to capitalize
preserveRestbooleanNoIf true, keeps the rest of the string as-is; if false, lowercases it (default: false)

string

capitalize({ str: "hello" });
// => "Hello"
capitalize({ str: "hELLO", preserveRest: true });
// => "HELLO" — rest preserved
capitalize({ str: "hELLO" });
// => "Hello" — rest lowercased
  • Throws if str is not a string.
  • Returns "" for empty string.

uppercase first, first letter uppercase, initial cap

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 capitalize to format user display names