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

@dubaua/merge-options

v2.0.0

Published

Helper for validate user options and merge with default configuration

Downloads

99

Readme

Parameters and Props Validator for Open Source Developers

This tool designed for validating user options against declarative configuration. To show how it is designed I introduce two characters. A developer of a library and a consumer of the library. The first uses merge-options internally and delegate it user options validation. The developer describes the options accepting by the library in declarative way. Each option either required or has default value, have a validator function and description of the validator. The developer can be sure that the library will get valid options passed through merge-options. Also, the developer can be sure the consumer will get verbose exceptions or warnings if they will pass invalid options or will forget to pass required options.

Reasoning

It saves the developer time on validation props and saves the consumer time on debugging.

How it Works

The function iterates over option configuration and looks for user option with same key. If the user option pass validation the function writes user value to a resulting object key. If the option isn't required, so the function writes default value to the resulting object key. If the user option fails validation or the user didn't pass required option the function throws an exception with a verbose message. If all options validation the function returns the resulting object.

Merge-options uses itself internally to validate its options :)

Installation

Using npm

npm i @dubaua/merge-options

or yarn

yarn add @dubaua/merge-options

or if you want to use package as UMD

<script src="https://unpkg.com/@dubaua/[email protected]/dist/merge-options.min.umd.js"></script>

Parameters and Return

The function accepts the only parameter — a configuration object described below. It returns an object with all keys in options. User value passing validation overrides corresponding initial value in defaults.

| option | type | default | description | | ------------ | ------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | optionConfig | Object.<string, Option> | | required. declarative option configuration | | userOptions | Object | {} | user options needs validation before merge | | preffix | string | '' | string before an error or warning message | | suffix | string | '' | string after an error or warning message | | strict | boolean | true | in strict mode if user value fails validation the function throws an exception. In not strict mode the function shows a warning message and fallback to default value |

Option

Each option configuration have necessary information to validation and composing message

| name | type | description | | ----------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------ | | required | boolean|function | a flag or function accepts userOptions | | default | any | default value for fallback if user option fail validation | | validator | function | function for validating user option. Accepts userValue as first argument and userOptions as second. Should return boolean value. | | description | string | human readable validator description. Uses to compose an error message and warning |

Usage

Import to Your Code

If you're using merge-options as UMD you don't have to import. It accessible as global function mergeOptions.

import mergeOptions from '@dubaua/merge-options';

Describe Default Options

Create an object with option configuration. Each option should have initial value, description and validator function.

const LIBRARY_OPTION_CONFIG = {
  pagerThreshold: {
    default: 0.5,
    validator: (x) => typeof x === 'number' && 0 <= x && x <= 1,
    description: 'a number between 0 and 1',
  },
};

class Library {
  constructor(userOptions) {
    this.options = mergeOptions({
      optionConfig: LIBRARY_OPTION_CONFIG,
      userOptions,
      preffix: '[Library]:',
      suffix: 'Check out documentation https://github.com/dubaua/merge-options',
      strict: false,
    });
  }
}

Now, everyone can pass options to your library.

// passing valid options to library
const libraryInstance = new Library({ pagerThreshold: 1 });
// options passing validation overrides defaults
libraryInstance.options.pagerThreshold; // 1

If invalid option passed user will see a warning in not strict mode and exceptions in strict.

// pass invalid options to your code
const anotherInstance = new Library({ pagerThreshold: 1.5 });
// [Library]: Expected pagerThreshold to be a number between 0 and 1,
// got number 1.5. Fallback to default value 0.5.
// Check out documentation https://github.com/dubaua/merge-options
anotherInstance.options.pagerThreshold; // 0.5

Validation Based on Another Option

In example below startPosition option validator relies on inputRange value.

const OPTION_CONFIG = {
  inputRange: {
    required: true,
    validator: (x) => Array.isArray(x) && x.length === 2 && x.every((y) => typeof y === 'number'),
    description: 'an array of two numbers',
  },
  startPosition: {
    required: (userOptions) => Object.prototype.hasOwnProperty.call(userOptions, 'inputRange'),
    default: 0,
    validator: (value, userOptions) =>
      Object.prototype.hasOwnProperty.call(userOptions, 'inputRange')
        ? userOptions.inputRange[0] <= value && value <= userOptions.inputRange[1]
        : true,
    description: 'an number within inputRange',
  },
};