Package Exports
- text-snake-case
- text-snake-case/dist.es2015/index.js
- text-snake-case/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (text-snake-case) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Snake Case
Transform text into snake_case format where words are lowercase and separated by underscores.
๐ Features
- Lightweight - Only ~450B 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
- Customizable - Flexible options for advanced use cases
๐ฆ Installation
# npm
npm install text-snake-case
# yarn
yarn add text-snake-case
# pnpm
pnpm add text-snake-case
# bun
bun add text-snake-case๐ฏ Quick Start
import { snakeCase } from "text-snake-case";
console.log(snakeCase("hello world")); // "hello_world"
console.log(snakeCase("userProfileData")); // "user_profile_data"
console.log(snakeCase("backgroundColor")); // "background_color"๐ Usage
ES Modules (Recommended)
import { snakeCase } from "text-snake-case";
console.log(snakeCase("hello world")); // "hello_world"CommonJS
const { snakeCase } = require("text-snake-case");
console.log(snakeCase("hello world")); // "hello_world"TypeScript
import { snakeCase, snakeCaseTransformMerge, Options } from "text-snake-case";
const result: string = snakeCase("hello world");
console.log(result); // "hello_world"๐ Transformation Examples
Basic Transformations
import { snakeCase } from "text-snake-case";
// From different cases
snakeCase("hello world"); // "hello_world"
snakeCase("Hello World"); // "hello_world"
snakeCase("HELLO WORLD"); // "hello_world"
snakeCase("camelCase"); // "camel_case"
snakeCase("PascalCase"); // "pascal_case"
snakeCase("kebab-case"); // "kebab_case"
snakeCase("dot.case"); // "dot_case"
snakeCase("CONSTANT_CASE"); // "constant_case"
// Complex examples
snakeCase("XMLHttpRequest"); // "xml_http_request"
snakeCase("iPhone"); // "i_phone"
snakeCase("version 1.2.3"); // "version_1_2_3"
snakeCase("userProfileData"); // "user_profile_data"Advanced Options
import { snakeCase, snakeCaseTransformMerge } from "text-snake-case";
// Custom transform to merge numbers without separator
snakeCase("version 1.2.3", {
transform: snakeCaseTransformMerge,
}); // "version_123"
// Custom word splitting
snakeCase("XMLHttpRequest", {
splitRegexp: /([a-z])([A-Z])/g,
}); // "xml_http_request"
// Custom character stripping
snakeCase("hello@world.com", {
stripRegexp: /[@.]/g,
}); // "hello_world_com"
// Custom transformation function
snakeCase("API-v2-endpoint", {
transform: (word, index) => {
if (word === "API") return "api";
if (word === "v2") return "v2";
return word.toLowerCase();
},
}); // "api_v2_endpoint"๐ Real-World Examples
Database Column Names
import { snakeCase } from "text-snake-case";
// Table columns
snakeCase("firstName"); // "first_name"
snakeCase("emailAddress"); // "email_address"
snakeCase("createdAt"); // "created_at"
snakeCase("userId"); // "user_id"
snakeCase("accessToken"); // "access_token"API Field Names
import { snakeCase } from "text-snake-case";
// REST API fields
snakeCase("userProfile"); // "user_profile"
snakeCase("lastLoginDate"); // "last_login_date"
snakeCase("isActive"); // "is_active"
snakeCase("paymentMethod"); // "payment_method"
snakeCase("shippingAddress"); // "shipping_address"Environment Variables
import { snakeCase } from "text-snake-case";
// Environment variable names
snakeCase("databaseUrl"); // "database_url"
snakeCase("apiKey"); // "api_key"
snakeCase("maxRetries"); // "max_retries"
snakeCase("timeoutMs"); // "timeout_ms"
snakeCase("debugMode"); // "debug_mode"Object Key Transformation
import { snakeCase } from "text-snake-case";
// Transform object keys from camelCase to snake_case
const jsUser = {
firstName: "John",
lastName: "Doe",
emailAddress: "john@example.com",
createdAt: "2023-01-01",
};
const dbUser = Object.fromEntries(
Object.entries(jsUser).map(([key, value]) => [snakeCase(key), value]),
);
console.log(dbUser);
// {
// first_name: "John",
// last_name: "Doe",
// email_address: "john@example.com",
// created_at: "2023-01-01"
// }๐ API Reference
snakeCase(input, options?)
Converts a string to snake_case.
Parameters
input(string): The string to convertoptions(Options, optional): Configuration options
Returns
string: The snake_case formatted string
Options
interface Options {
// Custom transform function for word processing
transform?: (word: string, index: number, words: string[]) => string;
// Regex to strip characters before processing
stripRegexp?: RegExp;
// Custom split function
split?: (value: string) => string[];
}snakeCaseTransformMerge
A transform function that merges numeric characters without separation.
import { snakeCase, snakeCaseTransformMerge } from "text-snake-case";
snakeCase("version 1.2.3", { transform: snakeCaseTransformMerge }); // "version_123"๐ง Advanced Configuration
Custom Word Splitting
import { snakeCase } from "text-snake-case";
// Split on specific patterns
snakeCase("XMLHttpRequest", {
splitRegexp: /([a-z])([A-Z])/g,
}); // "xml_http_request"
// Split on numbers
snakeCase("user123data", {
splitRegexp: /([a-zA-Z])(\d)/g,
}); // "user_123_data"Custom Character Stripping
import { snakeCase } from "text-snake-case";
// Strip specific characters
snakeCase("hello@world.com", {
stripRegexp: /[@.]/g,
}); // "hello_world_com"
// Strip all non-alphanumeric
snakeCase("hello!@#world", {
stripRegexp: /[^a-zA-Z0-9]/g,
}); // "hello_world"Custom Transform Functions
import { snakeCase } from "text-snake-case";
// Preserve specific formatting
snakeCase("XML-HTTP-Request", {
transform: (word, index) => {
const acronyms = ["xml", "http", "api", "url"];
if (acronyms.includes(word.toLowerCase())) {
return word.toLowerCase();
}
return word.toLowerCase();
},
}); // "xml_http_request"
// Custom business logic
snakeCase("UserV2API", {
transform: (word, index) => {
if (word === "V2") return "v2";
if (word === "API") return "api";
return word.toLowerCase();
},
}); // "user_v2_api"๐ Bundle Size
This package is optimized for minimal bundle size:
- Minified: ~450B
- Gzipped: ~250B
- 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: selikhov.dmitrey@gmail.com
- ๐ Issues: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
- ๐ Documentation: Full Documentation
Made with โค๏ธ by Dmitry Selikhov