智慧水务管理系统 - 精河县供水工程综合管理平台

formDataToJSON.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. import utils from '../utils.js';
  3. import AxiosError from '../core/AxiosError.js';
  4. import { DEFAULT_FORM_DATA_MAX_DEPTH } from './toFormData.js';
  5. const MAX_DEPTH = DEFAULT_FORM_DATA_MAX_DEPTH;
  6. function throwIfDepthExceeded(index) {
  7. if (index > MAX_DEPTH) {
  8. throw new AxiosError(
  9. 'FormData field is too deeply nested (' + index + ' levels). Max depth: ' + MAX_DEPTH,
  10. AxiosError.ERR_FORM_DATA_DEPTH_EXCEEDED
  11. );
  12. }
  13. }
  14. /**
  15. * It takes a string like `foo[x][y][z]` and returns an array like `['foo', 'x', 'y', 'z']
  16. *
  17. * @param {string} name - The name of the property to get.
  18. *
  19. * @returns An array of strings.
  20. */
  21. function parsePropPath(name) {
  22. // foo[x][y][z]
  23. // foo.x.y.z
  24. // foo-x-y-z
  25. // foo x y z
  26. const path = [];
  27. const pattern = /\w+|\[(\w*)]/g;
  28. let match;
  29. while ((match = pattern.exec(name)) !== null) {
  30. throwIfDepthExceeded(path.length);
  31. path.push(match[0] === '[]' ? '' : match[1] || match[0]);
  32. }
  33. return path;
  34. }
  35. /**
  36. * Convert an array to an object.
  37. *
  38. * @param {Array<any>} arr - The array to convert to an object.
  39. *
  40. * @returns An object with the same keys and values as the array.
  41. */
  42. function arrayToObject(arr) {
  43. const obj = {};
  44. const keys = Object.keys(arr);
  45. let i;
  46. const len = keys.length;
  47. let key;
  48. for (i = 0; i < len; i++) {
  49. key = keys[i];
  50. obj[key] = arr[key];
  51. }
  52. return obj;
  53. }
  54. /**
  55. * It takes a FormData object and returns a JavaScript object
  56. *
  57. * @param {string} formData The FormData object to convert to JSON.
  58. *
  59. * @returns {Object<string, any> | null} The converted object.
  60. */
  61. function formDataToJSON(formData) {
  62. function buildPath(path, value, target, index) {
  63. throwIfDepthExceeded(index);
  64. let name = path[index++];
  65. if (name === '__proto__') return true;
  66. const isNumericKey = Number.isFinite(+name);
  67. const isLast = index >= path.length;
  68. name = !name && utils.isArray(target) ? target.length : name;
  69. if (isLast) {
  70. if (utils.hasOwnProp(target, name)) {
  71. target[name] = utils.isArray(target[name])
  72. ? target[name].concat(value)
  73. : [target[name], value];
  74. } else {
  75. target[name] = value;
  76. }
  77. return !isNumericKey;
  78. }
  79. if (!utils.hasOwnProp(target, name) || !utils.isObject(target[name])) {
  80. target[name] = [];
  81. }
  82. const result = buildPath(path, value, target[name], index);
  83. if (result && utils.isArray(target[name])) {
  84. target[name] = arrayToObject(target[name]);
  85. }
  86. return !isNumericKey;
  87. }
  88. if (utils.isFormData(formData) && utils.isFunction(formData.entries)) {
  89. const obj = {};
  90. utils.forEachEntry(formData, (name, value) => {
  91. buildPath(parsePropPath(name), value, obj, 0);
  92. });
  93. return obj;
  94. }
  95. return null;
  96. }
  97. export default formDataToJSON;