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

option-html

v0.3.0

Published

Generate the options html string

Downloads

2

Readme

Build Status Coverage Status

option-html

Generate the <option>s html string.

Installation

npm install option-html

Usage

It is easy to use the option-html.

import optionHtml from 'option-html';
const html = optionHtml(settings); // The `settings` is a configurable object

Examples

An basic example:

const html = optionHtml({
  options: [
    'Java',
    'JavaScript',
    'PHP'
  ]
});
console.log(html);
// "<option value="Java">Java</option><option value="JavaScript">JavaScript</option><option value="PHP">PHP</option>" 

The settings.options can be a array of objects.

optionHtml({
  options: [
    {
      value: 'java',
      text: 'Java'
    },
    {
      value: 'javascript',
      text: 'JavaScript'
    },
    {
      value: 'php',
      text: 'PHP'
    }
  ]
});
// "<option value="java">Java</option><option value="javascript">JavaScript</option><option value="php">PHP</option>"

Also, the settings.options can be an array of [value, text] pairs.

optionHtml({
  options: [
    ['java', 'Java'],
    ['javascript', 'JavaScript'],
    ['php', 'PHP']
  ]
});
// <option value="java">Java</option><option value="javascript">JavaScript</option><option value="php">PHP</option>

The shortcut parameter:

optionHtml(options);
// Same as `optionHtml({ options: options })`

// For example
optionHtml(['Java', 'JavaScript', 'PHP']);

Specify the selected option(s):

optionHtml({
  selectedValue: ['php'],
  // Or
  // selectedText: ['PHP']
  options: [
    {
      value: 'java',
      text: 'Java'
    },
    {
      value: 'javascript',
      text: 'JavaScript'
    },
    {
      value: 'php',
      text: 'PHP'
    }
  ]
});

Specify the disabled option(s):

optionHtml({
  disabledValue: ['php'],
  // Or
  // disabledText: ['PHP']
  options: [
    {
      value: 'java',
      text: 'Java'
    },
    {
      value: 'javascript',
      text: 'JavaScript'
    },
    {
      value: 'php',
      text: 'PHP'
    }
  ]
});

You can customize each <option> html string with a function passed as the second parameter.

optionHtml({
  selectedValue: 'PHP',
  options: ['Java', 'JavaScript', 'PHP']
}, (option, index) => {
  let html = `<option value=${option.value.toLowerCase()}`;
  html += option.selected ? ' selected' : '';
  html += option.disabled ? ' disabled' : '';
  html += `>${option.text}</option>`;
  return html;
});

You can control the indention of each generated <option> string with an integer or a string passed as the third parameter.

optionHtml(['Java', 'JavaScript', 'PHP'], null, 2);
optionHtml(['Java', 'JavaScript', 'PHP'], null, '\t');

API

import optionHtml from 'option-html';

optionHtml(settings);
// See the below Settings section for the description about the "settings" parameter

optionHtml(options);
// Same as optionHtml({ options: options })

optionHtml(settings, replacer, space);
// With the second and third parameters
  • replacer

    Type: Function

    An "option data object" and "option index" will be passed as arguments. An string must be returned. The "option data object" is an plain object with four properties which is used to generate the option string:

    • value: An string;
    • text: An string;
    • selected: true or `false;
    • disabled: true or `false;

    The "option index" is a number.

  • space

    Type: Number|String

    Same as the third parameter of the JSON.stringify(), it is used to control spacing in the final string.

    • If this is a Number, it indicates the number of space characters to be used as the indention of the each <option>.
    • If this is a String, the string will be used as the indention of the each <option>.

    Notes, there isn't a limitation for the length of the indention.

Settings

  • options

    Type: Array

    The value of the options has three types:

    • An array contains the primitive values, such as [0, 1, 2] or ['foo', 'bar', 'baz'];
    • An array of objects which must have the value and text properties;
    • An array of [value, text] pairs.
  • selectedValue

    Type: String|Array|Function

    Specify the selected option(s). If it is an String or Array or Function, it will be converted to [String(selectedValue)].

  • selectedText

    Type: String|Array|Function

    As selectedValue, it specifies the selected option(s). If it is an String or Array or Function, it will be converted to [String(selectedText)].

  • disabledValue

    Type: String|Array|Function

    Specify the disabled option(s). If it is an String or Array or Function, it will be converted to [String(disabledValue)].

  • disabledText

    Type: String|Array|Function

    As disabledValue, it specifies the disabled option(s). If it is an String or Array or Function, it will be converted to [String(disabledText)].

License

MIT.