Skip to content

upperCase function ​

Converts a string to upper-case words separated by spaces, splitting identifiers such as camelCase.

ts
import { upperCase } from 'utils';

Signature ​

ts
export function upperCase(input: string): string

Parameters ​

NameDescription
inputThe identifier or sentence to convert, in any case style.

Returns ​

The words in upper case, separated by single spaces; '' when there is no word.

Example ​

ts
upperCase('engineRoomTemperature'); // 'ENGINE ROOM TEMPERATURE'
upperCase('max-speed'); // 'MAX SPEED'

Tests ​

Line coverage: 100 %.

  • ✅ converts "engineRoomTemperature" to "ENGINE ROOM TEMPERATURE"
  • ✅ converts "max-speed" to "MAX SPEED"
  • ✅ converts "élan vital" to "ÉLAN VITAL"
  • ✅ converts "" to ""

Source ​

src/string/upper-case.ts (on GitHub)

Uses ​

  • splitWords: Splits a string into words, whatever its case style (camelCase, kebab-case, snake_case, spaces…), in one pass.

Files it needs ​

src/string/upper-case.ts imports, directly or not:

Source code
ts
import { splitWords } from './split-words';

/**
 * Converts a string to upper-case words separated by spaces, splitting identifiers such as `camelCase`.
 *
 * @param input - The identifier or sentence to convert, in any case style.
 * @returns The words in upper case, separated by single spaces; `''` when there is no word.
 * @example
 * upperCase('engineRoomTemperature'); // 'ENGINE ROOM TEMPERATURE'
 * upperCase('max-speed'); // 'MAX SPEED'
 */
export function upperCase(input: string): string {
  return splitWords(input)
    .map((word) => word.toUpperCase())
    .join(' ');
}
Test file
ts
import { upperCase } from './upper-case';

describe(upperCase, () => {
  it.for([
    ['engineRoomTemperature', 'ENGINE ROOM TEMPERATURE'],
    ['max-speed', 'MAX SPEED'],
    ['élan vital', 'ÉLAN VITAL'],
    ['', ''],
  ] as const)('converts %j to %j', ([input, expected]) => {
    expect(upperCase(input)).toBe(expected);
  });
});

Keywords: upper, case, strings, function

Generated from the JSDoc of the sources.