Comparing version 1.0.7 to 2.0.1
52
index.js
module.exports = function(val,type){ | ||
var isTypeString = typeof type==='string'; | ||
var isTypeRegExp = type&&type.constructor.name.toLowerCase()==='regexp'; | ||
if(!(isTypeString||isTypeRegExp)){ | ||
var error = new TypeError('\x1b[31mThe second argument must be of type [String|RegExp].\x1b[0m'); | ||
throw error; | ||
const tDefined = arguments.length>=2; | ||
const tString = typeof type==='string'; | ||
const tFun = typeof type==='function'; | ||
const tUnd = typeof type==='undefined'; | ||
const vUnd = typeof val==='undefined'; | ||
const tNull = type===null; | ||
const vNull = val===null; | ||
const tArr = !tNull && typeof type==='object' && type.constructor.name === 'Array'; | ||
const tReg = type&&type.constructor.name === 'RegExp'; | ||
const reg = /^\[object Arguments\]$/i; | ||
if(!tDefined) return false; | ||
if(tNull) return vNull; | ||
if(tUnd) return val===undefined; | ||
if(tFun){ | ||
if(vUnd||vNull) return false; | ||
return val.constructor.name === type.name; | ||
} | ||
var reg = /^\[object Arguments\]$/i; | ||
if(isTypeString){ | ||
if(tArr){ | ||
for(var i in type){ | ||
if(type[i]===null&&vNull) return true; | ||
if(typeof type[i]==='undefined'&&vUnd) return true; | ||
if(vNull||vUnd) continue; | ||
if(type[i]===null||typeof type[i]==='undefined') continue; | ||
if(val.constructor.name === type[i].name) return true; | ||
} | ||
return false; | ||
} | ||
if(tString){ | ||
var t = type.toLowerCase().split('|'); | ||
@@ -14,5 +37,5 @@ if((t.length===1&&t[0]==='')||(t.some((i)=>i==='any'))) return true; | ||
if(t.some((i)=>i==='falsy')&&!val) 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; | ||
if(vUnd&&t.some((i)=>i==='undefined')) return true; | ||
if(vNull&&t.some((i)=>i==='null')) return true; | ||
if(vNull||val===undefined) return false; | ||
if((reg).test(val.toString())&&val.constructor.name==='Object'&&t.some((i)=>i==='arguments')) return true; | ||
@@ -22,12 +45,13 @@ return t.some((i)=>i===val.constructor.name.toLowerCase()); | ||
if(isTypeRegExp){ | ||
if(tReg){ | ||
if(type.test('any')||type.test('')) return true; | ||
if(type.test('truthy')&&!!val) return true; | ||
if(type.test('falsy')&&!val) return true; | ||
if(type.test('undefined')&&typeof val==='undefined') return true; | ||
if(type.test('null')&&val===null) return true; | ||
if(val===null||val===undefined) return false; | ||
if(type.test('undefined')&&vUnd) return true; | ||
if(type.test('null')&&vNull) return true; | ||
if(vNull||val===undefined) return false; | ||
if(type.test('arguments')&&val.constructor.name==='Object'&&(reg).test(val.toString())) return true; | ||
return type.test(val.constructor.name); | ||
} | ||
return false; | ||
}; |
{ | ||
"name": "of-type", | ||
"version": "1.0.7", | ||
"version": "2.0.1", | ||
"description": "Check if the given value is of the particular type or types.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node tests/jasmine.conf.js" | ||
}, | ||
@@ -12,2 +12,5 @@ "keywords": [ | ||
"value", | ||
"instanceof", | ||
"type", | ||
"validate", | ||
"validation" | ||
@@ -24,3 +27,7 @@ ], | ||
}, | ||
"homepage": "https://github.com/devrafalko/of-type#readme" | ||
"homepage": "https://github.com/devrafalko/of-type#readme", | ||
"devDependencies": { | ||
"jasmine": "^2.7.0", | ||
"jasmine-spec-reporter": "^4.2.1" | ||
} | ||
} |
130
readme.md
@@ -6,4 +6,10 @@ # Description | ||
* Any bugs found? Give me to know on *dev.rafalko@gmail.com* or on [GitHub](https://github.com/devrafalko/of-type) | ||
* Also check out [**`typeof-arguments`**](https://www.npmjs.com/package/typeof-arguments) to validate value type of the arguments passed through functions. | ||
* Also check out [**`typeof-arguments`**](https://www.npmjs.com/package/typeof-arguments) to validate value types of the arguments passed through functions. | ||
* Also check out [**`typeof-properties`**](https://www.npmjs.com/package/typeof-properties) to validate value types of the properties of objects. | ||
##### v2.0 features: | ||
* the value can be now checked with the `function:constructor` objects, `null` or `undefined` *(or array of these items)* | ||
* the TypeError is not throwing when the arguments passed through module function are incorrect *(missing)*. It return `false` instead. | ||
* tests added | ||
# Installation | ||
@@ -22,28 +28,57 @@ `npm install of-type` | ||
### `ofType(val,types)` | ||
### `ofType(val,type)` | ||
##### `val` | ||
* the value of any type | ||
##### `types` **[String|RegExp]** | ||
* Checks if the **`val`** is of the **`types`** type. | ||
* 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 **`val`** 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` | ||
##### `type` **[String|RegExp|null|undefined|Function|Array]** | ||
* Checks if the **`val`** is of the **`type`** type. | ||
###### `type` [String] | ||
* Possible values: `'null'`, `'undefined'`, or any value equal to `constructor.name`, eg: `'string'`, `'number'`, `'regexp'`, `'array'`, `'object'`, `'boolean'`,`'buffer'`, etc. | ||
* The **`type`** [String] is case insensitive: `'String'`, `'string'`, `'StRiNg'` checks if the **`val`** is of type [String]. | ||
* The **`type`** [String] can contain multiple allowed types, separated with `|`. eg: `'array|object'` checks if the **`val`** is of type [Array] **`OR`** of type [Object]. | ||
###### `type` [RegExp] | ||
* Possible values: `/null/`, `/undefined/`, or any value matching the `constructor.name`, eg: `/String/`, `/Number/`, `/RegExp/`, `/Array/`, `/Object/`, `/Boolean/`,`/Buffer/`, `/Promise/`, etc. | ||
* For the case insensitivity use `i` flag, eg: `/string/i`, `/regexp/i`, `/typeerror/i` | ||
* For multiple values use regexp `(x|y)` expression, eg: `/String|Number/`, `/TypeError|Error/` | ||
###### `type` [null|undefined|Function|Array] | ||
* Possible values: `null`, `undefined` or any constructor, eg: `String`, `TypeError`, `Promise`, `Array`, etc. | ||
* For multiple values use array, eg: `[String,Object,Array,null]` | ||
##### 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 **`val`** values like: `"abc"`, `true`, `1`, `{}`, `[]`,`function(){}`, etc. | ||
* The **`types`** can contain the value: `'falsy'`. It returns `true` for the **`val`** values like: `""`, `false`, `0`, `null`, `undefined`, etc. | ||
* The **`types`** can contain the value: `''` or `'any'`, then it returns `true` for the **`val`** of **any type** | ||
* The **`type`** can contain the value: `'arguments'` or `/arguments/`. It returns `true` for the `arguments` Object | ||
* The **`type`** can contain the value: `'truthy'` or `/truthy/`. It returns `true` for the **`val`** values like: `"abc"`, `true`, `1`, `{}`, `[]`,`function(){}`, etc. | ||
* The **`type`** can contain the value: `'falsy'` or `/falsy/`. It returns `true` for the **`val`** values like: `""`, `false`, `0`, `null`, `undefined`, etc. | ||
* The **`type`** can contain the value: `''` or `'any'` or `/any/`, It returns `true` for the **`val`** values of **any type** | ||
#### Return value | ||
The function `ofType()` returns `true` if the **`val`** is of the defined **type** or is one of the defined **types**. | ||
The function `ofType()` returns `false` if the **`val`** is non the defined **type** or is any of the defined **types** | ||
The function `ofType()` returns `true` if the **`val`** is of the defined **type** or is one of the defined **types**. | ||
The function `ofType()` returns `false` if the **`val`** is not of the defined **type** or is none of the defined **types** | ||
# Tips | ||
> missing the `val` or `type` parameter will always return false *(without throwing errors)* | ||
`ofType()` //false | ||
`ofType(undefined,undefined)` //true | ||
`ofType(undefined)` //false | ||
# Tests | ||
``` | ||
> git clone https://github.com/devrafalko/of-type.git | ||
> cd of-type | ||
> npm install | ||
> npm test | ||
> npm test deep //displays error messages | ||
``` | ||
# Samples | ||
##### for `types` [String] | ||
```javascript | ||
var ofType = require('of-type'); | ||
ofType("abc",'string'); //true | ||
ofType(15,'number'); //true | ||
ofType("hello world",'string'); //true | ||
ofType(10,'number'); //true | ||
ofType([1,2,3],'array'); //true | ||
@@ -67,2 +102,6 @@ ofType([1,2,3],'object'); //false | ||
ofType(Date,'date'); //false | ||
ofType(Date,'function'); //true | ||
ofType(Array,'function'); //true | ||
ofType(10,'string|number'); //true | ||
@@ -72,12 +111,18 @@ ofType(10,'string|array'); //false | ||
ofType("",'falsy'); //true | ||
ofType(0,'falsy'); //true | ||
ofType(null,'falsy'); //true | ||
ofType(0,'falsy'); //true | ||
ofType(undefined,'falsy'); //true | ||
ofType("",'falsy'); //true | ||
ofType(10,'any|string'); //true | ||
ofType(10,''); //true | ||
ofType("hello world",''); //true | ||
ofType(false,'any'); //true | ||
ofType(true,'truthy'); //true | ||
ofType("a",'truthy'); //true | ||
ofType("hello world",'truthy'); //true | ||
ofType("",'truthy'); //false | ||
ofType(new String(""),'truthy') //true | ||
ofType(new String("").valueOf(),'truthy') //false | ||
ofType([1,2,3],'truthy') //true | ||
ofType([],'truthy') //true | ||
@@ -98,9 +143,11 @@ ofType(new Error(),'error'); //true | ||
/* | ||
[RegExp] samples: | ||
*/ | ||
``` | ||
ofType("abc",/string/); //false | ||
ofType("abc",/String/); //true | ||
ofType("abc",/string/i); //true | ||
##### for `types` [RegExp] | ||
```javascript | ||
var ofType = require('of-type'); | ||
ofType("hello",/string/); //false | ||
ofType("hello",/String/); //true | ||
ofType("hello",/string/i); //true | ||
ofType(10,/string|number/i); //true | ||
@@ -111,7 +158,8 @@ ofType(0,/Number/); //true | ||
ofType((function(){return arguments;})(),/arguments/); //true | ||
ofType((function(){return arguments;})(),/arg/); //true | ||
ofType((function(){return arguments;})(),/object/i); //true | ||
ofType(123,/any/i); //true | ||
ofType("123",/any/i); //true | ||
ofType([1,2,3],/any/i); //true | ||
ofType(123,/any/); //true | ||
ofType("123",/ANY/i); //true | ||
ofType([1,2,3],/any/); //true | ||
@@ -132,2 +180,28 @@ ofType(document.createElement('DIV'),/^html.*element$/i); //true | ||
ofType(new Error(),/(syntax|type)error/i); //false | ||
``` | ||
##### for `types` [Function:Constructor] and [Array<function:constructor>] | ||
```javascript | ||
var ofType = require('of-type'); | ||
ofType("hello",String); //true | ||
ofType("hello",Number); //false | ||
ofType("hello",[String,Number]); //true | ||
ofType(10,Number); //true | ||
ofType(false,Boolean); //true | ||
ofType([1,2,3],Object); //false | ||
ofType([1,2,3],Array); //true | ||
ofType(Array,Array); //false | ||
ofType(Array,Function); //true | ||
ofType(null,null); //true | ||
ofType(undefined,null); //false | ||
ofType(false,null); //false | ||
ofType({}.name,undefined); //true | ||
ofType(undefined,undefined); //true | ||
ofType(null,undefined); //false | ||
ofType(new RangeError('error'),Error); //false | ||
ofType(new RangeError('error'),[Error,TypeError,RangeError]); //true | ||
``` |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
254005
8
2408
0
200
2
2
1