UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

19 lines (18 loc) 644 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.titleCase = void 0; /** * Converts a string to title case with smart exceptions. * @author @dailker * @param {string} str * @returns {string} */ function titleCase(str) { const exceptions = ['a', 'an', 'the', 'and', 'but', 'or', 'for', 'nor', 'on', 'at', 'to', 'by', 'with', 'of', 'in']; return str.toLowerCase().replace(/\w\S*/g, (word, i, all) => { if (i !== 0 && exceptions.includes(word)) return word; return word.charAt(0).toUpperCase() + word.slice(1); }); } exports.titleCase = titleCase;