4cko
/usr/local/rvm/gems/ruby-3.4.7/bin:/usr/local/rvm/gems/ruby-3.4.7@global/bin:/usr/local/rvm/rubies/ruby-3.4.7/bin:/home/codespace/.vscode-remote/data/User/globalStorage/github.copilot-chat/debugCommand:/home/codespace/.vscode-remote/data/User/globalStorage/github.copilot-chat/copilotCli:/vscode/bin/linux-x64/7e7950df89d055b5a378379db9ee14290772148a/bin/remote-cli:/home/codespace/.local/bin:/home/codespace/.dotnet:/home/codespace/nvm/current/bin:/home/codespace/.php/current/bin:/home/codespace/.python/current/bin:/home/codespace/java/current/bin:/home/codespace/.ruby/current/bin:/home/codespace/.local/bin:/usr/local/python/current/bin:/usr/local/py-utils/bin:/usr/local/jupyter:/usr/local/oryx:/usr/local/go/bin:/go/bin:/usr/local/sdkman/bin:/usr/local/sdkman/candidates/java/current/bin:/usr/local/sdkman/candidates/gradle/current/bin:/usr/local/sdkman/candidates/maven/current/bin:/usr/local/sdkman/candidates/ant/current/bin:/usr/local/rvm/gems/default/bin:/usr/local/rvm/gems/default@global/bin:/usr/local/rvm/rubies/default/bin:/usr/local/share/rbenv/bin:/usr/local/php/current/bin:/opt/conda/bin:/usr/local/nvs:/usr/local/share/nvm/versions/node/v24.14.0/bin:/usr/local/hugo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/share/dotnet:/home/codespace/.dotnet/tools:/usr/local/rvm/bin
a2c64e7 | ; | |
| import utils from '../utils.js'; | |
| import AxiosError from '../core/AxiosError.js'; | |
| import { DEFAULT_FORM_DATA_MAX_DEPTH } from './toFormData.js'; | |
| const MAX_DEPTH = DEFAULT_FORM_DATA_MAX_DEPTH; | |
| function throwIfDepthExceeded(index) { | |
| if (index > MAX_DEPTH) { | |
| throw new AxiosError( | |
| 'FormData field is too deeply nested (' + index + ' levels). Max depth: ' + MAX_DEPTH, | |
| AxiosError.ERR_FORM_DATA_DEPTH_EXCEEDED | |
| ); | |
| } | |
| } | |
| /** | |
| * It takes a string like `foo[x][y][z]` and returns an array like `['foo', 'x', 'y', 'z'] | |
| * | |
| * @param {string} name - The name of the property to get. | |
| * | |
| * @returns An array of strings. | |
| */ | |
| function parsePropPath(name) { | |
| // foo[x][y][z] | |
| // foo.x.y.z | |
| // foo-x-y-z | |
| // foo x y z | |
| const path = []; | |
| const pattern = /\w+|\[(\w*)]/g; | |
| let match; | |
| while ((match = pattern.exec(name)) !== null) { | |
| throwIfDepthExceeded(path.length); | |
| path.push(match[0] === '[]' ? '' : match[1] || match[0]); | |
| } | |
| return path; | |
| } | |
| /** | |
| * Convert an array to an object. | |
| * | |
| * @param {Array<any>} arr - The array to convert to an object. | |
| * | |
| * @returns An object with the same keys and values as the array. | |
| */ | |
| function arrayToObject(arr) { | |
| const obj = {}; | |
| const keys = Object.keys(arr); | |
| let i; | |
| const len = keys.length; | |
| let key; | |
| for (i = 0; i < len; i++) { | |
| key = keys[i]; | |
| obj[key] = arr[key]; | |
| } | |
| return obj; | |
| } | |
| /** | |
| * It takes a FormData object and returns a JavaScript object | |
| * | |
| * @param {string} formData The FormData object to convert to JSON. | |
| * | |
| * @returns {Object<string, any> | null} The converted object. | |
| */ | |
| function formDataToJSON(formData) { | |
| function buildPath(path, value, target, index) { | |
| throwIfDepthExceeded(index); | |
| let name = path[index++]; | |
| if (name === '__proto__') return true; | |
| const isNumericKey = Number.isFinite(+name); | |
| const isLast = index >= path.length; | |
| name = !name && utils.isArray(target) ? target.length : name; | |
| if (isLast) { | |
| if (utils.hasOwnProp(target, name)) { | |
| target[name] = utils.isArray(target[name]) | |
| ? target[name].concat(value) | |
| : [target[name], value]; | |
| } else { | |
| target[name] = value; | |
| } | |
| return !isNumericKey; | |
| } | |
| if (!utils.hasOwnProp(target, name) || !utils.isObject(target[name])) { | |
| target[name] = []; | |
| } | |
| const result = buildPath(path, value, target[name], index); | |
| if (result && utils.isArray(target[name])) { | |
| target[name] = arrayToObject(target[name]); | |
| } | |
| return !isNumericKey; | |
| } | |
| if (utils.isFormData(formData) && utils.isFunction(formData.entries)) { | |
| const obj = {}; | |
| utils.forEachEntry(formData, (name, value) => { | |
| buildPath(parsePropPath(name), value, obj, 0); | |
| }); | |
| return obj; | |
| } | |
| return null; | |
| } | |
| export default formDataToJSON; | |