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

@locustjs/forms

v2.0.1

Published

This library provides utility functions to interact with forms and form elements in a web page.

Downloads

2

Readme

About

This library provides utility functions to interact with forms and form elements in a web page.

Install

npm i @locustjs/forms

Usage

CommonJs

var someFn = require('@locustjs/forms').someFn;

ES6

import { someFn } from '@locustjs/forms'

Converting an HTML form into JSON

example Form

<form class="my-form">
  <div>
    <label>
      <span>First Name</span>
      <input type="text" name="firstname" />
    </label>
  </div>
  <div>
    <label>
      <span>Last Name</span>
      <input type="text" name="lastname" />
    </label>
  </div>
  <div>
    <label>
      <span>Sex</span>
      <input type="radio" name="sex" value="male" /> Male
      <input type="radio" name="sex" value="female" /> Female
    </label>
  </div>
  <div>
    <label>
      <span>Age group</span>
      <select name="age-group">
        <option value="0">Not specified</option>
        <option value="1">6 and Below 6</option>
        <option value="2">7 to 12</option>
        <option value="3">13 to 19</option>
        <option value="4">20 to 45</option>
        <option value="5">46 and Over 46</option>
      </select>
    </label>
  </div>
  <div>
    <label>
      <span>I Agree</span>
      <input type="checkbox" name="agreement" />
    </label>
  </div>
</form>

Suppose we fill in the form with the following data:

First Name = Johen Last Name = Doe Sex = Male Age Group = 20 to 45 I Agree = -

To get the form data as a json object we can use the following code:

import { toJson } from 'locustjs-forms';

const data = toJson('.my-form');

console.log(data);
/*
  {
    "firstname": "John",
    "lastname": "Doe",
    "sex": "male",
    "age-group": "4",
    "agreement": []
  }
*/

List of utility functions

  • formEachElement(selector, callback, [excludes]): This function iterates over one or more forms whose selector are specified in 'selector' argument and calls the 'callback' argument on each form element it finds in the form. The arguments passed to the callback are: form, element, elementIndex, formIndex. Using the third parameter 'excludes' one can exclude some form elements, so that they are not inlcuded in iteration.
  • formEach(selector, callback, [excludes]): This function does the same thing as formEachElement, except that it ignores buttons and fieldsets (formEachElement also iterates buttons and fieldsets).
  • isEditable(element): This function checks whether given element is an input entry element (user can potentially enter its value) or not. Editable elements are textboxes, radio buttons, checkboxes, textareas, selects.
  • disableForm(selector): This function disables a form and its elements.
  • enableForm(selector): This function enables a form and its elements.
  • clearForm(selector): This function clears a form and its elements.
  • resetForm(selector): This function resets a form and its elements.
  • readOnlyForm(selector): This function makes a form and its elements readonly.
  • unreadOnlyForm(selector): This function makes a form and its elements writable.
  • getValue(form, elementName): This function returns value a form element based on its name.
  • setValue(form, elementName, value): This function sets a value on a form element based on its name.
  • toJson(form, [excludes]): This function serializes a form into json.
  • fromJson(form, obj): This function fills a form based on a json object.
  • toArray(form): This functions serializes form element values into array.
  • fromArray(form, arr): This function fills a form based on an array of values.
  • post(url, data): This function creates an arbitrary form, fills it with data and posts the form to the specified target url in HTTP POST method.
  • Form: This is a helper class with various helper methods.

There is a FormHelper object with methods with the same name as above functions. So, there's no need to import functions separately. We can only import FormHelper and use its methods.

import FormHelper from 'locustjs-forms';

var x = FormHelper.toJson('#my-form');

console.log(x);

FormHelper.clear('#my-form');

FormHelper.fromJson('#my-form');

Form class

This class provides more convenience when working with a form, removing the need to refer to the form over and over again.

  • each(callback)
  • eachElement(callback)
  • enable()
  • disable()
  • readOnly()
  • unreadOnly()
  • clear()
  • reset()
  • fromJson(obj)
  • toJson()
  • fromArray(arr)
  • toArray()
  • getValue(key)
  • setValue(key, value)

Example:

var f = new Form('#my-form');

f.each((frm, el) => console.log(el));
f.enable();
f.disable();
f.readOnly();
f.unreadOnly();
f.reset();

var x = f.toJson();

console.log(x);

f.clear();
f.fromJson(x);

var arr = f.toArray();

console.log(arr);

f.clear();

f.fromArray(arr);

console.log(f.getValue('firstname'));
console.log(f.getValue('age-group'));

f.setValue('firstname', 'John');
f.setValue('age-group', 4);