Convert a text by swapping every character from upper to lower case, or lower to upper case
npm install text-swap-case[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Bundle size][bundlephobia-image]][bundlephobia-url]


> Transform text by swapping the case of each character (uppercase becomes lowercase and vice versa).
- Lightweight - Only ~200B minified + gzipped
- Type-safe - Full TypeScript support with comprehensive type definitions
- Zero dependencies - No external dependencies
- Tree-shakeable - ES modules support
- Universal - Works in browsers, Node.js, and serverless environments
- Well-tested - Comprehensive test suite with edge cases
``bashnpm
npm install text-swap-case
π― Quick Start
`javascript
import { swapCase } from "text-swap-case";console.log(swapCase("Hello World")); // "hELLO wORLD"
console.log(swapCase("JavaScript")); // "jAVAsCRIPT"
console.log(swapCase("CamelCase")); // "cAMELcASE"
`π Usage
$3
`javascript
import { swapCase } from "text-swap-case";console.log(swapCase("Hello")); // "hELLO"
`$3
`javascript
const { swapCase } = require("text-swap-case");console.log(swapCase("Hello")); // "hELLO"
`$3
`typescript
import { swapCase } from "text-swap-case";const result: string = swapCase("Hello World");
console.log(result); // "hELLO wORLD"
`π Transformation Examples
$3
`javascript
import { swapCase } from "text-swap-case";// Simple cases
swapCase("Hello"); // "hELLO"
swapCase("HELLO"); // "hello"
swapCase("hello"); // "HELLO"
// Mixed cases
swapCase("Hello World"); // "hELLO wORLD"
swapCase("CamelCase"); // "cAMELcASE"
swapCase("PascalCase"); // "pASCALcASE"
swapCase("snake_case"); // "SNAKE_CASE"
swapCase("kebab-case"); // "KEBAB-CASE"
// Complex examples
swapCase("JavaScript"); // "jAVAsCRIPT"
swapCase("XMLHttpRequest"); // "xmlhTTPrEQUEST"
swapCase("iPhone"); // "IpHONE"
swapCase("macOS"); // "MACos"
`$3
`javascript
import { swapCase } from "text-swap-case";// Numbers and symbols (unchanged)
swapCase("Hello123"); // "hELLO123"
swapCase("Test@Email.Com"); // "tEST@eMAIL.cOM"
swapCase("User_123"); // "uSER_123"
// Empty and whitespace
swapCase(""); // ""
swapCase(" "); // " "
swapCase("\n\t"); // "\n\t"
// Unicode characters
swapCase("CafΓ©"); // "cAFΓ"
swapCase("NaΓ―ve"); // "nAΓVE"
swapCase("RΓ©sumΓ©"); // "rΓSUMΓ"
`π Real-World Examples
$3
`javascript
import { swapCase } from "text-swap-case";// Simple text obfuscation
function obfuscateText(text) {
return swapCase(text);
}
console.log(obfuscateText("Secret Message")); // "sECRET mESSAGE"
console.log(obfuscateText("Password123")); // "pASSWORD123"
`$3
`javascript
import { swapCase } from "text-swap-case";// Create alternating case effect
function alternatingCase(text) {
return text
.split("")
.map((char, index) =>
index % 2 === 0 ? char.toLowerCase() : char.toUpperCase(),
)
.join("");
}
// Compare with swap case
const original = "Hello World";
console.log("Original:", original); // "Hello World"
console.log("Swap Case:", swapCase(original)); // "hELLO wORLD"
console.log("Alternating:", alternatingCase(original)); // "hElLo WoRlD"
`$3
`javascript
import { swapCase } from "text-swap-case";// Test case sensitivity
function testCaseSensitivity(input) {
const swapped = swapCase(input);
return {
original: input,
swapped: swapped,
areEqual: input === swapped,
length: input.length,
};
}
console.log(testCaseSensitivity("Hello"));
// {
// original: "Hello",
// swapped: "hELLO",
// areEqual: false,
// length: 5
// }
`$3
`javascript
import { swapCase } from "text-swap-case";// Create stylized text
function stylizeText(text, style = "swap") {
switch (style) {
case "swap":
return swapCase(text);
case "upper":
return text.toUpperCase();
case "lower":
return text.toLowerCase();
case "title":
return text.replace(
/\w\S*/g,
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(),
);
default:
return text;
}
}
const text = "JavaScript Programming";
console.log("Swap:", stylizeText(text, "swap")); // "jAVAsCRIPT pROGRAMMING"
console.log("Upper:", stylizeText(text, "upper")); // "JAVASCRIPT PROGRAMMING"
console.log("Lower:", stylizeText(text, "lower")); // "javascript programming"
console.log("Title:", stylizeText(text, "title")); // "Javascript Programming"
`$3
`javascript
import { swapCase } from "text-swap-case";// Transform passwords (for demonstration only)
function transformPassword(password) {
// Note: This is for demonstration only, not for real security
return swapCase(password);
}
console.log(transformPassword("MyPassword123")); // "mYpASSWORD123"
`$3
`javascript
import { swapCase } from "text-swap-case";class TextProcessor {
constructor() {
this.processors = [];
}
addSwapCase() {
this.processors.push(swapCase);
return this;
}
addReverse() {
this.processors.push((text) => text.split("").reverse().join(""));
return this;
}
process(text) {
return this.processors.reduce(
(result, processor) => processor(result),
text,
);
}
}
const processor = new TextProcessor().addSwapCase().addReverse();
console.log(processor.process("Hello World")); // "DLROw OLLEh"
`π API Reference
$3
Swaps the case of each character in a string.
#### Parameters
-
input (string): The string to transform#### Returns
-
string: The string with swapped caseπ Bundle Size
This package is optimized for minimal bundle size:
- Minified: ~200B
- Gzipped: ~150B
- Tree-shakeable: Yes
- Side effects: None
π Browser Support
- Modern browsers: ES2015+ (Chrome 51+, Firefox 54+, Safari 10+)
- Node.js: 12+
- TypeScript: 4.0+
- Bundle formats: UMD, ESM, CommonJS
π§ͺ Testing
`bash
Run tests
pnpm testRun tests in watch mode
pnpm test --watchRun tests with coverage
pnpm test --coverageType checking
pnpm typecheckLinting
pnpm lint
`π Related Packages
text-camel-case - Convert to camelCase
- text-capital-case - Convert to Capital Case
- text-constant-case - Convert to CONSTANT_CASE
- text-dot-case - Convert to dot.case
- text-header-case - Convert to Header-Case
- text-case - All case transformations in one packageπ License
π€ Contributing
1. Fork the repository
2. Create your feature branch (
git checkout -b feature/amazing-feature)
3. Commit your changes (git commit -m 'Add some amazing feature')
4. Push to the branch (git push origin feature/amazing-feature`)- π§ Email: selikhov.dmitrey@gmail.com
- π Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
- π Documentation: Full Documentation
---
Made with β€οΈ by Dmitry Selikhov
[npm-image]: https://img.shields.io/npm/v/text-swap-case.svg?style=flat
[npm-url]: https://npmjs.org/package/text-swap-case
[downloads-image]: https://img.shields.io/npm/dm/text-swap-case.svg?style=flat
[downloads-url]: https://npmjs.org/package/text-swap-case
[bundlephobia-image]: https://img.shields.io/bundlephobia/minzip/text-swap-case.svg
[bundlephobia-url]: https://bundlephobia.com/result?p=text-swap-case