UNPKG

keys-converter

Version:

This package provide a util function to convert snake case object keys to camel case

57 lines 1.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.objectKeysToSnakeCaseV2 = void 0; const get_params_1 = __importDefault(require("./get_params")); const transform_snake_case_key_1 = __importDefault(require("./transform_snake_case_key")); /** * * @param obj `{ key: value }` * @returns obj `{ key: value }` * * * @argument Result type as Object interface to type the return * @argument Input type as Object interface to validate arguments type * @example * objectKeysToSnakeCaseV2<Result, Input>({}); * * @example * * interface Input { * goodMorning: string * goodAfternoon: string * } * * interface Result { * good_morning: string, * good_afternoon: string * } * * const result = objectKeysToSnakeCaseV2<Result, Input>( * { * goodMorning: "Its a beautiful day", * goodAfternoon: "Nice afternoon" * } * ); * */ const objectKeysToSnakeCaseV2 = function (obj) { const resultsForKey = get_params_1.default(obj); if (resultsForKey.length < 1) { return {}; } let result = {}; const valuesForKeys = Object.values(obj); let index = 0; while (resultsForKey[index]) { const camelCaseKey = transform_snake_case_key_1.default(resultsForKey[index].ranges); result = Object.assign(result, { [camelCaseKey]: valuesForKeys[index] }); index++; } return result; }; exports.objectKeysToSnakeCaseV2 = objectKeysToSnakeCaseV2; exports.default = objectKeysToSnakeCaseV2; //# sourceMappingURL=object_keys_to_snake_case_v2.js.map