UNPKG

@upsided/utilities

Version:
43 lines (38 loc) 1.33 kB
const SMALL_WORDS = /\b(?:an?d?|a[st]|because|but|by|en|for|i[fn]|neither|nor|o[fnr]|only|over|per|so|some|tha[tn]|the|to|up|upon|vs?\.?|versus|via|when|with|without|yet)\b/i; const TOKENS = /[^\s:–—-]+|./g; const WHITESPACE = /\s/; const IS_MANUAL_CASE = /.(?=[A-Z]|\..)/; const ALPHANUMERIC_PATTERN = /[A-Za-z0-9\u00C0-\u00FF]/; /** * A capitalization function, due to JavaScript's lack of one. * @param {string} input * @returns {string} The capitalized string, if capitalized. */ export default (input) => { let result = ""; /** * @type {RegExpExecArray | null} */ let m; // tslint:disable-next-line while ((m = TOKENS.exec(input)) !== null) { const { 0: token, index } = m; if ( // Ignore already capitalized words. !IS_MANUAL_CASE.test(token) && // Ignore small words except at beginning or end. (!SMALL_WORDS.test(token) || index === 0 || index + token.length === input.length) && // Ignore URLs. (input.charAt(index + token.length) !== ":" || WHITESPACE.test(input.charAt(index + token.length + 1))) ) { // Find and uppercase first word character, skips over *modifiers*. result += token.replace(ALPHANUMERIC_PATTERN, (m) => m.toUpperCase()); continue; } result += token; } return result; }