Skip to content

dotCase function ​

Converts a string to dot.case: lower-case words joined with ., as in translation keys and property paths.

ts
import { dotCase } from 'utils';

Signature ​

ts
export function dotCase(input: string): string

Parameters ​

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

Returns ​

The dot.case string; '' when there is no word.

Example ​

ts
dotCase('engineRoomTemperature'); // 'engine.room.temperature'

Tests ​

Line coverage: 100 %.

  • ✅ converts "engineRoomTemperature" to "engine.room.temperature"
  • ✅ converts "Alarm Panel" to "alarm.panel"
  • ✅ converts "" to ""

Source ​

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

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

/**
 * Converts a string to dot.case: lower-case words joined with `.`, as in translation keys and property paths.
 *
 * @param input - The identifier or sentence to convert, in any case style.
 * @returns The dot.case string; `''` when there is no word.
 * @example
 * dotCase('engineRoomTemperature'); // 'engine.room.temperature'
 */
export function dotCase(input: string): string {
  return splitWords(input)
    .map((word) => word.toLowerCase())
    .join('.');
}
Test file
ts
import { dotCase } from './dot-case';

describe(dotCase, () => {
  it.for([
    ['engineRoomTemperature', 'engine.room.temperature'],
    ['Alarm Panel', 'alarm.panel'],
    ['', ''],
  ] as const)('converts %j to %j', ([input, expected]) => {
    expect(dotCase(input)).toBe(expected);
  });
});

Keywords: dot, case, strings, function

Generated from the JSDoc of the sources.