You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openNG/old/lib/formdata-to-object.js

33 lines
858 B
JavaScript

'use strict';
const qs = require("qs");
// FIXME: Can this also take a URLSearchParams?
module.exports = function formDataToObject(formData) {
let items = Array.from(formData.entries()).reduce((items, [key, value]) => {
if (key.match(/\[\]$/)) {
/* Array item */
if (items[key] == null) {
items[key] = [];
}
items[key].push(value);
} else {
/* Single item */
items[key] = value;
}
return items;
}, {});
console.log("ITEMS", qs.stringify(items));
/* This is a bit of a hack... we have an array of values that may or may not
involve array and object specifications, and the `items` object is an
object that contains a flat set of them as keys and values. By passing it
into `stringify` and `parse` consecutively, we get back a properly nested
version of it. */
return qs.parse(qs.stringify(items));
};