npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

text-swap-case

v1.2.9

Published

Convert a text by swapping every character from upper to lower case, or lower to upper case

Readme

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

  • 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

📜 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