npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

obfo

v0.0.9

Published

Convert HTML Forms into nested JavaScript objects.

Downloads

35

Readme

Obfo (Object Form)

Convert HTML Forms into nested JavaScript objects.

Installation

npm install obfo

Usage

<form id="fo" data-obfo-container="{}">
  <input name="first" value="Jon" />
  <input name="last" value="Doe" />
</form>
import obfo from "obfo";
const fo = document.getElementById("fo");
const ob = obfo(fo);
console.log(ob); // logs { first: "Jon", last: "Doe" }

Attributes

To handle the differences between HTML forms and JavaScript object structures, we use data attributes to define container behavior.

data-obfo-container

The data-obfo-container attribute groups elements into dictionaries ({}) or arrays ([]) when converting a form to an object.

An element is considered a "direct child" of a container if:

  • It is an input (input, textarea, select) element or has a data-obfo-container attribute.
  • There are no other elements between the container and the element.

Elements that are not inputs or do not have the data-obfo-container attribute are ignored. Unless specified with the option.submit parameter, button elements are also ignored.

Note: The data-obfo-container attribute must be set on the top form element.

data-obfo-container: {}

Direct children of data-obfo-container="{}" must have a name or data-obfo-name attribute set.

Example:

<form id="fo" data-obfo-container="{}">
  <input name="first" value="Jon" />
  <input name="last" value="Doe" />
</form>
import obfo from "obfo";
const fo = document.getElementById("fo");
const ob = obfo(fo);
console.log(ob); // logs { first: "Jon", last: "Doe" }

data-obfo-container: []

name and data-obfo-name attributes on direct children of data-obfo-container="[]" are ignored.

Example:

<form id="fo" data-obfo-container="[]">
  <input name="first" value="Jon" />
  <input name="last" value="Doe" />
</form>
import obfo from "obfo";
const fo = document.getElementById("fo");
const ob = obfo(fo);
console.log(ob); // logs ["Jon", "Doe"]

data-obfo-name

This attribute is used as the key if the container is a direct child of a dictionary.

data-obfo-value

This attribute is used to extract the text of a non-input element to use as a value. It should be used in conjunction with data-obfo-name where a key is required.

data-obfo-cast

The data-obfo-cast attribute casts the input value to a specific type. The available types are:

  • string (default) - The value as is.
  • checkbox/radio - true or false based on the "checked" state of the element.
  • number - The value as a number.
  • bigint - The value as a BigInt.
  • date - The value as a Date object.
  • file - Used on input[type=file]. Returns the first file object, which can later be read using FileReader.
  • files - Returns a list of file objects (see file above).
  • null - null
  • undefined - undefined

If data-obfo-cast is used without a value, it defaults to the value of the "type" attribute.

Example:

<input type="checkbox" data-obfo-cast="checkbox" />
<!-- is equivalent to -->
<input type="checkbox" data-obfo-cast />

Comprehensive Examples

Example with nested objects:

<form id="fo" data-obfo-container="{}">
  <input name="first" value="Jon" />
  <input name="last" value="Doe" />
  <div data-obfo-container="{}" data-obfo-name="address">
    <input name="street" value="123 Main St" />
    <input name="city" value="Anytown" />
  </div>
</form>
import obfo from "obfo";
const fo = document.getElementById("fo");
const ob = obfo(fo);
console.log(ob); // logs { first: "Jon", last: "Doe", address: { street: "123 Main St", city: "Anytown" } }

Example with arrays:

<form id="fo" data-obfo-container="{}">
  <div data-obfo-container="[]" data-obfo-name="tags">
    <input value="JavaScript" />
    <input value="HTML" />
    <input value="CSS" />
  </div>
</form>
import obfo from "obfo";
const fo = document.getElementById("fo");
const ob = obfo(fo);
console.log(ob); // logs { tags: ["JavaScript", "HTML", "CSS"] }