formatObjKey.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.formatObjKey = void 0;
  4. function formatObjKey(obj, type, options) {
  5. if (obj === null || typeof obj !== 'object') {
  6. return obj;
  7. }
  8. let o;
  9. if (Array.isArray(obj)) {
  10. o = [];
  11. for (let i = 0; i < obj.length; i++) {
  12. o.push(formatObjKey(obj[i], type, options));
  13. }
  14. }
  15. else {
  16. o = {};
  17. Object.keys(obj).forEach((key) => {
  18. o[handelFormat(key, type, options)] = formatObjKey(obj[key], type, options);
  19. });
  20. }
  21. return o;
  22. }
  23. exports.formatObjKey = formatObjKey;
  24. function handelFormat(key, type, options) {
  25. var _a;
  26. if (options && ((_a = options.exclude) === null || _a === void 0 ? void 0 : _a.includes(key)))
  27. return key;
  28. if (type === 'firstUpperCase') {
  29. key = key.replace(/^./, (_) => _.toUpperCase());
  30. }
  31. else if (type === 'firstLowerCase') {
  32. key = key.replace(/^./, (_) => _.toLowerCase());
  33. }
  34. return key;
  35. }