Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toSnakeObject): Add toSnakeObject export to object index #969

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gweesin
Copy link
Contributor

@gweesin gweesin commented Feb 28, 2025

Add toSnakeObject Utility Function

Summary

Added a new utility function toSnakeObject that converts the keys of an object to snake_case. This function is designed to handle objects with any depth of nesting and ensures that the original object remains unaltered.

Implementation Details

  • The function accepts an object with keys in any case format.
  • It returns a new object with all keys converted to snake_case.
  • The function is recursive, allowing it to handle nested objects and arrays.
  • Utilizes a helper function snakeCase to perform the key conversion.
  • Ensures immutability by not modifying the original object.

Usage Examples

import { toSnakeObject } from 'es-toolkit/object';

// Convert object keys to snake_case
const user = {
  firstName: 'John',
  lastName: 'Doe',
  contactInfo: {
    emailAddress: '[email protected]',
    phoneNumber: '123-456-7890'
  }
};

const formattedUser = toSnakeObject(user);
console.log(formattedUser);
// Output: {
//   first_name: 'John',
//   last_name: 'Doe',
//   contact_info: {
//     email_address: '[email protected]',
//     phone_number: '123-456-7890'
//   }
// }

This function is particularly useful for applications that require consistent key formatting, such as when interfacing with APIs that expect snake_case keys.

@gweesin gweesin requested a review from raon0211 as a code owner February 28, 2025 07:33
Copy link

vercel bot commented Feb 28, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
es-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 28, 2025 7:41am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant