Observed since 20 Oct, 19:14 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
A lightweight TypeScript utility library for seamlessly converting between FormData and nested JavaScript objects. Perfect for handling complex form submissions with hierarchical data structures.
A lightweight TypeScript utility library for seamlessly converting between FormData and nested JavaScript objects. Perfect for handling complex form submissions with hierarchical data structures.
✨ Bidirectional Conversion
🎯 Smart Type Handling
🏗️ Nested Structure Support
user.address.street)items.0.name, items.1.name)✅ Fully Tested
npm install 2k-form-data-to-object
or
yarn add 2k-form-data-to-object
import { transformFormDataToObject } from "2k-form-data-to-object";
const formData = new FormData();
formData.append("name", "John");
formData.append("age", "30");
formData.append("active", "on");
formData.append("user.email", "john@example.com");
formData.append("user.address.city", "New York");
const result = transformFormDataToObject(formData);
// Result:
// {
// name: "John",
// age: 30,
// active: true,
// user: {
// email: "john@example.com",
// address: {
// city: "New York"
// }
// }
// }
import { transformObjectToFormData } from "2k-form-data-to-object";
const data = {
name: "John",
age: 30,
active: true,
profile: {
email: "john@example.com",
avatar: fileObject, // File or Blob
},
tags: ["javascript", "typescript"],
};
const formData = transformObjectToFormData(data);
// FormData entries:
// name: "John"
// age: "30"
// active: "true"
// profile.email: "john@example.com"
// profile.avatar: File
// tags.0: "javascript"
// tags.1: "typescript"
transformFormDataToObject(formData: FormData): UnknownObjectConverts FormData to a nested JavaScript object with automatic type coercion.
formData (FormData): The FormData object to transform| Input | Output | Example |
|---|---|---|
"true"/"false" | boolean | "true"→true |
"on"/"off" | boolean | "on"→true |
| Numeric string | number | "42"→42 |
| File object | File | File→File(unchanged) |
| Regular string | string | "hello"→"hello" |
user.name → { user: { name: "..." } }items.0.id → { items: [{ id: "..." }] }config.nested.deep.value → deeply nested objectstransformObjectToFormData(object: Record<string, FormDataValue>): FormDataConverts a nested JavaScript object to FormData with proper serialization.
object (Record<string, FormDataValue>): The object to transformtype FormDataValue =
| string
| number
| boolean
| bigint
| Date
| File
| Blob
| null
| undefined
| FormDataValue[]
| { [key: string]: FormDataValue };
| Input Type | Serialized As | Notes |
|---|---|---|
File/Blob | Raw value | Preserved as-is |
Date | ISO 8601 string | UsestoISOString() |
null/undefined | Omitted | Not included in FormData |
Array | Indexed keys | [a, b]→0: a, 1: b |
Object | Nested keys | {x: y}→x: y |
| Primitives | String | true→"true",42→"42" |
const form = new FormData();
form.append("user.profile.firstName", "Jane");
form.append("user.profile.lastName", "Doe");
form.append("user.contacts.0", "jane@example.com");
form.append("user.contacts.1", "jane.doe@work.com");
form.append("user.active", "true");
form.append("user.age", "28");
const result = transformFormDataToObject(form);
// {
// user: {
// profile: {
// firstName: "Jane",
// lastName: "Doe"
// },
// contacts: ["jane@example.com", "jane.doe@work.com"],
// active: true,
// age: 28
// }
// }
const formData = new FormData();
const file = new File(["content"], "document.pdf");
formData.append("document", file);
formData.append("metadata.title", "My Document");
const result = transformFormDataToObject(formData);
// {
// document: File,
// metadata: {
// title: "My Document"
// }
// }
const data = {
createdAt: new Date("2024-01-15"),
updatedAt: new Date("2024-01-20"),
name: "Project",
};
const formData = transformObjectToFormData(data);
// FormData contains:
// createdAt: "2024-01-15T00:00:00.000Z"
// updatedAt: "2024-01-20T00:00:00.000Z"
// name: "Project"
// Object to FormData
const data = {
items: [
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" },
],
};
const formData = transformObjectToFormData(data);
// FormData entries:
// items.0.id: "1"
// items.0.name: "Item 1"
// items.1.id: "2"
// items.1.name: "Item 2"
The library includes a comprehensive Jest test suite covering:
Run tests with:
npm test
Full TypeScript support with strict type safety:
import {
transformFormDataToObject,
transformObjectToFormData,
} from "@2_k/2k-form-data-to-object";
// Type-safe usage
const data: Record<string, FormDataValue> = {
name: "John",
age: 30,
avatar: fileObject,
};
const formData = transformObjectToFormData(data);
const result = transformFormDataToObject(formData);
Optimized for typical form sizes with:
Send your donation directly to:
my Orange Money 📱 +261 37 65 442 50
Or Mvola 📱 +261 34 68 814 74
Or Airtel Money 📱 +261 33 36 613 20
Any amount is deeply appreciated! 🤝
MIT
Download counts and popularity will appear here as the collector receives them.
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 19:14 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:0f8f1c4f82b1121e3b0a06e434eb88c76f244fd214db59cfa2db355162186e6c