UNPKG

1.21 kBJavaScriptView Raw
1"use strict";
2const camel_case_1 = require("camel-case");
3function getter(key) {
4 return function () {
5 return this[key];
6 };
7}
8function setter(key) {
9 return function (value) {
10 this[key] = value;
11 };
12}
13function toCamelCase(str) {
14 let prefixLength = -1;
15 while (str[++prefixLength] === '_')
16 ;
17 if (!prefixLength) {
18 return (0, camel_case_1.camelCase)(str);
19 }
20 return str.substring(0, prefixLength) + (0, camel_case_1.camelCase)(str.substring(prefixLength));
21}
22function camelCaseKeys(obj) {
23 if (typeof obj !== 'object')
24 throw new TypeError('obj must be an object!');
25 const keys = Object.keys(obj);
26 const result = {};
27 for (const oldKey of keys) {
28 const newKey = toCamelCase(oldKey);
29 result[newKey] = obj[oldKey];
30 if (newKey !== oldKey) {
31 Object.defineProperty(result, oldKey, {
32 get: getter(newKey),
33 set: setter(newKey),
34 configurable: true,
35 enumerable: true
36 });
37 }
38 }
39 return result;
40}
41module.exports = camelCaseKeys;
42//# sourceMappingURL=camel_case_keys.js.map
\No newline at end of file