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

required-parameters-js

v1.0.5

Published

Mandatory function parameters for JS, with declarative syntax

Downloads

23

Readme

required-parameters-js : Mandatory function parameters for JS

This tiny package allows you to use the required identifier (as a default parameter value) on those function parameters that you want to be mandatory. If any of those parameters are not provided, a Missing required parameter Error is thrown. s Compatible with Node & Browsers

Usage Example

  /* global required */
  
  import './required-parameters.js'; 
  // or in Node...
  // require('required-parameters');
  
  // declare a function with a required parameter
  function myFunc( myParam = required ){
    /* function code... */
  }

  myFunc();
  // Uncaught Error : Missing required parameter 
  // at myFunc (https://localhost/main.js/:10:5)

Distribution & installation

You can use any of the following alternatives:

- NPM : Install with npm :

$ npm install required-parameters-js -s

- GIT : Clone with git :

$ git clone https://github.com/colxi/required-parameters-js.git

-ZIP : Download the package in a ZIP file from :

GITHUB LATEST PACKAGE RELEASE PAGE

- CDN: Use the CDN to include the latest version of the library in your document head :

Warning: Not recommended in production enviroments :

<script src="http://colxi.info/required-parameters-js/required-parameters.js">

Importing

This package is shipped with support to Node CommonJS and ES6 Modules. Use the appropiate method accoordintg to your enviroment.

  // ES6 Module Import : 
  import './required-parameters.js'; 

  // CommonJS Node Import :
  require('required-parameters');

Code Linters

Because the required keyword is declared by the library in the global scope your linter will complain when detecting that required is used without previous explicit declaration in your specific namespace. To disable the warning set the require keyword in your linter config file or inlined in your code use :

/* global required */

How it works

- Short story short :

Because a line of code is worth a thousand words...

const required = ()=>{ throw new Error('Required Parameter!') };

function myFunc( myParamater = required() ){ /**/ }

Involve a getter for required and you have it... your own implementation of required parameters ;)

- Short story long :

Required-param performs its magic using the ES6 function default parameter values.

First the required identifier is declared by this library in the corresponding global object (window | self | global), to ensure it can be used anywhere (Browser main thread, Modules, Workers or Node).

When you call a function that uses required in any of its parameters, and the required parameter is missing, the javascript interpreter tries to assign the value of required to the argument.

Because the required variable value retrieval attemps, are handled internally by a getter, the getter function only needs to throw an Error.