UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

67 lines (66 loc) 2.01 kB
const require_isArray = require("../compat/predicate/isArray.js"); const require_isPlainObject = require("../compat/predicate/isPlainObject.js"); const require_kebabCase = require("../string/kebabCase.js"); //#region src/object/toKebabCaseKeys.ts /** * Creates a new object composed of the properties with keys converted to kebab-case. * * This function takes an object and returns a new object that includes the same properties, * but with all keys converted to kebab-case format. * * @template T - The type of object. * @param obj - The object to convert keys from. * @returns A new object with all keys converted to kebab-case. * * @example * // Example with objects * const obj = { userId: 1, firstName: 'John' }; * const result = toKebabCaseKeys(obj); * // result will be { 'user-id': 1, 'first-name': 'John' } * * // Example with arrays of objects * const arr = [ * { userId: 1, firstName: 'John' }, * { userId: 2, firstName: 'Jane' } * ]; * const arrResult = toKebabCaseKeys(arr); * // arrResult will be [{ 'user-id': 1, 'first-name': 'John' }, { 'user-id': 2, 'first-name': 'Jane' }] * * // Example with nested objects * const nested = { * userData: { * userId: 1, * userAddress: { * streetName: 'Main St', * zipCode: '12345' * } * } * }; * const nestedResult = toKebabCaseKeys(nested); * // nestedResult will be: * // { * // 'user-data': { * // 'user-id': 1, * // 'user-address': { * // 'street-name': 'Main St', * // 'zip-code': '12345' * // } * // } * // } */ function toKebabCaseKeys(obj) { if (require_isArray.isArray(obj)) return obj.map((item) => toKebabCaseKeys(item)); if (require_isPlainObject.isPlainObject(obj)) { const result = {}; const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const kebabKey = require_kebabCase.kebabCase(key); result[kebabKey] = toKebabCaseKeys(obj[key]); } return result; } return obj; } //#endregion exports.toKebabCaseKeys = toKebabCaseKeys;