string-conv
v1.4.0
Published
A lightweight, zero-dependency TypeScript utility for string case conversion and inspection — supports camelCase, PascalCase, snake_case, kebab-case, CONSTANT_CASE, dot.case, Train-Case, path/case, flatcase, slug, sentence case, inverse case, and title ca
Maintainers
Readme
string-conv
A lightweight, zero-dependency TypeScript utility for string case conversion, inspection, and manipulation. Convert strings between 18+ formats, detect case styles with validation guards, and use robust utilities for everyday string tasks. Works in both JavaScript and TypeScript projects.
Features
- ✅ Zero dependencies — Extremely lightweight.
- ✅ Full TypeScript Support — Built-in type declarations included.
- ✅ Hybrid Package — Works seamlessly with ESM (
import) and CommonJS (require). - ✅ Robust Case Handling — Handles edge cases like multiple spaces, special characters, and mixed-case inputs.
- ✅ Case Detection — Identify case styles programmatically.
- ✅ Advanced Utilities — Built-in support for HTML stripping, palindromes, truncation, and more.
Installation
npm install string-convUsage
TypeScript / ESM
import {
// Case transforms
toUpperCase,
toLowerCase,
toTitleCase,
toSentenceCase,
toCamelCase,
toPascalCase,
toSnakeCase,
toKebabCase,
toConstantCase,
toDotCase,
toSlug,
fromSlug,
toInverseCase,
toReverseCase,
toTrainCase,
toPathCase,
toFlatCase,
toHeaderCase,
toSpongeCase,
// Inspection
detectCase,
isCamelCase,
isPascalCase,
isSnakeCase,
isKebabCase,
isConstantCase,
isDotCase,
isTrainCase,
isPathCase,
// Utilities
truncate,
wordCount,
charCount,
countOccurrences,
reverseWords,
trimWords,
padStart,
padEnd,
stripHtml,
isPalindrome,
escapeHtml,
} from "string-conv";JavaScript (CommonJS)
const {
toUpperCase,
toLowerCase,
toTitleCase,
toSentenceCase,
toCamelCase,
toPascalCase,
toSnakeCase,
toKebabCase,
toConstantCase,
toDotCase,
toSlug,
fromSlug,
toInverseCase,
toReverseCase,
toTrainCase,
toPathCase,
toFlatCase,
toHeaderCase,
toSpongeCase,
detectCase,
isCamelCase,
isPascalCase,
isSnakeCase,
isKebabCase,
isConstantCase,
isDotCase,
isTrainCase,
isPathCase,
truncate,
wordCount,
charCount,
countOccurrences,
reverseWords,
trimWords,
padStart,
padEnd,
stripHtml,
isPalindrome,
escapeHtml,
} = require("string-conv");API — Case Transforms
toUpperCase(str: string): string
Converts a string to UPPERCASE.
toUpperCase("hello world"); // => "HELLO WORLD"toLowerCase(str: string): string
Converts a string to lowercase.
toLowerCase("HELLO WORLD"); // => "hello world"toTitleCase(str: string): string
Converts to Title Case (first letter of each word capitalized).
toTitleCase("hello world"); // => "Hello World"toSentenceCase(str: string): string
Capitalizes only the first character.
toSentenceCase("hello world"); // => "Hello world"toCamelCase(str: string): string
toCamelCase("hello world"); // => "helloWorld"toPascalCase(str: string): string
toPascalCase("hello world"); // => "HelloWorld"toSnakeCase(str: string): string
toSnakeCase("hello world"); // => "hello_world"toKebabCase(str: string): string
toKebabCase("hello world"); // => "hello-world"toConstantCase(str: string): string
toConstantCase("hello world"); // => "HELLO_WORLD"toDotCase(str: string): string
toDotCase("hello world"); // => "hello.world"toInverseCase(str: string): string
Inverts character casing.
toInverseCase("Hello World"); // => "hELLO wORLD"toReverseCase(str: string): string
Reverses characters.
toReverseCase("hello"); // => "olleh"toTrainCase(str: string): string
toTrainCase("hello world"); // => "Hello-World"toPathCase(str: string): string
toPathCase("hello world"); // => "hello/world"toFlatCase(str: string): string
toFlatCase("Hello World"); // => "helloworld"toHeaderCase(str: string): string
toHeaderCase("content type"); // => "Content-Type"toSpongeCase(str: string): string
toSpongeCase("hello"); // => "hElLo" (random)API — Inspection
detectCase(str: string): CaseType
Detects the case style of a string.
detectCase("helloWorld"); // => "camelCase"Possible values: "camelCase", "PascalCase", "snake_case", "kebab-case", "CONSTANT_CASE", "dot.case", "path/case", "Train-Case", "Title Case", "UPPERCASE", "lowercase", "unknown".
Validation guards
Boolean checks for specific styles: isCamelCase, isPascalCase, isSnakeCase, isKebabCase, isConstantCase, isDotCase, isTrainCase, isPathCase.
API — Utilities
stripHtml(str: string): string
Removes HTML tags.
stripHtml("<p>Hello <b>World</b></p>"); // => "Hello World"isPalindrome(str: string, ignoreSpaces?: boolean): boolean
isPalindrome("racecar"); // => trueescapeHtml(str: string): string
escapeHtml("<b>Hi</b>"); // => "<b>Hi</b>"truncate(str: string, maxLength: number, suffix?: string): string
truncate("Hello, World!", 8); // => "Hello..."wordCount(str: string): number
wordCount("hello world"); // => 2reverseWords(str: string): string
reverseWords("hello world"); // => "world hello"trimWords(str: string): string
Collapses internal whitespace.
trimWords(" hello world "); // => "hello world"Quick Reference Table
| Function | Input | Output |
| :--------------- | :-------------- | :-------------- |
| toUpperCase | "hello world" | "HELLO WORLD" |
| toLowerCase | "HELLO WORLD" | "hello world" |
| toTitleCase | "hello world" | "Hello World" |
| toSentenceCase | "hello world" | "Hello world" |
| toCamelCase | "hello world" | "helloWorld" |
| toPascalCase | "hello world" | "HelloWorld" |
| toSnakeCase | "hello world" | "hello_world" |
| toKebabCase | "hello world" | "hello-world" |
| toConstantCase | "hello world" | "HELLO_WORLD" |
| toDotCase | "hello world" | "hello.world" |
| toInverseCase | "Hello World" | "hELLO wORLD" |
| toTrainCase | "hello world" | "Hello-World" |
| toPathCase | "hello world" | "hello/world" |
| toFlatCase | "Hello World" | "helloworld" |
License
MIT © Chetan Nada
