Skip to content

lowerCase function ​

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

ts
import { lowerCase } from 'utils';

Signature ​

ts
export function lowerCase(input: string): string

Parameters ​

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

Returns ​

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

Example ​

ts
lowerCase('engineRoomTemperature'); // 'engine room temperature'
lowerCase('MAX_SPEED'); // 'max speed'

Tests ​

Line coverage: 100 %.

  • ✅ converts "engineRoomTemperature" to "engine room temperature"
  • ✅ converts "MAX_SPEED" to "max speed"
  • ✅ converts " Hello---World " to "hello world"
  • ✅ converts "" to ""

Source ​

src/string/lower-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/lower-case.ts imports, directly or not:

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

/**
 * Converts a string to lower-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 lower case, separated by single spaces; `''` when there is no word.
 * @example
 * lowerCase('engineRoomTemperature'); // 'engine room temperature'
 * lowerCase('MAX_SPEED'); // 'max speed'
 */
export function lowerCase(input: string): string {
  return splitWords(input)
    .map((word) => word.toLowerCase())
    .join(' ');
}
Test file
ts
import { lowerCase } from './lower-case';

describe(lowerCase, () => {
  it.for([
    ['engineRoomTemperature', 'engine room temperature'],
    ['MAX_SPEED', 'max speed'],
    ['  Hello---World ', 'hello world'],
    ['', ''],
  ] as const)('converts %j to %j', ([input, expected]) => {
    expect(lowerCase(input)).toBe(expected);
  });
});

Keywords: lower, case, strings, function

Generated from the JSDoc of the sources.