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

@krisell/options-handler

v1.1.0

Published

JavaScript function options handler

Downloads

8

Readme

JavaScript function options handler

Enables a combination of named parameters, default values and order-defined values, for easy-to-read function invocation.

OptionsHandler is an extremely small and simple library to allow a function to take both an options object (named parameters) as well as primitive arguments where their indices map to specific option properties.

This enables any method to be called in the preferred way. For instance an XHR-function might accept an object specifying url and method, but sometimes you might want to pass in the url as a string as the first argument.

Installation

You can pull in the code through npm, or just copy the contents and importing the code manually.

Through npm

npm install @krisell/options-handler

By importing code

Include OptionsHandler.js in your project however you like. If you don't use ES6-modules, leave out the "export default OptionsHandler". The library do use ES6-syntax however and transpilation may be needed.

How to use

  1. Use the rest-operator (...) to gather all arguments that are passed to your function in a specifications-array.

  2. Call OptionsHandler.build({}) with an object containing the following properties:

    • specs: The array of arguments to your function, built by the rest-operator.
    • defaultOptions: An object with any default options you would like to use.
    • namedOptions: An array which names the special arguments in the expected order.

Template

function doSomething (...specs) {
  const options = OptionsHandler.build({
    specs,
    defaultOptions: { key: value },
    namedOptions: ['argOneKey', 'argTwoKey', 'argThreeKey']
  })

  // Implement function with access to options.property
}

Now, options consists of a merge between the default options, any options passed in as an object and most importantly, if the first argument was not an object it's value will be set in the argOneKey-property of options, and similar for the rest.

You may pass several objects with option-properties, but you should always pass the primitive arguments first, and end with one or more ojects. You can not pass objects as a specific named options, since it will be treated as an options object.

An example

function xhr (...specs) {
  const options = OptionsHandler.build({
    specs,
    defaultOptions: { 
        method: 'get',
        responseType: 'json',
    },
    namedOptions: ['url', 'method', 'responseType']
  })

  // Implementation ...
}

xhr may now be called in any of the following ways, yielding the exact same result.

xhr({
    method: 'get',
    url: 'api/users',
    responseType: 'json',
})

xhr('api/users', 'get', 'json')

xhr('api/users')

xhr('api/users', 'get', {
    responseType: 'json'
})