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 1.0.2 to 1.0.3

66

index.js

@@ -0,24 +1,44 @@

const ofType = require('of-type');
module.exports = function(a,o,c){
const errA = '\x1b[31mInvalid arguments. The first argument must indicate function arguments [Object].\x1b[0m';
const errB = '\x1b[31mInvalid arguments. The second argument must be of type [Array].\x1b[0m';
const isO = isType(a,'object');
const isA = isType(o,'array');
if(!isO) console.log(new Error(errA));
if(!isA) console.log(new Error(errB));
const errArguments = '\x1b[31mInvalid arguments. The first argument must indicate [Object Arguments] object.\x1b[0m';
const errTypes = '\x1b[31mInvalid arguments. The second argument must be of type [Array].\x1b[0m';
const errItems = '\x1b[31mInvalid arguments. Each item of the second [Array] argument must be of type [String|RegExp].\x1b[0m';
const isO = ofType(a,'arguments');
const isA = ofType(o,'array');
if(!isO) console.log(new Error(errArguments));
if(!isA) console.log(new Error(errTypes));
if(!isO||!isA) return;
const clb = typeof c === 'function';
const clb = ofType(c,'function');
var r = true;
for(var x in o){
var t = typeof o[x]==='string'?o[x]:'';
var res = isType(a[x],t);
var t = o[x];
if(!ofType(o[x],'string|regexp')){
console.log(new Error(errItems));
return;
}
var isStr = ofType(t,'string');
var res = ofType(a[x],t);
if(!res){
r = false;
var act = typeName(a[x]);
var exp = t.split('|').map((i)=>{
return i==='null'||i==='undefined' ? i.toLowerCase():i[0].toUpperCase()+i.slice(1).toLowerCase();
}).join('|');
var act = ofType(a[x],'undefined') ? 'undefined':ofType(a[x],'null') ? 'null':ofType(a[x],'arguments') ? 'object Arguments':a[x].constructor.name;
var truthyFalsy = '';
var exp = isStr ?
t.split('|').map((i)=>{
const truthy = i==='truthy';
const falsy = i==='falsy';
const args = i==='arguments';
const nullOrUnd = i==='null'||i==='undefined';
truthyFalsy = truthy ? '<<falsy>> ':falsy ? '<<truthy>> ':truthyFalsy;
return nullOrUnd||truthy||falsy||args ? i.toLowerCase():i[0].toUpperCase()+i.slice(1).toLowerCase();
}).join('|'):
(()=>{
var r = t.toString();
truthyFalsy = r.match(/truthy/) ? '<<falsy>> ':r.match(/falsy/) ? '<<truthy>> ':'';
return r;
})();
if(clb){
c(`[${act}]`,`[${exp}]`);
c(act,exp);
} else {
console.log(new TypeError(`\x1b[31mInvalid argument. The [${act}] argument has been passed, while the [${exp}] one is expected.\x1b[0m`));
console.log(new TypeError(`\x1b[31mInvalid argument [${x}]. The [${act}] ${truthyFalsy}argument has been passed, while the ${isStr ? `[${exp}] one`:`argument of the type matching the regular expression: ${exp}`} is expected.\x1b[0m`));
}

@@ -28,18 +48,2 @@ }

return r;
};
function isType(val,type){
if(typeof type!=='string') throw new Error('\x1b[31mThe second argument must be of type [String].\x1b[0m');
var t = type.toLowerCase().split('|');
if((t.length===1&&t[0]==='')||(t.some((i)=>i==='any'))) return true;
if(typeof val==='undefined'&&t.some((i)=>i==='undefined')) return true;
if(val===null&&t.some((i)=>i==='null')) return true;
if(val===null||val===undefined) return false;
return t.some((i)=>i===val.constructor.name.toLowerCase());
};
function typeName(val){
if(typeof val==='undefined') return 'undefined';
if(val===null) return 'null';
return val.constructor.name;
};
{
"name": "typeof-arguments",
"version": "1.0.2",
"version": "1.0.3",
"description": "Validate the types of arguments passed to the function.",

@@ -18,3 +18,6 @@ "main": "index.js",

"author": "Paweł Rafałko",
"license": "MIT"
"license": "MIT",
"dependencies": {
"of-type": "^1.0.4"
}
}
# Description
`typeof-arguments` is a module that validates arguments' types passed to the enclosing function.
* Any bugs found? Give me to know on **dev.rafalko@gmail.com**
* Also check out [**`of-type`**](https://www.npmjs.com/package/of-type) package that checks whether the given value is of particular type *(`typeof-arguments` is based on `of-type` package)*.

@@ -13,14 +14,17 @@ # Installation

# Usage
#### `args(arguments,types[,callback])`
### `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`
##### `types` **[Array]** of **[String|RegExp]** items.
* It should contain the list of **expected types** for each *(or chosen)* enclosing function parameter. The **`types[2]`** specifies the expected type(s) of **`arguments[2]`**, etc.
* Possible values: `'null'`, `'undefined'`, or any value equal to `constructor.name`, eg. `'string'`, `'number'`, `'regexp'`, `'array'`, `'object'`, `'boolean'`,`'buffer'`, etc.
* The **`types`** [String] is case insensitive: `'String'`, `'string'`, `'StRiNg'` checks if the **`arguments`** item is of type [String]. For **`types`** [RegExp] case insensitivity use `i` flag, eg.: `/String/`, `/string/i`, `/sTrInG/i`
* The **`types`** [String] can contain multiple allowed types, separated with `|`, eg: `'array|object'`, `'boolean|number|null|undefined'`, `string|number`. For **`types`** [RegExp] multiple values use `(x|y)` expression, eg: `/(string|number)/i`
##### Extra types:
* The **`types`** can contain the value: `'arguments'`. It returns `true` for the `arguments` Object
* The **`types`** can contain the value: `'truthy'`. It returns `true` for the **`arguments`** item's values like: `"abc"`, `true`, `1`, `{}`, `[]`,`function(){}`, etc.
* The **`types`** can contain the value: `'falsy'`. It returns `true` for the **`arguments`** item's values like: `""`, `false`, `0`, `null`, `undefined`, etc.
* The **`types`** can contain the value: `''` or `'any'`, then it returns `true` for the **`arguments`** item of **any type**. Use it if you do not want to check the type of the particular **`arguments`** item, eg. `['string','any','object|array']`
```javascript

@@ -38,5 +42,9 @@ var args = require('typeof-arguments');

* 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]'`.
* The TypeError default message is eg.:
* `Invalid argument [0]. The [String] argument has been passed, while the [Number] one is expected.`
* `Invalid argument [2]. The [undefined] argument has been passed, while the argument of the type matching the regular expression: /array|object/i is expected.`
* `Invalid argument [1]. The [Number] <<truthy>> argument has been passed, while the [falsy|String] 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 `callback` function is executed **only** if at least one argument passed through the enclosing function is of invalid type.
* the parameter **`actual`** [String] and **`expected`** [String] is passed through 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]'`, `/array|object/i`.

@@ -55,7 +63,5 @@ ```javascript

#### 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.
The function `args()` returns `false` when at least **one** of the arguments passed through the enclosing function is of **invalid** type.

@@ -74,5 +80,3 @@ ```javascript

# Samples
```javascript

@@ -92,7 +96,7 @@ var args = require('typeof-arguments');

hello(true,20,null);
//Invalid argument. The [Boolean] argument has been passed, while the [Number|String] one is expected.
//Invalid argument [0]. 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.
//Invalid argument [0]. The [Object] argument has been passed, while the [Number|String] one is expected.
//Invalid argument [2]. The [RegExp] argument has been passed, while the [null|Array] one is expected.

@@ -103,3 +107,88 @@ hello(10,20,null,30,40,50,60,70);

hello(10);
//Invalid argument. The [undefined] argument has been passed, while the [Null|Array] one is expected.
//Invalid argument [2]. The [undefined] argument has been passed, while the [null|Array] one is expected.
```
### more samples
```javascript
var args = require('typeof-arguments');
function hello(paramA,paramB){
args(arguments,['truthy|string',/(regexp|falsy)/i]);
}
hello();
//Invalid argument [0]. The [undefined] <<falsy>> argument has been passed, while the [truthy|String] one is expected.
hello('','');
//Invalid argument [0]. The [String] <<falsy>> argument has been passed, while the [truthy|String] one is expected.
hello(1,0);
//no errors
hello(0,1);
//Invalid argument [0]. The [Number] <<falsy>> argument has been passed, while the [truthy|String] one is expected.
//Invalid argument [1]. The [Number] <<truthy>> argument has been passed, while the argument of the type matching the regular expression: /(regexp|falsy)/i is expected.
hello([1,2,3],/test/);
//no errors
hello('hello',null);
//no errors
```
### more samples
```javascript
var args = require('typeof-arguments');
function hello(paramA,paramB,paramC){
args(arguments,[/date|object|array/i,/^html.*element$/i,/^html(ul|li)element/i]);
}
var div = document.createElement('DIV');
var ul = document.createElement('UL');
var li = document.createElement('LI');
var a = document.createElement('A');
hello([1,2,3],null);
//Invalid argument [1]. The [null] argument has been passed, while the argument of the type matching the regular expression: /^html.*element$/i is expected.
hello([1,2,3],div,ol);
//no errors
hello([1,2,3],div,ul);
//no errors
hello(new Date(),a,div);
//Invalid argument [2]. The [HTMLDivElement] argument has been passed, while the argument of the type matching the regular expression: /^html[uo]listelement/i is expected.
```
### more samples
```javascript
var args = require('typeof-arguments');
function hello(paramA,paramB){
args(arguments,['arguments|falsy',/((syntax|type)error)|falsy/i]);
}
function returnArguments(){
return arguments;
}
hello(null,new TypeError());
//no errors
hello(false,new SyntaxError());
//no errors
hello(0,new Error());
//Invalid argument [1]. The [Error] argument has been passed, while the argument of the type matching the regular expression: /((syntax|type)error)|boolean/i is expected.
hello(1,false);
//Invalid argument [0]. The [Number] <<truthy>> argument has been passed, while the [arguments|falsy] one is expected.
hello(returnArguments,new TypeError());
//Invalid argument [0]. The [Function] <<truthy>> argument has been passed, while the [arguments|falsy] one is expected.
hello(returnArguments(),new TypeError());
//no errors
```
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