text-swap-case
v1.2.9
Published
Convert a text by swapping every character from upper to lower case, or lower to upper case
Maintainers
Readme
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("[email protected]"); // "[email protected]"
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-camel-case- Convert to camelCasetext-capital-case- Convert to Capital Casetext-constant-case- Convert to CONSTANT_CASEtext-dot-case- Convert to dot.casetext-header-case- Convert to Header-Casetext-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: [email protected]
- 🐛 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
- 📖 Documentation: Full Documentation
Made with ❤️ by Dmitry Selikhov
