Skip to content

Latest commit

 

History

History
58 lines (46 loc) · 1.55 KB

serializeKeysToSnakeCase.md

File metadata and controls

58 lines (46 loc) · 1.55 KB

serializeKeysToSnakeCase (source code)

  • Curried: false
  • Failsafe status: failsafe by default

The serializeKeysToSnakeCase function recursively converts the camel-cased object keys to snake case. It gracefully handles special objects like Date and dayjs instances. Additionally, while converting, this function checks if the value being processed is an object and if it has a toJSON function. If the toJSON function is present, it calls the function and uses the return value for further processing.

Arguments:

  • object: An object with camelCase keys to be converted and serialized.

Usage:

Example 1:

serializeKeysToSnakeCase({
  name: { toJSON: () => ({ firstName: "Oliver", lastName: "Smith" }) },
  phoneNumber: "389791382",
});

/*
{
  name: { first_name: "Oliver", last_name: "Smith" },
  phone_number: "389791382",
}
*/

Example 2: (Real world example)

serializeKeysToSnakeCase({
  address: { city: "Miami", phoneNumber: "389791382" },
  firstName: "Oliver",
  lastName: "Smith",
  dob: new Date("1980-01-01"),
});

/*
{
  first_name: "Oliver",
  last_name: "Smith",
  address: { city: "Miami", phone_number: "389791382" },
  dob: "1980-01-01T00:00:00.000Z",
}
*/

In the above example, the value of dob is an date object and has toJSON method present in it. The toJSON method returns the date in ISO format which will be used for further processing instead of recursively converting the original date object.