reformdata
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

reformdata (re:FormData)

Build Coverage Downloads Version deno.land/x/reformdata License Language Typescript

Reform FormData to Object or Array format.

Usage

with Deno

import { reform } from "https://deno.land/x/reformdata/mod.ts";

const form = new FormData();

form.append("multiple[]", "1");
form.append("multiple[]", "2");
form.append("multiple[]", "3");
form.append("multiple[]", "4");

reform(form); // { multiple: ["1", "2", "3", "4"] }

with Node.js & Browser

Install

npm install reformdata
import { reform } from "reformdata";

// Usage is as above :-)

Support File or Blob

const form = new FormData();
form.append("text", "plain text");
form.append("blob1", new Blob());
form.append("blob2", new Blob([], { type: "application/json" }), "data.json");
form.append("file", new File([], "data.json", { type: "application/json" }));

reform(form);
/*
{
  text: "plain text",
  blob1: File
  blob2: File
  file: File
}
*/

Array

const form = new FormData();

form.append("multiple[]", "1");
form.append("multiple[]", "2");
form.append("multiple[]", "3");
form.append("multiple[]", "4");

reform(form); // { multiple: ["1", "2", "3", "4"] }

n-depth Array

const form = new FormData();

form.append("multiple[0][]", "1");
form.append("multiple[0][]", "2");
form.append("multiple[1][]", "3");
form.append("multiple[1][]", "4");

reform(form), // { multiple: [["1", "2"], ["3", "4"]] }

Object

const form = new FormData();

// dot notation
form.append("users[1].name", "wan3land");
form.append("users[0].id", "10");

// bracket notation
form.append("users[1][id]", "11");
form.append("users[0][name]", "wan2land");

reform(form);
/*
{
  users: [
    { id: "10", name: "wan2land" },
    { id: "11", name: "wan3land" },
  ],
}
*/

Example with Deno

import { serve } from "https://deno.land/std@0.161.0/http/server.ts";
import reform from "https://deno.land/x/reformdata/mod.ts";

serve(async (request) => {
  if (request.method === "POST") {
    const formData = reform(await request.formData());

    // do something
  }

  return new Response(":D");
});

Readme

Keywords

Package Sidebar

Install

npm i reformdata

Weekly Downloads

5

Version

0.2.2

License

MIT

Unpacked Size

13.3 kB

Total Files

13

Last publish

Collaborators

  • wan2land