Skip to the content.

Swap Case

NPM version NPM downloads Bundle size License: MIT TypeScript

Transform text by swapping the case of each character (uppercase becomes lowercase and vice versa).

πŸš€ Features

πŸ“¦ 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

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

Returns

πŸ“Š Bundle Size

This package is optimized for minimal bundle size:

🌍 Browser Support

πŸ§ͺ 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

πŸ“œ License

MIT Β© Dmitry Selikhov

🀝 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)
  5. Open a Pull Request

πŸ†˜ Support


Made with ❀️ by Dmitry Selikhov