Skip to content

capitalize

type: function

The capitalize function capitalizes either:

  • the first character in the whole string, or
  • each token after splitting by a separator.

Usage

import { capitalize } from '@mustib/utils';
capitalize('hello world'); // Hello World
capitalize('hello world', { onlyFirstWord: true }); // Hello world
capitalize('hello-world', { splitter: '-', joiner: ' ' }); // Hello World

Definition

function capitalize(
str: string,
options?: {
onlyFirstWord?: boolean;
splitter?: string;
joiner?: string;
},
): string { }

Parameters

  1. str
    type str = string;
    • Input string to capitalize.
  2. options
    type options = {
    onlyFirstWord?: boolean,
    splitter?: string,
    joiner?: string,
    };
    • options.onlyFirstWord
      • When true, only the first word is capitalized.
      • default:
        false
    • options.splitter
      • Delimiter used to split the input string before capitalization.
      • default:
        ' '
    • options.joiner
      • Delimiter used to join transformed parts.
      • default:
        splitter

Returns

type T = string;
  • Capitalized output string. If input is empty, returns the original value.