More reorganization and initial form interception implementation
parent
cd0238c86b
commit
48073c0a0c
@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
const qs = require("qs");
|
||||
|
||||
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));
|
||||
};
|
Loading…
Reference in New Issue