PHP 8.2.30
Preview: formDataToJSON.js Size: 2.11 KB
/home/byroehnu/.trash/node_modules11/axios/lib/helpers/formDataToJSON.js

'use strict';

import utils from '../utils.js';

/**
 * 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
  return utils.matchAll(/\w+|\[(\w*)]/g, name).map(match => {
    return match[0] === '[]' ? '' : match[1] || match[0];
  });
}

/**
 * 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) {
    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] = [target[name], value];
      } else {
        target[name] = value;
      }

      return !isNumericKey;
    }

    if (!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;

Directory Contents

Dirs: 0 × Files: 32

Name Size Perms Modified Actions
3.59 KB lrw-r--r-- 2026-02-20 03:53:04
Edit Download
1.41 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
444 B lrw-r--r-- 2026-02-20 03:53:06
Edit Download
1.54 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
372 B lrw-r--r-- 2026-02-20 03:53:06
Edit Download
382 B lrw-r--r-- 2026-02-20 03:53:06
Edit Download
1.33 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
1.30 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
746 B lrw-r--r-- 2026-02-20 03:53:06
Edit Download
2.01 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
2.11 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
2.90 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
1.33 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
1.72 KB lrw-r--r-- 2026-02-20 03:53:06
Edit Download
618 B lrw-r--r-- 2026-02-20 03:53:06
Edit Download
371 B lrw-r--r-- 2026-02-20 03:53:06
Edit Download
420 B lrw-r--r-- 2026-02-20 03:53:08
Edit Download
56 B lrw-r--r-- 2026-02-20 03:53:08
Edit Download
1.35 KB lrw-r--r-- 2026-02-20 03:53:08
Edit Download
151 B lrw-r--r-- 2026-02-20 03:53:08
Edit Download
1.21 KB lrw-r--r-- 2026-02-20 03:53:08
Edit Download
318 B lrw-r--r-- 2026-02-20 03:53:08
Edit Download
351 B lrw-r--r-- 2026-02-20 03:53:18
Edit Download
2.15 KB lrw-r--r-- 2026-02-20 03:53:08
Edit Download
1.07 KB lrw-r--r-- 2026-02-20 03:53:08
Edit Download
566 B lrw-r--r-- 2026-02-20 03:53:08
Edit Download
852 B lrw-r--r-- 2026-02-20 03:53:10
Edit Download
5.97 KB lrw-r--r-- 2026-02-20 03:53:10
Edit Download
540 B lrw-r--r-- 2026-02-20 03:53:10
Edit Download
1.65 KB lrw-r--r-- 2026-02-20 03:53:10
Edit Download
2.72 KB lrw-r--r-- 2026-02-20 03:53:12
Edit Download
681 B lrw-r--r-- 2026-02-20 03:53:12
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).