Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typeof-arguments

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typeof-arguments

Validate the types of arguments passed to the function.

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.9K
decreased by-26.47%
Maintainers
1
Weekly downloads
 
Created
Source

Description

typeof-arguments is a module that validates arguments' types passed to the enclosing function.

Installation

npm install typeof-arguments

var args = require('typeof-arguments');

Usage

args(arguments,types[,callback])
arguments [Object]
  • It should always indicate the enclosing function arguments object
types [Array] of [String] items.
  • It should contain the list of expected types for each (or chosen) enclosing function parameter.
  • The item possible values: 'null', 'undefined', or any value equal to contructor.name, eg. 'string', 'number', 'regexp', 'array', 'object', 'boolean'.
  • The upper and lower cases do not make any difference: 'String', 'string', 'StRiNg' checks if the argument is of type [String]
  • The item value can equal to: '' or 'any', then the argument can be of any type
  • The item can contain multiple allowed types, separated with |, eg: 'array|object', 'boolean|number|null|undefined', string|number
var args = require('typeof-arguments');

hello('hello', "world!");

function hello(paramA,paramB){
  args(arguments,['string','string']);
}
callback [Function] (optional)
  • if not passed, the TypeError with default message will be printed to the console, if the argument passed to the function is invalid.
  • The TypeError default message is eg.: Invalid argument. The [String] argument has been passed, while the [Number] one is expected.
  • if passed, the default error message will not be printed to the console and the user can decide what to do inside the callback function
  • the parameter actual [String] and expected [String] is passed to the callback function. The parameter actual indicates the actual type of the argument passed to the enclosing function, eg '[String]', when the expected parameter indicates the type(s) expected by the user, eg. '[Array]', '[Boolean|Number]'.
var args = require('typeof-arguments');

hello(10, "hello!");

function hello(paramA,paramB){
  args(arguments,['any','string|number'],(actual,expected)=>{
    console.log(new Error(`Not good! You passed ${actual}, when you should have passed ${expected}.`));
  });
}
Return value

The function args() returns true when all arguments passed to the enclosing function are of valid types. The function args() returns false when at least one of the arguments passed to the enclosing function is of invalid type.

var args = require('typeof-arguments');

hello("hello","world!");

function hello(paramA,paramB){
  var areValid = args(arguments,['string','string']);
  if(!areValid) return; //stop executing code if at least one argument is of invalid type
  return paramA + " " + paramB;
}

Samples

var args = require('typeof-arguments');

function hello(paramA,paramB,paramC){
  args(arguments,['number|string','any','null|array']);
}

hello("hello", "it's me!", null);
//no errors

hello(10, 20, [1,2,3]);
//no errors

hello(true,20,null);
//Invalid argument. The [Boolean] argument has been passed, while the [Number|String] one is expected.

hello({name:'Paul'},false,/test/);
//Invalid argument. The [Object] argument has been passed, while the [Number|String] one is expected.
//Invalid argument. The [RegExp] argument has been passed, while the [Null|Array] one is expected.

hello(10,20,null,30,40,50,60,70);
//no errors

hello(10);
//Invalid argument. The [undefined] argument has been passed, while the [Null|Array] one is expected.

Keywords

FAQs

Package last updated on 28 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc