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

js-form-data

v1.1.0

Published

Plain-old-JS near-implementation of the FormData web API with analogue methods for browsers with limited support and ergonomic parsing & serialization functionality

Downloads

54

Readme

npm Version Build Status Coverage Status License
GitHub Repo GitHub Follow GitHub stars

JS Form Data

Plain-old-JS near-implementation of the FormData web API with analogue methods for browsers with limited support and ergonomic parsing & serialization functionality

Constructor

Every class that instantiates has one!

JSFormData
Creates a new JSFormData object from a <form> tag and its children.

// in document body:
//<form id="example">
//	<input name="foo" value="bar" />
//	<input name="baz" value="qux" />
//	<input name="bool" value="true" />
//	<input name="bool" value="false" />
//</form>

// params: <form>
const formData = new JSFormData(document.getElementById('example'));
formData.serialize();
// => { foo: 'bar', baz: 'qux', bool: [true, false] }

Class Methods

The JSFormData class has one util method but it's an important one if you need an instance but don't have a DOM element to pass to the constructor

JSFormData::parse
Creates a new JSFormData object from k/v pairs in a plain JS object argument.

// params: <Object>
const formData = JSFormData.parse({ foo: 'bar', baz: true, qux: [1, 2, 3] });
formData.serialize();
// => { foo: 'bar', baz: true, qux: [1, 2, 3] }

Instance Methods

JSFormData instance methods are analogues of the regular FormData web API instance methods, modified to accept and return friendlier objects for easier integration with other processing logic

JSFormData.append
Appends a new value onto an existing key in a JSFormData internal data structure, or adds the key and value if it does not already exist.

// params: <String>, <_>
const formData = new JSFormData();
formData.append('foo', 'bar');
formData.append('foo', 'baz');
formData.append('qux', true);
formData.serialize();
// => { foo: ['bar', 'baz'], qux: true }

// params: <String>, <Blob>, <String>
const formData = new JSFormData();
formData.append('blob', new Blob(), 'blob-name');
formData.serialize().blob.name;
=> 'blob-name'

// params: <Object>
const formData = new JSFormData();
formData.append({ foo: 'bar' });
formData.serialize();
// => { foo: ['bar'] }

JSFormData.delete
Deletes a k/v pair from a JSFormData internal data structure.

// params: <String>
const formData = JSFormData.parse({ foo: 'bar', baz: 'qux'});
formData.delete('foo');
formData.serialize();
// => { baz: 'qux' }

JSFormData.entries
Returns an Array of 2-element Arrays from k/v pairs in JSFormData's internal data structure.

// params: none
const formData = JSFormData.parse({ foo: 'bar', baz: ['qux', 'quux']});
formData.serialize();
// => [['foo', 'bar'], ['baz', ['qux', 'quux]]]

JSFormData.get
Returns the first value associated with a given key in a JSFormData instance.

// params: <String>
const formData = JSFormData.parse({ foo: 'bar', baz: [1, 2] });
formData.get('foo');
// => 'bar'
formData.get('baz');
// => 1

JSFormData.getAll
Returns an Array of all values associated with a given key in a JSFormData instance.

params: <String>
const formData = JSFormData.parse({ foo: 'bar', baz: [1, 2] });
formData.getAll('foo');
// => ['bar']
formData.getAll('baz');
// => [1, 2]

JSFormData.has
Returns a boolean indicating whether a JSFormData instance contains a given key.

params: <String>
const formData = JSFormData.parse({ foo: 'bar', baz: false });
formData.has('foo');
// => true
formData.has('baz');
// => true
formData.has('qux');
// => false

JSFormData.keys
Returns an Array of all the keys in a JSFormData instance.

// params: none
const formData = JSFormData.parse({ foo: 'bar', baz: false, qux: [1, 2, 3] });
formData.keys();
// => ['foo', 'baz', 'qux']

JSFormData.set(<str>, <_>)
Sets a new value for an existing key in a JSFormData instance, or adds a k/v pair if the given key does not already exist. Similar to append, but replaces any existing values on specified keys.

params: <String>, <_>
const formData = JSFormData({ foo: 'bar' });
formData.set('foo', 'baz');
formData.serialize();
 // => { foo: 'baz' }
 
 // params: <String>, <Blob>, <String>
 const formData = new JSFormData();
 formData.set('blob', 'test-value');
 formData.set('blob', new Blob(), 'blob-name');
 formData.serialize().blob.name;
 => 'blob-name'
 
 // params: <Object>
 const formData = new JSFormData();
 formData.set({ foo: 'bar' });
 formData.set({ foo: 'baz' });
 formData.serialize();
 // => { foo: ['baz'] }

JSFormData.values
Returns an Array of values in a JSFormData instance; Array elements are either single values associated 1:1 with a key, or the first of a set of values associated many:1 with a key.

// params: none
const formData = JSFormData.parse({ foo: 'bar', baz: false, qux: [1, 2, 3] });
JSFormData.values();
// => ['bar', false, 1]