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.

25 lines (24 loc) 714 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.alternateCase = void 0; /** * Alternates between lowercase and uppercase for each letter in the string. * * Example: alternateCase("hello world") → "HeLlO WoRlD" * * @author @dailker * @param {string} str - The input string. * @returns {string} The string with alternating case. */ function alternateCase(str) { let up = true; return str.split('').map(c => { if (/[a-z]/i.test(c)) { const res = up ? c.toUpperCase() : c.toLowerCase(); up = !up; return res; } return c; }).join(''); } exports.alternateCase = alternateCase;