Formatting

import { humanize, titleCase, slugify, ordinalize, pluralize } from '@ppabari/strio/str';
๐ŸŒ

English-locale: titleCase, pluralize, and ordinalize implement English rules only.

humanize

humanize(str: string): string

Convert a machine identifier into a readable sentence.

humanize('first_name'); // โ†’ 'First name'
humanize('userID');     // โ†’ 'User id'
humanize('created-at'); // โ†’ 'Created at'

titleCase

titleCase(str: string): string

Title Case with English conventions โ€” minor words (articles, short prepositions, conjunctions) stay lowercase unless first or last.

titleCase('the lord of the rings'); // โ†’ 'The Lord of the Rings'
titleCase('a tale of two cities');  // โ†’ 'A Tale of Two Cities'

slugify

slugify(str: string, separator?: string): string

Produce a URL-friendly slug: transliterated to ASCII, lowercased, with runs of non-alphanumerics collapsed to separator (default '-').

slugify('Hรฉllo, World!');  // โ†’ 'hello-world'
slugify('Foo   Bar', '_'); // โ†’ 'foo_bar'

ordinalize

ordinalize(n: number | string): string

Return the ordinal form of a number.

ordinalize(1);   // โ†’ '1st'
ordinalize(2);   // โ†’ '2nd'
ordinalize(11);  // โ†’ '11th'
ordinalize(103); // โ†’ '103rd'

pluralize

pluralize(word: string, count?: number): string

Pluralize an English word. When count is provided, the word is only pluralized if count !== 1. First-letter case is preserved; handles irregulars and uncountables.

pluralize('cat');    // โ†’ 'cats'
pluralize('bus');    // โ†’ 'buses'
pluralize('city');   // โ†’ 'cities'
pluralize('child');  // โ†’ 'children'
pluralize('sheep');  // โ†’ 'sheep'
pluralize('cat', 1); // โ†’ 'cat'
pluralize('cat', 3); // โ†’ 'cats'