Convert into a lower case text with underscores between words
npm install text-snake-case[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Bundle size][bundlephobia-image]][bundlephobia-url]


> Transform text into snake_case format where words are lowercase and separated by underscores.
- 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
``bashnpm
npm install text-snake-case
๐ฏ Quick Start
`javascript
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
$3
`javascript
import { snakeCase } from "text-snake-case";console.log(snakeCase("hello world")); // "hello_world"
`$3
`javascript
const { snakeCase } = require("text-snake-case");console.log(snakeCase("hello world")); // "hello_world"
`$3
`typescript
import { snakeCase, snakeCaseTransformMerge, Options } from "text-snake-case";const result: string = snakeCase("hello world");
console.log(result); // "hello_world"
`๐ Transformation Examples
$3
`javascript
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"
`$3
`javascript
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
$3
`javascript
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"
`$3
`javascript
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"
`$3
`javascript
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"
`$3
`javascript
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
$3
Converts a string to snake_case.
#### Parameters
-
input (string): The string to convert
- options (Options, optional): Configuration options#### Returns
-
string: The snake_case formatted string#### Options
`typescript
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[];
}
`$3
A transform function that merges numeric characters without separation.
`javascript
import { snakeCase, snakeCaseTransformMerge } from "text-snake-case";snakeCase("version 1.2.3", { transform: snakeCaseTransformMerge }); // "version_123"
`๐ง Advanced Configuration
$3
`javascript
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"
`$3
`javascript
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"
`$3
`javascript
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
`bash
Run tests
pnpm testRun tests in watch mode
pnpm test --watchRun tests with coverage
pnpm test --coverageType checking
pnpm typecheckLinting
pnpm lint
`๐ Related Packages
text-camel-case - Convert to camelCase
- text-capital-case - Convert to Capital Case
- text-constant-case - Convert to CONSTANT_CASE
- text-dot-case - Convert to dot.case
- text-header-case - Convert to Header-Case
- text-case - All case transformations in one package๐ License
๐ค 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`)- ๐ง Email: selikhov.dmitrey@gmail.com
- ๐ Issues: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
- ๐ Documentation: Full Documentation
---
Made with โค๏ธ by Dmitry Selikhov
[npm-image]: https://img.shields.io/npm/v/text-snake-case.svg?style=flat
[npm-url]: https://npmjs.org/package/text-snake-case
[downloads-image]: https://img.shields.io/npm/dm/text-snake-case.svg?style=flat
[downloads-url]: https://npmjs.org/package/text-snake-case
[bundlephobia-image]: https://img.shields.io/bundlephobia/minzip/text-snake-case.svg
[bundlephobia-url]: https://bundlephobia.com/result?p=text-snake-case