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 - npm Package Compare versions

Comparing version 3.2.0 to 4.0.0

src/index.js

59

.eslintrc.json
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"windows"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"windows"
],
"quotes": [
"error",
"single",
{
"avoidEscape": true
}
],
"semi": [
"error",
"always"
]
}
}
{
"name": "typeof-arguments",
"version": "3.2.0",
"version": "4.0.0",
"description": "Validate the types of arguments passed to the function.",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "node tests/jasmine.conf.js"
"test": "node tests/jasmine.conf.js",
"build": "webpack --config webpack.config.js"
},

@@ -31,5 +32,11 @@ "keywords": [

"devDependencies": {
"babel-cli": "^6.26.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"jasmine": "^3.1.0",
"jasmine-spec-reporter": "^4.2.1"
"jasmine-spec-reporter": "^4.2.1",
"uglifyjs-webpack-plugin": "^1.2.7",
"webpack": "^4.16.1",
"webpack-cli": "^3.1.0"
}
}

@@ -8,14 +8,31 @@ # Description

# Installation
### Node
`npm install typeof-arguments`
```javascript
const argType = require('typeof-arguments');
const type = require('typeof-arguments');
```
### Browsers
Load the `typeof-arguments.min.js` file from the `src` folder into your `.html` file.
The module is accessible as `typeofArguments` in the global scope.
It is a `babel` converted and `webpack` bundled crossbrowser module version.
```html
<script src="./src/typeof-arguments.min.js"></script>
<script>
function test() {
typeofArguments(arguments, [String, Number]);
}
</script>
```
# Usage
### `argType(actual, expected[, callback])`
##### `actual` **[Object]**
### `type(actual, expected[, callback])`
### `actual` **[Object]**
* It should always indicate the enclosing function **`arguments`** object
##### `expected` **[Array]**
### `expected` **[Array]**
* It should contain the [Array] list of **expected types** for each subsequent argument passed through the enclosing function.

@@ -31,7 +48,7 @@ * The [Array] `expected` item's index coheres with the index of `actual` argument item passed through the enclosing function.

//and the age should be of [Number|String|null] type
argType(arguments, ['string', 'number|string|null']);
type(arguments, ['string', 'number|string|null']);
}
```
##### The `expected` Types
### The `expected` Types
There are three ways to check the type of the arguments:

@@ -42,3 +59,3 @@ * by **string expression** values

##### 1. [String] expressions
#### 1. [String] expressions
* Possible values: `'null'`, `'undefined'`, or any value equal to `constructor.name`, eg: `'string'`, `'number'`, `'regexp'`, `'array'`, `'object'`, `'boolean'`,`'buffer'`, etc.

@@ -52,7 +69,7 @@ * The [String] value is case insensitive: `'String'`, `'string'`, `'StRiNg'` checks if the argument is of type [String].

function test() {
argType(arguments, ['string', 'number|string|null']);
type(arguments, ['string', 'number|string|null']);
}
```
##### 2. [RegExp] expressions
#### 2. [RegExp] expressions
* Possible values: `/null/`, `/undefined/`, or any value matching the `constructor.name`, eg: `/String/`, `/Number/`, `/RegExp/`, `/Array/`, `/Object/`, `/Boolean/`,`/Buffer/`, `/Promise/`, etc.

@@ -69,7 +86,7 @@ * For the case insensitivity use `i` flag, eg: `/string/i`, `/regexp/i`, `/typeerror/i`

function test() {
argType(arguments, [/string/i, /number|string|null/i]);
type(arguments, [/string/i, /number|string|null/i]);
}
```
##### 3. [null|undefined|Function] expressions
#### 3. [null|undefined|Function] expressions
* Possible values: `null`, `undefined` or any **constructor** object, eg: `String`, `TypeError`, `Promise`, `Array`, etc.

@@ -82,14 +99,14 @@ * For **multiple** values use **array**, eg: `[String,Object,Array,null]`

function test() {
argType(arguments, [String, [Number, String, null]]);
type(arguments, [String, [Number, String, null]]);
}
```
##### Extra types:
### Extra types:
* The value can be: `'arguments'` or `/arguments/`. It returns `true` if the argument is defined as the `arguments` Object
* The value can be : 'instance' or /instance/. It returns `true` for the **instances** of user **classes** or **constructors**. It returns `false` for instances of built-in *(native)* constructors, *eg. for [], "hello world", {}*
* The value can be: `'truthy'` or `/truthy/`. It returns `true` if the argument has the value like: `"abc"`, `true`, `1`, `{}`, `[]`,`function(){}`, etc.
* The value can be : `'instance'` or `/instance/`. It returns `true` if the argument is the **instance** of user **class** or **constructor**. It returns `false` for instances of built-in *(native)* constructors, *eg. for `[]`, `"hello world"`, `{ }`*
* The value can be: `'truthy'` or `/truthy/`. It returns `true` if the argument has the value like: `"abc"`, `true`, `1`, `{ }`, `[]`,`function(){ }`, etc.
* The value can be: `'falsy'` or `/falsy/`. It returns `true` if the argument has the value like: `""`, `false`, `0`, `null`, `undefined`, `NaN`, etc.
* The value can be: `''` or `'any'` or `/any/` or `[]`, It returns `true` if the argument is of **any type**.
##### `callback` **[Function]** *(optional)*
### `callback` **[Function]** *(optional)*
* if **not passed**, the **TypeError** with **default message** will be **thrown** to the console, if the argument passed to the function is invalid.

@@ -115,3 +132,3 @@ * The TypeError default message is eg.:

```javascript
const argType = require('typeof-arguments');
const type = require('typeof-arguments');

@@ -121,6 +138,8 @@ hello('Paul', 26);

function hello(name, age) {
argType(arguments, [String, 'string|number'], (o) => {
type(arguments, [String, 'string|number'], (o) => {
console.error(o.message);
//console.error('Not good! Use ' + o.expected + ' instead of ' + o.actual + ' for argument ' + o.index);
//throw new Error("Aborted! " + o.message);
/*
console.error('Not good! Use ' + o.expected + ' instead of ' + o.actual + ' for argument ' + o.index);
throw new Error("Aborted! " + o.message);
*/
});

@@ -130,8 +149,8 @@ }

#### Return value
The function `argType()` returns `true` when all arguments passed through the enclosing function are of **valid** types.
The function `argType()` returns `false` when at least **one** of the arguments passed through the enclosing function is of **invalid** type.
### Return value
The function `type()` returns `true` when all arguments passed through the enclosing function are of **valid** types.
The function `type()` returns `false` when at least **one** of the arguments passed through the enclosing function is of **invalid** type.
```javascript
const argType = require('typeof-arguments');
const type = require('typeof-arguments');

@@ -141,3 +160,3 @@ hello('hello', 'world!');

function hello(paramA, paramB) {
const valid = argType(arguments, ['string', 'string'], () => { });
const valid = type(arguments, ['string', 'string'], () => { });
if (!valid) return; //stop executing code if at least one argument is of invalid type

@@ -159,6 +178,6 @@ //your code here...

```javascript
const argType = require('typeof-arguments');
const type = require('typeof-arguments');
function test(paramA, paramB, paramC) {
argType(arguments, ['number|string', 'any', 'null|array']);
type(arguments, ['number|string', 'any', 'null|array']);
}

@@ -188,6 +207,6 @@

```javascript
const argType = require('typeof-arguments');
const type = require('typeof-arguments');
function test(paramA, paramB) {
argType(arguments, ['truthy|string', /(regexp|falsy)/i]);
type(arguments, ['truthy|string', /(regexp|falsy)/i]);
}

@@ -217,6 +236,6 @@

```javascript
const argType = require('typeof-arguments');
const type = require('typeof-arguments');
function test(paramA, paramB) {
argType(arguments, [String, 'any', 'any', Number, /((syntax|type)error)|falsy/i]);
type(arguments, [String, 'any', 'any', Number, /((syntax|type)error)|falsy/i]);
}

@@ -245,6 +264,6 @@

```javascript
const argType = require('typeof-arguments');
const type = require('typeof-arguments');
function test(paramA, paramB) {
argType(arguments, ['instance', 'Name', 'object', 'falsy']);
type(arguments, ['instance', 'Name', 'object', 'falsy']);
}

@@ -251,0 +270,0 @@

/* global describe, expect, it */
const path = require('path');
const type = require(path.resolve('./index.js'));
const type = require(path.resolve('./src/index.js'));
const scenario = require('./scenarios-arguments.js');

@@ -5,0 +5,0 @@

/* global jasmine, describe, expect, it */
const path = require('path');
const type = require(path.resolve('./index.js'));
const type = require(path.resolve('./src/index.js'));
const scenario = require('./scenarios-invalid.js');

@@ -5,0 +5,0 @@

/* global jasmine, describe, expect, it */
const path = require('path');
const type = require(path.resolve('./index.js'));
const type = require(path.resolve('./src/index.js'));
const scenario = require('./scenarios-valid.js');

@@ -5,0 +5,0 @@

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