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

ls-service-form2json

v1.0.0

Published

Litespeed.js service component that converts an HTML form element to a valid JSON object.

Downloads

8

Readme

ls-service-form2json

npm npm version Build Status Chat With Us

Litespeed.js service component that converts an HTML form element to a valid JSON object.

Installation

This package is wrapped as a Litespeed.js component. To use it, you need to init a new Litespeed.js project or use an exisiting Litespeed.js project. To learn more about Litespeed.js Javascript web framework got to the official repository.

Install with NPM:

npm install ls-service-form2json

Install with CDN:

<script src="https://cdn.jsdelivr.net/npm/ls-service-form2json"></script>

Getting Started

Keys & Values

The form2json service uses each form element name attribute as a key for the output JSON object and the element value as the JSON key value.

Arrays & Objects

The form2json service uses a FIELDSET tag to wrap a set of primitives as an object. When multiple form elements have the same name attribute the will be joined into an array. If you wish to force an array casting for single elements who have a unique name attribute, add the data-cast-to="array" attribute to the required form element.

Data Casting

The form2json service is trying to cast HTML form elements to their most suitable var types. For example, number or range input elements will be converted to into integers, while text or search input elements will be converted to strings. To force a specific type use the data-cast-to="array" on your HTML element. The data-cast-to attribute accepts these values: string, int, integer, bool, boolean, array, and JSON. The JSON type converts a resulting form object (FIELDSET tag) to a JSON string.

Checkboxes

Checkbox elements are automatically cast to an array list of checked values.

Example

This basic form:

<form id="unique">
    <input type="text" name="title" value="Hello World!" />
    <input type="text" name="firstname" value="Eldad" />
    <input type="text" name="lastname" value="Fux" />
</form>
let form = document.getElementById('unique');
let form2json = window.ls.container.get('form2json');

return form2json.toJson(form);

Will result to this JSON:

{
    "title": "Hello World!",
    "firstname": "Eldad",
    "lastname": "Fux",
}

This form:

<form id="unique">
    <input type="text" name="title" value="Coding Languages" />

    <fieldset name="language">
        <input type="text" name="name" value="PHP" />
        <input type="text" name="link" value="https://www.php.net/" />
    </fieldset>
    <fieldset name="language">
        <input type="text" name="name" value="NodeJS" />
        <input type="text" name="link" value="https://nodejs.org/en/" />
    </fieldset>
    <fieldset name="language">
        <input type="text" name="name" value="Ruby" />
        <input type="text" name="link" value="https://www.ruby-lang.org/en/" />
    </fieldset>
</form>
let form = document.getElementById('unique');
let form2json = window.ls.container.get('form2json');

return form2json.toJson(form);

Will result to this JSON including a list of objects:

{
    "title": "Coding Languages",
    "language": [
        { "name": "PHP", "link": "https://www.php.net/"},
        { "name": "NodeJS", "link": "https://nodejs.org/en/"},
        { "name": "Ruby", "link": "https://www.ruby-lang.org/en/"}
    ]
}

For more examples visit our jest test file

Contributing

All code contributions - including those of people having commit access - must go through a pull request and approved by a core developer before being merged. This is to ensure proper review of all the code.

Fork the project, create a feature branch, and send us a pull request.

For security issues, please email [email protected] instead of posting a public issue in GitHub.

Copyright and license

The MIT License (MIT) http://www.opensource.org/licenses/mit-license.php