Skip to content

uncapitalize function ​

Lower-cases the first character of a string and leaves the rest unchanged: the reverse of capitalize.

ts
import { uncapitalize } from 'utils';

Signature ​

ts
export function uncapitalize(input: string): string

Parameters ​

NameDescription
inputThe string.

Returns ​

The string with its first character in lower case.

Example ​

ts
uncapitalize('RingBuffer'); // 'ringBuffer'
uncapitalize('Élan'); // 'élan'

Tests ​

Line coverage: 100 %.

  • ✅ uncapitalizes "RingBuffer" as "ringBuffer"
  • ✅ uncapitalizes "Élan" as "élan"
  • ✅ uncapitalizes "already" as "already"
  • ✅ uncapitalizes "HTTP" as "hTTP"
  • ✅ uncapitalizes "" as ""

Source ​

src/string/uncapitalize.ts (on GitHub)

Source code
ts
/**
 * Lower-cases the first character of a string and leaves the rest unchanged: the reverse of `capitalize`.
 *
 * @param input - The string.
 * @returns The string with its first character in lower case.
 * @example
 * uncapitalize('RingBuffer'); // 'ringBuffer'
 * uncapitalize('Élan'); // 'élan'
 */
export function uncapitalize(input: string): string {
  const first = input.codePointAt(0);
  if (first === undefined) {
    return input;
  }
  const firstCharacter = String.fromCodePoint(first);
  return firstCharacter.toLowerCase() + input.slice(firstCharacter.length);
}
Test file
ts
import { uncapitalize } from './uncapitalize';

describe(uncapitalize, () => {
  it.for([
    ['RingBuffer', 'ringBuffer'],
    ['Élan', 'élan'],
    ['already', 'already'],
    ['HTTP', 'hTTP'],
    ['', ''],
  ] as const)('uncapitalizes %j as %j', ([input, expected]) => {
    expect(uncapitalize(input)).toBe(expected);
  });
});

Keywords: uncapitalize, strings, function

Generated from the JSDoc of the sources.