UNPKG

web-ui-pack

Version:
61 lines (60 loc) 1.88 kB
export function isSpecialSymbol(s, i) { const c = s.charCodeAt(i); return c < 65 || (c > 90 && (c < 97 || (c > 123 && c < 128))); } export function stringLowerCount(s, stopWith) { let c = 0; stopWith = stopWith ?? s.length; for (let i = 0; i < s.length && c < stopWith; ++i) { if (s[i].toLowerCase() === s[i] && !isSpecialSymbol(s, i)) { ++c; } } return c; } export function stringUpperCount(s, stopWith) { let c = 0; stopWith = stopWith ?? s.length; for (let i = 0; i < s.length && c < stopWith; ++i) { if (s[i].toUpperCase() === s[i] && !isSpecialSymbol(s, i)) { ++c; } } return c; } export function stringPrettify(text, capitalize = true, handleKebabCase = false) { let r = ""; let nextUpper = false; let wasUpper = false; for (let i = 0; i < text.length; ++i) { const c = text.charCodeAt(i); if (c > 96 && c < 123) { wasUpper = false; r += String.fromCharCode(nextUpper || i === 0 ? c - 32 : c); } else if (c > 64 && c < 91) { const cNext = text.charCodeAt(i + 1); const isAbbr = i === text.length - 1 || (cNext > 64 && cNext < 91); const isAbbrPrev = wasUpper && isAbbr; if (!nextUpper && !isAbbrPrev && i !== 0) { r += " "; } r += String.fromCharCode(capitalize || isAbbr || i === 0 ? c : c + 32); wasUpper = true; } else if (c === 95 || (c === 45 && handleKebabCase)) { wasUpper = false; if (i !== 0) { r += " "; } nextUpper = capitalize; continue; } else { r += String.fromCharCode(c); wasUpper = false; } nextUpper = false; } return r; }