typeof-arguments
Advanced tools
Comparing version 2.0.0 to 2.1.0
15
index.js
const ofType = require('of-type'); | ||
const cliColor = require('cli-color'); | ||
const error = cliColor.red; | ||
const warn = cliColor.bgYellow.black; | ||
module.exports = function(a,o,c){ | ||
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 errArguments = warn('typeof-arguments')+': '+error('Invalid module argument. The first argument must indicate [Object Arguments] object.'); | ||
const errTypes = warn('typeof-arguments')+': '+error('Invalid module argument. The second argument must be of type [Array].'); | ||
const errItems = warn('typeof-arguments')+': '+error('Invalid module argument. Each item of the second [Array] argument must be of type [String|RegExp].'); | ||
const isO = ofType(a,'arguments'); | ||
@@ -46,6 +48,7 @@ const isA = ofType(o,'array'); | ||
})(); | ||
var msg = `Invalid 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.`; | ||
if(clb){ | ||
c(act,exp); | ||
c(act,exp,msg); | ||
} else { | ||
var err = 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`); | ||
var err = new TypeError(error(msg)); | ||
throw err; | ||
@@ -52,0 +55,0 @@ } |
{ | ||
"name": "typeof-arguments", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Validate the types of arguments passed to the function.", | ||
@@ -20,4 +20,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"of-type": "^1.0.6" | ||
"of-type": "^1.0.6", | ||
"cli-color": "^1.2.0" | ||
} | ||
} |
# 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** | ||
* Any bugs found? Give me to know on dev.rafalko@gmail.com or on [GitHub](https://github.com/devrafalko/typeof-arguments) | ||
* 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)*. | ||
@@ -40,3 +40,3 @@ | ||
##### `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. | ||
* if **not passed**, the **TypeError** with **default message** will be **thrown** to the console, if the argument passed to the function is invalid. | ||
* The TypeError default message is eg.: | ||
@@ -46,5 +46,9 @@ * `Invalid argument [0]. The [String] argument has been passed, while the [Number] one 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 | ||
* if **passed**, the default TypeError **will not be thrown** to the console and the user can decide what to do inside the `callback` function. | ||
* Use callback function if you don't want to stop your code execution by default *(no callback)* **`throw`** statement! | ||
* 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 through the enclosing function, eg. `'[String]'`, when the `expected` parameter indicates the type(s) expected by the user, eg. `'[Array]'`, `'[Boolean|Number]'`, `/array|object/i`. | ||
* the parameter **`actual`** [String], **`expected`** [String] and **msg** [String] is passed through the callback function: | ||
* `actual` indicates the actual type of the argument passed through the enclosing function, eg. `'[String]'` | ||
* `expected` indicates the type(s) expected by the user, eg. `'[Array]'`, `'[Boolean|Number]'`, `/array|object/i` | ||
* `msg` is the default error [String] message, that you can use for example to throw an error in the callback function | ||
@@ -57,4 +61,5 @@ ```javascript | ||
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}.`)); | ||
args(arguments,['any','string|number'],(actual,expected,msg)=>{ | ||
console.error(msg); | ||
//throw new Error("Aborted! " + msg); | ||
}); | ||
@@ -61,0 +66,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12169
57
194
2
+ Addedcli-color@^1.2.0
+ Addedansi-regex@2.1.1(transitive)
+ Addedcli-color@1.4.0(transitive)
+ Addedd@1.0.2(transitive)
+ Addedes5-ext@0.10.64(transitive)
+ Addedes6-iterator@2.0.3(transitive)
+ Addedes6-symbol@3.1.4(transitive)
+ Addedes6-weak-map@2.0.3(transitive)
+ Addedesniff@2.0.1(transitive)
+ Addedevent-emitter@0.3.5(transitive)
+ Addedext@1.7.0(transitive)
+ Addedis-promise@2.2.2(transitive)
+ Addedlru-queue@0.1.0(transitive)
+ Addedmemoizee@0.4.17(transitive)
+ Addednext-tick@1.1.0(transitive)
+ Addedtimers-ext@0.1.8(transitive)
+ Addedtype@2.7.3(transitive)