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

options-config

v3.1.2

Published

Validate and replace your default configuration options with ease

Downloads

9

Readme

options-config

Validate and replace your default configuration options with ease.

The validation scripts for configuration objects are harder to write than it seems. Luckily for you, options-config was released. This tiny library allows you to add a configuration object, of up to 2 levels, into your JavaScript library. This way, without you worrying about the validation mechanisms, your user will be able to replace any configuration option with any valid value.

Build status Dependencies status Version License

Installation

Open Terminal and install the package with this command:

npm install options-config --save

Then import options-config into nay file you are planning to use it:

import OptionsConfig from 'options-config';

Finally, do the one time setup:

const options = new OptionsConfig([defaultsObject]);

Defaults’ object

The defaults’ object is an object containing the default values and restrictions for every configuration option.

| Key | Type | Description | |:---------:|:---------------------:|-------------------------------------------------------------| | default | string, object | The result when there isn’t any valid value given. | | type | string, array | Only the values of this type will be valid. | | valid | string, array, object | The only values that will be valid. | | regex | regexp | A RegExp expression that has to match with the given value. | | range | object | The min, max and step parameters for numbers. |

Practical examples:

const defaultsObject = {
  x: {
    default: 10,
    type: ['number', 'boolean']
  },
  y: {
    default: 'foo',
    type: 'string',
    valid: ['foo', 'bar', 'hello', 'world']
  }
};
const defaultsObject = {
  x: true,
  y: {
    default: {
      y1: 'foo',
      y2: 'bar'
    },
    type: 'string',
    regex: /[A-z]{3}/
  }
};
const defaultsObject = {
  x: {
    x1: {
      default: 15,
      type: 'number',
      range: {
        min: 0,
        max: 100,
        step: 5
      }
    },
    x2: {
      default: false,
      type: 'boolean'
    }
  },
  y: {
    default: 200,
    valid: {
      number: 'all',
      string: ['e', 'π', 'pi']
    }
  }
};

Validate

options.validate(object, [defaultsObject])

Returns an object with all the configuration options, whether they are the default value or the one given by the user.

| Parameter | Required | Description | |:----------------:|:--------:|--------------------------------------------------------------------------------------------------------| | object | true | The configuration object given by the user. It can contain none, some or all of the available options. | | defaultsObject | false | The default values and restrictions. It’s not required if it has already been declared in the setup. |

Practical examples:

const userOptions = options.validate({
  x: false,
  y: 'hello'
});
const userOptions = options.validate({
  x: 15
}, {
  x: {
    default: 60,
    type: 'number'
  }
});

About

Contributing

If you have any trouble while installing or using options-config, or you want to suggest a change, I encourage you to open an issue or make a pull request. A short explanation is enough, and it will improve this project for you and other developers.

Tests

To run the tests, first install the dev dependencies and then run the test command:

npm install -d && npm test

License

© 2018, Nil Vila. Released under the MIT License.