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

form-gather

v1.0.0

Published

JavaScript HTML Form to object conversion library.

Downloads

3

Readme

form-gather

JavaScript HTML Form to object conversion library.

Works similar to browsers FormData but returns a plain object and works in IE 9+ without polyfills.

Features:

  • Does not depend on FormData browser API.
  • Customizable - you can decide how to handle every form element.
  • No dependencies.
  • Written with TypeScript.

Getting started

$ npm install form-gather

Sample

Let's say we have a form:

<form>
    <input type="text" name="str" />
    <input type="number" name="number" />
    <input type="date" name="date" />
    <input type="checkbox" name="bool" value="1" />

    <input type="radio" name="radio" value="1" />
    <input type="radio" name="radio" value="2" />
    <input type="radio" name="radio" value="3" />

    <select name="select">
        <option value="value0">One</option>
        <option value="value1" selected>Two</option>
    </select>
</form>

Then the following code on form submission

import { getFormData } from 'form-gather'

function onSumbit(event) {
    let form = event.target

    const data = getFormData(form)

    console.log(data)
}

will output

{
    str: '',
    number: 42,
    date: Date(),
    checkbox: '1',
    radio: [null, 2, null],
    select: 'value1'
}

How it works

getFormData internally looks like the following:

const handler = combine(...defaultHandlers)
const getFormData = (form) => gather(form.elements, handler)

gather

The main function is gather. It takes HTML elements array and a single function to retrieve form elements values.

Consider the example below:

let element = document.createElement('input')
element.name = 'test'
element.value = '42'

let form = gather([element], context => context.value)

console.log(form) // output: { test: '42' }

The two important parts are:

  • elements MUST be of type HTMLElement (i.e. nodeType === Element.ELEMENT_NODE).
  • elements MUST have name property, otherwise there's no key to bind value to.

The function argument returns the value, assosiated with the element. The whole thing may be simplified to:

let name = 'test'
let context = { value: '42' }

let form = {
    [name]: context.value
}

And that's basically it. The other interesting things:

  • gather may take plain array (Array<Element>) or HTML-like collection ({ length: number, item: (i) => Element })
  • function argument accepts context which looks like:
    type Context = {
        element: HTMLElement,
        tagName: string,
        type?: string | null,
        value?: string | null,
        checked?: boolean,
        multiple: boolean,
    }

Value handlers

gather functional argument is implemented by chaining together several value handlers.

combine function is used for chaining. It accepts spread array of functions with signature like:

handler(context: Context, next: (context: Context) => any): any

The second argument allows you to call the next handler, which may be used to skip execution or to alter the next result. When the handler processed the element fully, it simply skips the next call, terminating the chain.

The are many default handlers, but you can add your own. Simply use

import { gather, defaultHandlers, combine } from 'form-gather'

const customHandlers = []

const handler = combine(
    ...customHandlers,
    ...defaultHandlers
)

const getFormData = (form) => gather(form.elements, handler)