obj-to-fd

0.1.5 • Public • Published

obj-to-fd npm version

Clean and minimalist utility to convert a plain JavaScript Object into a FormData Object (multipart/form-data).

  • Functional
  • Recursive
  • No dependencies
  • ~ 20 LOC

Usage

Importing the utility:

const objToFd = require('obj-to-fd');

Converting obj to a FormData Object:

const obj = { person: { name: "Bob", age: 25, children: ["Sarah", "Kyle"] } };
const fData = objToFd(obj);

// fData will look something like:
// person[name]: "Bob", person[age]: "25", person[children][]: "Sarah", person[children][]: "Kyle" ...

Nesting

Nested objects are handled elegantly by recursion. Note: As a FormData Object cannot contain an Object entry, some cases like arrays of Objects (i.e. [{key: val}, {key: val}]) have to be parsed before either by:

  • Converting the above to two seperate arrays obj[keys][] and obj[values][]
  • Converting the above to use indices as keys obj[0][key], obj[1][key] etc.

Readme

Keywords

none

Package Sidebar

Install

npm i obj-to-fd

Weekly Downloads

0

Version

0.1.5

License

MIT

Unpacked Size

10.9 kB

Total Files

7

Last publish

Collaborators

  • klintmane