Catalog npm
Back to packageRELEASE DETAILS

1.0.8

npm

2k-form-data-to-object

A lightweight TypeScript utility library for seamlessly converting between FormData and nested JavaScript objects. Perfect for handling complex form submissions with hierarchical data structures.

array-handlingbidirectionalblob-handlingbrowser-apiconversiondata-mapperdata-structuredata-transformationdate-serializationdeep-nestingdeserializationdot-notationes6file-uploadformform-data
All keywords (45)
form-handlingform-parserform-submissionform-validationformdataformdata-parserfully-testedjavascriptjestjson-conversionlightweightmodernmultipartnested-datanested-formsnested-objectsobject-mapperobject-restructuringobject-serializationserializationtransformtwo-way-bindingtype-coerciontype-conversiontype-safetypescriptutilityweb-apizero-dependencies
Published5 Sept 2026
Direct dependencies8
Transitive dependenciesNot calculated

README

Current package documentation

2k-form-data-to-object

A lightweight TypeScript utility library for seamlessly converting between FormData and nested JavaScript objects. Perfect for handling complex form submissions with hierarchical data structures.

Features

✨ Bidirectional Conversion

  • Transform FormData to nested objects
  • Convert nested objects back to FormData
  • Maintain data integrity through round-trip conversions

🎯 Smart Type Handling

  • Automatic type inference for strings, numbers, and booleans
  • Preserve File and Blob objects during conversion
  • Support for Date serialization (ISO 8601)
  • Handle null and undefined values intelligently

🏗️ Nested Structure Support

  • Deep object nesting with dot notation (user.address.street)
  • Array indexing support (items.0.name, items.1.name)
  • Mixed nested objects and arrays

✅ Fully Tested

  • Comprehensive Jest test suite
  • Edge case coverage
  • Type-safe with TypeScript

Installation

npm install 2k-form-data-to-object

or

yarn add 2k-form-data-to-object

Quick Start

FormData → 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"
//     }
//   }
// }

Object → FormData

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"

API Reference

transformFormDataToObject(formData: FormData): UnknownObject

Converts FormData to a nested JavaScript object with automatic type coercion.

Parameters

  • formData (FormData): The FormData object to transform

Returns

  • (UnknownObject): A nested object with intelligently typed values

Type Conversion Rules

InputOutputExample
"true"/"false"boolean"true"→true
"on"/"off"boolean"on"→true
Numeric stringnumber"42"→42
File objectFileFile→File(unchanged)
Regular stringstring"hello"→"hello"

Dot Notation Examples

  • user.name → { user: { name: "..." } }
  • items.0.id → { items: [{ id: "..." }] }
  • config.nested.deep.value → deeply nested objects

transformObjectToFormData(object: Record<string, FormDataValue>): FormData

Converts a nested JavaScript object to FormData with proper serialization.

Parameters

  • object (Record<string, FormDataValue>): The object to transform

Returns

  • (FormData): A FormData object ready for submission

Supported Types

type FormDataValue =
  | string
  | number
  | boolean
  | bigint
  | Date
  | File
  | Blob
  | null
  | undefined
  | FormDataValue[]
  | { [key: string]: FormDataValue };

Serialization Rules

Input TypeSerialized AsNotes
File/BlobRaw valuePreserved as-is
DateISO 8601 stringUsestoISOString()
null/undefinedOmittedNot included in FormData
ArrayIndexed keys[a, b]→0: a, 1: b
ObjectNested keys{x: y}→x: y
PrimitivesStringtrue→"true",42→"42"

Usage Examples

Complex Nested Form

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
//   }
// }

File Handling

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"
//   }
// }

Date Serialization

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"

Array Handling

// 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"

Testing

The library includes a comprehensive Jest test suite covering:

  • ✅ Basic type conversions
  • ✅ Nested object transformations
  • ✅ Array handling and indexing
  • ✅ File and Blob preservation
  • ✅ Date serialization
  • ✅ Edge cases (null, undefined, empty values)
  • ✅ Round-trip conversions (object → FormData → object)

Run tests with:

npm test

TypeScript Support

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);

Use Cases

  • 📝 Form Submission: Convert HTML form data to structured objects
  • 🔄 API Integration: Prepare nested data for multipart form requests
  • 📤 File Uploads: Handle file uploads with additional metadata
  • 🗂️ Data Validation: Work with typed objects before form submission
  • 🔁 Data Serialization: Round-trip conversion for data persistence

Performance

Optimized for typical form sizes with:

  • Minimal memory overhead
  • Single-pass processing
  • Efficient recursion handling
  • No external dependencies

💛 Donate via Orange Money (Madagascar)

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! 🤝

License

MIT

Changelog

v1.0.0

  • Initial release
  • FormData ↔ Object conversion
  • Type inference for primitives
  • Nested object and array support
  • File/Blob preservation
  • Date serialization

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 8

Native requirements
8 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^30.5.1development
rimraf
Environment
{
  "local_name": "rimraf",
  "alias": false,
  "peer_metadata": null
}
^6.1.3development
ts-jest
Environment
{
  "local_name": "ts-jest",
  "alias": false,
  "peer_metadata": null
}
^29.4.12development
@swc/core
Environment
{
  "local_name": "@swc/core",
  "alias": false,
  "peer_metadata": null
}
^1.16.2development
@swc/jest
Environment
{
  "local_name": "@swc/jest",
  "alias": false,
  "peer_metadata": null
}
^0.2.39development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^6.0.2development
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
^30.0.0development
jest-environment-jsdom
Environment
{
  "local_name": "jest-environment-jsdom",
  "alias": false,
  "peer_metadata": null
}
^30.5.1development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "6769fc458d523289c0a3d30c941c4faef98bdd85",
  "tarball": "https://registry.npmjs.org/2k-form-data-to-object/-/2k-form-data-to-object-1.0.8.tgz",
  "fileCount": 11,
  "integrity": "sha512-cB2nwc4W7XNssR4lHk+zZEX7GzWG70ZEvYmvWeg74LU7x8G+TK5mt62y0mvioStwgdSEqLBGYuNpKRkCJOP52w==",
  "signatures": [
    {
      "sig": "MEYCIQCnzmcKjTZw3JEcd7lba9GLVk6bz+4ZFHdNERWLm5Ky+QIhANLhvXQxmMQ8sBA19uodAj0/JGFN8l/w2PJ7h2JKYc64",
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
    },
    {
      "sig": "MEUCIGS0/oGz6Sg8Bjuuygk7jnW9g0RUUX5+AsRAgQ/ADNCaAiEAmPVeb10lOBFfZHeRKNfmF2sizztj9aaWicGfCPQunXs=",
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
    }
  ],
  "unpackedSize": 16259
}

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:14 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:03ceadff4ab771ff3f2ad41e90280fe7be45a141704c7c0c19e5a7f3d6ee8dae

Other collected versions

1.0.1410 Sept1.0.105 Sept1.0.95 Sept1.0.75 Sept1.0.65 Sept1.0.45 SeptView all collected versions →