or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Case

Case is an extensible string utility library for converting, identifying, and flipping string case formats. It provides comprehensive case conversion capabilities for various programming and text formatting needs, with support for custom case types and Unicode characters.

Package Information

  • Package Name: case
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install case

Core Imports

const Case = require("case");

ES6 modules:

import Case from "case";

For individual functions:

const { snake, camel, pascal, kebab } = require("case");

Basic Usage

const Case = require("case");

// Convert between different case formats
Case.snake("Hello World!")    // "hello_world"
Case.camel("hello-world")     // "helloWorld"  
Case.pascal("hello_world")    // "HelloWorld"
Case.kebab("HelloWorld")      // "hello-world"
Case.constant("Hello World")  // "HELLO_WORLD"

// Text formatting
Case.title("hello world")     // "Hello World"
Case.sentence("HELLO WORLD")  // "Hello world"

// Utility functions
Case.of("hello_world")        // "snake" 
Case.flip("Hello World")      // "hELLO wORLD"
Case.random("Hello")          // "HeLlO" (randomized)

Architecture

Case is built with several key components:

  • Core Conversion Engine: Foundation functions (upper, lower, capital) that handle string preparation and character-level transformations
  • Specialized Converters: Built-in case types (snake, camel, pascal, kebab, etc.) that leverage the core engine
  • Extensibility System: Case.type() allows defining custom case conversion types
  • Unicode Support: Comprehensive handling of international characters and diacritics
  • Utility Functions: Case identification, case flipping, and randomization
  • String Prototype Extensions: Optional extensions that add methods to String.prototype

Capabilities

Foundation Functions

Core string transformation functions with customizable fill characters and apostrophe handling.

/**
 * Converts string to uppercase with optional fill character
 * @param str - Input string to convert
 * @param fill - Optional fill character to replace non-alphanumeric characters
 * @param noApostrophes - Whether to remove apostrophes (default: false)
 * @returns Uppercase string
 */
function upper(str: string, fill?: string, noApostrophes?: boolean): string;

/**
 * Converts string to lowercase with optional fill character
 * @param str - Input string to convert
 * @param fill - Optional fill character to replace non-alphanumeric characters  
 * @param noApostrophes - Whether to remove apostrophes (default: false)
 * @returns Lowercase string
 */
function lower(str: string, fill?: string, noApostrophes?: boolean): string;

/**
 * Converts string to capital case (first letter of each word capitalized)
 * @param str - Input string to convert
 * @param fill - Optional fill character to replace non-alphanumeric characters
 * @param noApostrophes - Whether to remove apostrophes (default: false)
 * @returns Capital case string
 */
function capital(str: string, fill?: string, noApostrophes?: boolean): string;

Usage Examples:

Case.upper("hello world")           // "HELLO WORLD"
Case.upper("hello-world", ".")      // "HELLO.WORLD"
Case.lower("don't stop", "_", true) // "dont_stop"
Case.capital("hello world")         // "Hello World"

Code Case Conversion

Specialized functions for converting strings to common programming case formats.

/**
 * Converts string to snake_case (lowercase with underscores)
 * @param str - Input string to convert
 * @returns snake_case string
 */
function snake(str: string): string;

/**
 * Converts string to PascalCase (first letter of each word capitalized, no spaces)
 * @param str - Input string to convert
 * @returns PascalCase string
 */
function pascal(str: string): string;

/**
 * Converts string to camelCase (first word lowercase, subsequent words capitalized)
 * @param str - Input string to convert
 * @returns camelCase string
 */
function camel(str: string): string;

/**
 * Converts string to kebab-case (lowercase with hyphens)
 * @param str - Input string to convert
 * @returns kebab-case string
 */
function kebab(str: string): string;

/**
 * Converts string to Header-Case (title case with hyphens)
 * @param str - Input string to convert
 * @returns Header-Case string
 */
function header(str: string): string;

/**
 * Converts string to CONSTANT_CASE (uppercase with underscores)
 * @param str - Input string to convert
 * @returns CONSTANT_CASE string
 */
function constant(str: string): string;

Usage Examples:

Case.snake("Hello World!")    // "hello_world"
Case.pascal("hello-world")    // "HelloWorld"
Case.camel("Hello World")     // "helloWorld"
Case.kebab("HelloWorld")      // "hello-world"
Case.header("hello world")    // "Hello-World"
Case.constant("Hello World")  // "HELLO_WORLD"

Text Formatting

Functions designed for human-readable text formatting with proper grammar rules.

/**
 * Converts string to Title Case with proper handling of articles and prepositions
 * @param str - Input string to convert
 * @returns Title Case string with proper grammar
 */
function title(str: string): string;

/**
 * Converts string to sentence case with proper name and abbreviation handling
 * @param str - Input string to convert
 * @param names - Optional array of proper names to capitalize
 * @param abbreviations - Optional array of abbreviations that don't end sentences
 * @returns Sentence case string
 */
function sentence(str: string, names?: string[], abbreviations?: string[]): string;

Usage Examples:

Case.title("the lord of the rings")           // "The Lord of the Rings"
Case.sentence("HELLO WORLD!")                 // "Hello world!"
Case.sentence("meet john and jane", ["John", "Jane"])  // "Meet John and Jane"
Case.sentence("the 12 oz. can is cold", null, ["oz"])  // "The 12 oz. can is cold"

Utility Functions

Functions for analyzing, manipulating, and working with string case patterns.

/**
 * Identifies the case type of a string
 * @param str - Input string to analyze
 * @param names - Optional array of proper names for sentence case detection
 * @returns String identifier of case type or undefined if unrecognized
 */
function of(str: string, names?: string[]): string | undefined;

/**
 * Reverses the case of letters (uppercase becomes lowercase and vice versa)
 * @param str - Input string to flip
 * @returns String with reversed case
 */
function flip(str: string): string;

/**
 * Randomizes the case of letters in the string
 * @param str - Input string to randomize
 * @returns String with randomized case
 */
function random(str: string): string;

Usage Examples:

Case.of("hello_world")              // "snake"
Case.of("HelloWorld")               // "pascal"
Case.of("Hello, World!")            // undefined (unrecognized)
Case.of("Hello, Bob!", ["Bob"])     // "sentence"

Case.flip("Hello World")            // "hELLO wORLD"
Case.random("Hello")                // "HeLlO" (varies)

Extensibility

System for adding custom case conversion types to the Case library.

/**
 * Extends Case by adding a new case conversion type
 * @param type - Name of the new case type
 * @param fn - Converter function that takes a string and returns transformed string
 */
function type(type: string, fn: (str: string) => string): void;

Usage Examples:

// Define a custom case type
Case.type("bang", function(s) {
    return Case.upper(s, "!") + "!";
});

// Use the custom case type
Case.bang("hello world")            // "HELLO!WORLD!"
Case.of("TEST!THIS!")               // "bang"

Legacy Functions

/**
 * Alias for pascal case (deprecated, use pascal instead)
 * @param str - Input string to convert
 * @returns PascalCase string
 * @deprecated Use pascal() instead
 */
function squish(str: string): string;

Types

// Case types that can be identified by Case.of()
type CaseType = 
  | "lower" 
  | "upper" 
  | "capital" 
  | "camel" 
  | "pascal" 
  | "snake" 
  | "kebab" 
  | "header" 
  | "constant" 
  | "title" 
  | "sentence"
  | "squish"  // deprecated
  | string;   // custom types

// Function signature for custom case converters
type CaseConverter = (str: string) => string;

String Prototype Extensions

When using the toCase.js extension file, Case automatically adds methods to String.prototype:

// Dynamic methods added to String.prototype
interface String {
  toCamelCase(): string;
  toSnakeCase(): string;
  toPascalCase(): string;
  toKebabCase(): string;
  toUpperCase(fill?: string, noApostrophes?: boolean): string;
  toLowerCase(fill?: string, noApostrophes?: boolean): string;
  toCapitalCase(fill?: string, noApostrophes?: boolean): string;
  toTitleCase(): string;
  toSentenceCase(names?: string[], abbreviations?: string[]): string;
  toHeaderCase(): string;
  toConstantCase(): string;
  // Plus any custom types defined via Case.type()
}

Usage Examples:

"hello world".toCamelCase()                    // "helloWorld"
"Hello World".toSnakeCase()                    // "hello_world"
"test string".toUpperCase("-", true)           // "TEST-STRING"

Error Handling

Case functions handle various input types gracefully:

  • null and undefined inputs are converted to empty strings
  • Boolean values are converted to their string representations
  • Numbers are converted to strings
  • Objects use their toString() method (or custom toString if defined)

Unicode Support

Case provides comprehensive Unicode support including:

  • Latin diacritics (résumé → RÉSUMÉ)
  • Extended Latin characters (café → CAFÉ)
  • International text with proper case conversion
  • Configurable regex patterns for different character sets

All case conversion functions properly handle international characters while maintaining their diacritical marks.