Back to Es Toolkit

toConstantCaseKeys

docs/reference/object/toConstantCaseKeys.md

1.51.02.0 KB
Original Source

toConstantCaseKeys

Returns a new object with all keys in objects and arrays converted to CONSTANT_CASE.

Constant case is a naming convention where each word in an identifier is written in uppercase and connected with underscores (_). For example, it's written as CONSTANT_CASE.

typescript
const constantCased = toConstantCaseKeys(obj);

Usage

toConstantCaseKeys(obj)

Use toConstantCaseKeys when you want to convert all keys of an object to CONSTANT_CASE. Nested objects and objects within arrays are also converted recursively.

For example, object keys are converted as follows:

  • camelCaseCONSTANT_CASE (e.g. userIdUSER_ID)
  • PascalCaseCONSTANT_CASE (e.g. UserIdUSER_ID)
  • snake_caseCONSTANT_CASE (e.g. first_nameFIRST_NAME, lastLAST)
typescript
import { toConstantCaseKeys } from 'es-toolkit/object';

// Basic object conversion
const obj = { userId: 1, firstName: 'John', lastName: 'Doe' };
const result = toConstantCaseKeys(obj);
// result is { USER_ID: 1, FIRST_NAME: 'John', LAST_NAME: 'Doe' }

// Objects within arrays are also converted
const users = [
  { userId: 1, firstName: 'John' },
  { userId: 2, firstName: 'Jane' },
];
const convertedUsers = toConstantCaseKeys(users);
// convertedUsers is [{ USER_ID: 1, FIRST_NAME: 'John' }, { USER_ID: 2, FIRST_NAME: 'Jane' }]

// Nested objects are fully converted
const nested = {
  userData: {
    userId: 1,
    contactInfo: {
      emailAddress: '[email protected]',
      phoneNumber: '123-456-7890',
    },
  },
};
const nestedResult = toConstantCaseKeys(nested);
// nestedResult is {
//   USER_DATA: {
//     USER_ID: 1,
//     CONTACT_INFO: {
//       EMAIL_ADDRESS: '[email protected]',
//       PHONE_NUMBER: '123-456-7890'
//     }
//   }
// }

Parameters

  • obj (T): The object, array, or primitive value to convert keys to CONSTANT_CASE.

Returns

(ToConstantCaseKeys<T>): Returns a new object with all keys converted to CONSTANT_CASE.