Swap Case
Transform text by swapping the case of each character (uppercase becomes lowercase and vice versa).
π Features
- 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
π¦ Installation
# npm
npm install text-swap-case
# yarn
yarn add text-swap-case
# pnpm
pnpm add text-swap-case
# bun
bun add text-swap-case
π― Quick Start
import { swapCase } from "text-swap-case";
console.log(swapCase("Hello World")); // "hELLO wORLD"
console.log(swapCase("JavaScript")); // "jAVAsCRIPT"
console.log(swapCase("CamelCase")); // "cAMELcASE"
π Usage
ES Modules (Recommended)
import { swapCase } from "text-swap-case";
console.log(swapCase("Hello")); // "hELLO"
CommonJS
const { swapCase } = require("text-swap-case");
console.log(swapCase("Hello")); // "hELLO"
TypeScript
import { swapCase } from "text-swap-case";
const result: string = swapCase("Hello World");
console.log(result); // "hELLO wORLD"
π Transformation Examples
Basic Transformations
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"
Edge Cases
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
Text Obfuscation
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"
Alternating Case Effect
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"
Case Inversion for Testing
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
// }
Creative Text Effects
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"
Password Transformation
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"
Text Processing Pipeline
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
swapCase(input)
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
# Run tests
pnpm test
# Run tests in watch mode
pnpm test --watch
# Run tests with coverage
pnpm test --coverage
# Type checking
pnpm typecheck
# Linting
pnpm lint
π Related Packages
text-upper-case
- Convert to uppercasetext-lower-case
- Convert to lowercasetext-upper-case-first
- Make first character uppercasetext-case
- All case transformations in one package
π License
π€ Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
π Support
- π§ Email: selikhov.dmitrey@gmail.com
- π Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
- π Documentation: Full Documentation
Made with β€οΈ by Dmitry Selikhov