Text manipulation functions including capitalization, title casing, truncation, and line break conversion.
Functions for capitalizing the first letter of strings and words.
/**
* Capitalizes the first letter in a string, optionally downcasing the tail
* @param string - String to capitalize
* @param downCaseTail - Whether to lowercase the rest of the string (default: false)
* @returns Capitalized string
*/
function capitalize(string: string, downCaseTail?: boolean): string;
/**
* Capitalizes the first letter of each word in a string
* @param string - String to process
* @returns String with each word capitalized
*/
function capitalizeAll(string: string): string;Usage Examples:
const Humanize = require('humanize-plus');
// Basic capitalization
console.log(Humanize.capitalize('hello world')); // "Hello world"
console.log(Humanize.capitalize('wHoOaA!')); // "WHoOaA!"
console.log(Humanize.capitalize('wHoOaA!', true)); // "Whooaa!"
// Capitalize all words
console.log(Humanize.capitalizeAll('hello world')); // "Hello World"
console.log(Humanize.capitalizeAll('some boring string')); // "Some Boring String"Intelligently capitalizes words in a string following proper title case grammar rules.
/**
* Intelligently title-cases string with proper grammar rules
* Handles small words, hyphens, apostrophes, and internal capitalization
* @param string - String to title case
* @returns Title-cased string with normalized whitespace
*/
function titleCase(string: string): string;Usage Examples:
const Humanize = require('humanize-plus');
console.log(Humanize.titleCase('hello world')); // "Hello World"
console.log(Humanize.titleCase('some of a boring string')); // "Some of a Boring String"
console.log(Humanize.titleCase('cool the iTunes cake, O\'Malley!')); // "Cool the iTunes Cake, O'Malley!"
console.log(Humanize.titleCase('cul-de-sac drive-by')); // "Cul-de-Sac Drive-By"
// Handles internal capitalization
console.log(Humanize.titleCase('you get the cake an iTunes hat is West wacky?'));
// "You Get the Cake an iTunes Hat Is West Wacky?"Title Case Rules:
Functions for truncating strings by character count or word count.
/**
* Truncates a string if longer than specified number of characters
* @param str - String to truncate
* @param length - Maximum length including ending (default: 100)
* @param ending - Truncation indicator (default: '...')
* @returns Truncated string or original if within limit
*/
function truncate(str: string, length?: number, ending?: string): string;
/**
* Truncates a string after a certain number of words
* @param string - String to truncate
* @param length - Maximum number of words
* @returns Truncated string with '...' or null if not needed
*/
function truncateWords(string: string, length: number): string | null;Usage Examples:
const Humanize = require('humanize-plus');
// Character truncation
console.log(Humanize.truncate('This is a long string')); // "This is a long string" (under 100 chars)
console.log(Humanize.truncate('This is a long string', 10)); // "This is..."
console.log(Humanize.truncate('This is a long string', 10, ' [more]')); // "This [more]"
// Word truncation
console.log(Humanize.truncateWords('The quick brown fox jumps', 3)); // "The quick brown ..."
console.log(Humanize.truncateWords('Short string', 5)); // null (not needed)Functions for converting between different line break formats.
/**
* Converts newlines to HTML br tags
* @param string - String to convert
* @param replacement - Replacement for newlines (default: '<br/>')
* @returns String with converted newlines
*/
function nl2br(string: string, replacement?: string): string;
/**
* Converts HTML br tags to newlines
* Handles various br tag formats: <br>, <br/>, <br />
* @param string - String to convert
* @param replacement - Replacement for br tags (default: '\r\n')
* @returns String with converted br tags
*/
function br2nl(string: string, replacement?: string): string;Usage Examples:
const Humanize = require('humanize-plus');
// Newlines to br tags
console.log(Humanize.nl2br('Line 1\nLine 2')); // "Line 1<br/>Line 2"
console.log(Humanize.nl2br('Line 1\nLine 2', '<br>')); // "Line 1<br>Line 2"
// Br tags to newlines
console.log(Humanize.br2nl('Line 1<br/>Line 2')); // "Line 1\r\nLine 2"
console.log(Humanize.br2nl('Line 1<br>Line 2')); // "Line 1\r\nLine 2"
console.log(Humanize.br2nl('Line 1<br />Line 2')); // "Line 1\r\nLine 2" (handles malformed)
console.log(Humanize.br2nl('Line 1<br/>Line 2', '\n')); // "Line 1\nLine 2"/**
* @deprecated Use truncateWords instead. Will be removed in next major version.
*/
function truncatewords(...args: any[]): string;/**
* @deprecated Use titleCase instead. Will be removed in next major version.
*/
function titlecase(...args: any[]): string;