upperFirst()
upperFirst(
str):string
Converts the first character of a string to uppercase and the rest to lowercase.
๐ Why is this a Hidden Gem?
Capitalize just the first character โ ideal for display names and UI labels.
DEPRECATED
Use capitalize from Arkhe directly instead.
Parametersโ
str: stringโ
The string to convert.
Returns: stringโ
The string with its first character converted to uppercase and the rest lowercased.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
upperFirst (Lodash, es-toolkit) ยท โ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleโ
// โ Deprecated approach
upperFirst('hello'); // => 'Hello'
upperFirst('HELLO'); // => 'Hello'
// โ
Recommended approach
import { capitalize } from 'pithos/arkhe/string/capitalize';
capitalize('hello'); // => 'Hello'
How it works?โ
Converts the first character to uppercase.
Deprecated: Use str[0].toUpperCase() + str.slice(1) directly.
Native Equivalentโ
// โ upperFirst('hello')
// โ
'hello'[0].toUpperCase() + 'hello'.slice(1)
Use Casesโ
Capitalize first letter ๐โ
Uppercase first character only.
const result = upperFirst('hello'); // 'Hello'
Format user-generated sentencesโ
Ensure the first letter of user-submitted text is capitalized for consistent display.
const formatComment = (text: string) => upperFirst(text.trim());
formatComment('great article, thanks!');
// => 'Great article, thanks!'
Build component names from stringsโ
Capitalize the first letter when constructing dynamic component or class names.
const getComponentName = (type: string) => `Icon${upperFirst(type)}`;
getComponentName('arrow'); // 'IconArrow'
getComponentName('check'); // 'IconCheck'