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

field-assist

v4.0.2

Published

Making reading/populating input fields easy and fun

Downloads

16

Readme

Field-Assist

TL;DR : Two functions:

  1. just write collectValues(myFormNode) and get a proper JSON with all the values in all the input fields. There are more related goodies there, see below + demo or the example.

  2. Just write const form = new AutoForm(); form.render(values, document.body); and get an HTML form for your data (of course you can customize it, @see FormMetaData)

Install

npm install field-assist

API documentation

Full API documentation (dosn't yet include the auto-form)

Also, see the robust, yet simple example in example/index.html.

*Note that there's a TypeScript declaration file, too.

General

This cute library makes working with input fields, inside or outside of forms, very easy, fun, readable and extremely simple. It's super light and has no dependencies, and it is TypeScript ready out of the box.

It's main function is, perhaps, collectValues( baseElement) that basically does the following:

  1. Recursively collect all the values from any input fields under the baseElement that have a ref attribute (with a logical name as a value).
  2. Places them in a simple map, where the ref value is the entry's key, where its is the normalized from the input field.
  3. Validates the values and create an error object (the "_error") in the returned map

Features

  • One function call to collect all values in a neat JSON
  • One function call to populate all fields from JSON
  • Supports nested fields (with dot notation in the "ref" names)
  • Tiny and self-contained, zero dependencies
  • Support partial updates (both for reading and populating)

Field Population

There's another very cute function, called populateFields, which does the opposite: it takes a map of ref-names to values, and populate the fields with the values. It is also normalized (e.g. select/checkboxes/radio-buttons are also handled). It is the opposite of the collectValues method, of sorts.

Normalization

The normalization means that checkable items (radio buttons groups, multi-selects, checkboxes, textareas) appear in the map as you would expect them to appear: as booleans, a single value or an array of values, so it would be trivial to work with.

(you're welcome to suggest other conversions/normalizatons)

Validation

The validation supports the standard modern HTML validation, of course, plus the ability to easily add a custom javascript validator, using an attribute "validator".

If a value failed validation, the map entry for it would be the symbol Invalid and its erroneous value would still be easily accessible via the map under the "_errors" entry.

custom validator

The custom validator should be defined in the "validator" attribute and should be a JS function that returns a function with the following signature: f( value, context): boolean. Very simple. The context is the option context object that you may pass to the collectValues method.

Update-as-you-type

The function getFieldAndValue(event) is useful for PWA-style update-as-you-type style. See example. Internally it uses the same logic, but it finds the input element that was just changed, so you can process further only it, instead of all the values. Pretty useful.

enabling/disabling fields

using the disable function, you can disable or enable input fields by name. You can do it to the whole sub-document by not providing field names. You can even control sub-fields (e.g. specific radio buttons) but that's partially supported right now.

example

See full example in a single web-page under the example directory. You can run it from the main project folder: npx http-server . and just open example folder from the browser.

...Or you can check out the Live Demo of it.

License

This library provided as-is, with absolutely no guarantee. Enjoy, support, etc, in short, it's ISC.

Support me

I'd be happy to receive a star