Comparing version 2.2.0 to 3.0.0
{ | ||
"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" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
] | ||
} | ||
} |
@@ -1,32 +0,33 @@ | ||
const webpackConfig = require('./webpack.dev.js'); | ||
const path = require('path'); | ||
const merge = require('webpack-merge'); | ||
const webpackProd = require('./webpack.prod.js')[1]; | ||
const specMode = process.env.karma_spec_mode; | ||
module.exports = function(config) { | ||
module.exports = function (config) { | ||
config.set({ | ||
basePath: '', | ||
exclude:[ | ||
'tests/scenarios.js', | ||
], | ||
files: [ | ||
{pattern: 'tests/*.js',watched:true,served:true,included:true} | ||
{ pattern: 'tests/helpers/*.js', watched: false, served: true, included: true }, | ||
{ pattern: 'tests/*.js', watched: true, served: true, included: true } | ||
], | ||
autoWatch: false, | ||
singleRun:true, | ||
failOnEmptyTestSuite:false, | ||
singleRun: true, | ||
failOnEmptyTestSuite: false, | ||
logLevel: config.LOG_WARN, //config.LOG_DISABLE, config.LOG_ERROR, config.LOG_INFO, config.LOG_DEBUG | ||
frameworks: ['jasmine'], | ||
browsers: ['Chrome'], | ||
reporters: ['mocha'], | ||
reporters: ['mocha', 'kjhtml'], | ||
listenAddress: '0.0.0.0', | ||
hostname: 'localhost', | ||
port: 9876, | ||
retryLimit:0, | ||
browserDisconnectTimeout: 5000, | ||
browserNoActivityTimeout: 10000, | ||
retryLimit: 0, | ||
browserDisconnectTimeout: 50000, | ||
browserNoActivityTimeout: 50000, | ||
captureTimeout: 60000, | ||
client: { | ||
captureConsole:true, | ||
clearContext:false, | ||
captureConsole: true, | ||
clearContext: false, | ||
runInParent: false, | ||
useIframe:true, | ||
jasmine:{ | ||
useIframe: true, | ||
jasmine: { | ||
random: false, | ||
@@ -36,5 +37,11 @@ } | ||
preprocessors: { | ||
'./tests/helpers/*.js': ['webpack'], | ||
'./tests/*.js': ['webpack'] | ||
}, | ||
webpack: webpackConfig, | ||
webpack: merge(webpackProd, { | ||
output: { | ||
filename: '[name].js', | ||
path: path.resolve(__dirname, 'dist') | ||
} | ||
}), | ||
webpackMiddleware: { | ||
@@ -45,5 +52,6 @@ noInfo: true, | ||
mochaReporter: { | ||
output: specMode //noFailures, full, autowatch, minimal | ||
output: specMode, //noFailures, full, autowatch, minimal | ||
ignoreSkipped: true | ||
} | ||
}); | ||
}; |
{ | ||
"name": "of-type", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "Check if the given value is of the particular type or types.", | ||
"main": "prod/of-type.min.js", | ||
"main": "dist/of-type.node.js", | ||
"scripts": { | ||
"start": "webpack --config webpack.dev.js", | ||
"build": "webpack --config webpack.prod.js", | ||
"test": "cross-env karma_spec_mode=noFailures karma start", | ||
"test-err": "cross-env karma_spec_mode=full NODE_ENV=dist karma start" | ||
"test": "babel-node jasmine.conf.js", | ||
"test-web": "cross-env karma_spec_mode=noFailures karma start", | ||
"test-err": "cross-env karma_spec_mode=full karma start" | ||
}, | ||
@@ -31,17 +32,20 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-loader": "^7.1.4", | ||
"babel-preset-env": "^1.6.1", | ||
"cross-env": "^5.1.4", | ||
"jasmine-core": "^2.9.1", | ||
"karma": "^2.0.2", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-jasmine": "^1.1.1", | ||
"karma-mocha-reporter": "^2.2.5", | ||
"karma-webpack": "^3.0.0", | ||
"uglifyjs-webpack-plugin": "^1.2.5", | ||
"webpack": "^4.6.0", | ||
"webpack-cli": "^2.1.2", | ||
"webpack-merge": "^4.1.3" | ||
"@babel/cli": "^7.1.0", | ||
"@babel/core": "^7.0.1", | ||
"@babel/node": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"babel-loader": "^8.0.2", | ||
"babel-plugin-add-module-exports": "^1.0.0", | ||
"cross-env": "^5.2.0", | ||
"jasmine": "^3.3.1", | ||
"jasmine-spec-reporter": "^4.2.1", | ||
"join-array": "^1.1.1", | ||
"styles-loader": "^1.0.0", | ||
"uglifyjs-webpack-plugin": "^1.3.0", | ||
"webpack": "^4.19.1", | ||
"webpack-cli": "^3.1.1", | ||
"webpack-karma-jasmine": "^3.0.4", | ||
"webpack-merge": "^4.1.4", | ||
"webpack-node-externals": "^1.7.2" | ||
} | ||
} |
587
readme.md
# Description | ||
`of-type` is a very light module *(function)* that checks whether the given value is of particular type *(or types)*. | ||
`of-type` is a very light module that checks if the given value is of the expected type *(or types)*. | ||
* Any bugs found? Give me to know on [GitHub](https://github.com/devrafalko/of-type) | ||
* 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. | ||
Also see [`typeof-arguments`](https://www.npmjs.com/package/typeof-arguments) to validate types of the arguments passed through functions. | ||
Also see [`typeof-properties`](https://www.npmjs.com/package/typeof-properties) to validate types of the objects' properties. | ||
##### v2.0 features: | ||
* the value can be now checked with the `'instance'` or `/instance/` extra type, `function:constructor` objects, `null` or `undefined` *(or array of these items)* | ||
* the **empty** array passed as the second argument is equal to 'any', or /any/ types. The modules function returns `true` for the values of **any** type. | ||
* the TypeError is not throwing when the arguments passed through module function are incorrect *(missing)*. It return `false` instead. | ||
* tests added | ||
# Implementation | ||
# Installation | ||
#### with NodeJS | ||
`npm install of-type` | ||
```javascript | ||
var ofType = require('of-type'); | ||
const type = require('of-type'); | ||
ofType("hello world!",'string') //returns true | ||
ofType(true,'boolean|number|string') //returns true | ||
ofType(new Date(),/date/i) //returns true | ||
type('hello world!', 'string'); //true | ||
type(true, 'boolean|number|string'); //true | ||
type(new Date(), /date/i); //true | ||
``` | ||
#### with Browser | ||
#### Add `of-type.js` library to the HTML file. | ||
The library is located in `./dist/of-type.js` directory. | ||
It is a webpack&babel bundled cross-browser library version. | ||
The library is accessible as `ofType` variable in the global *(window)* scope. | ||
```html | ||
<head> | ||
<script src='of-type.js'></script> | ||
<script> | ||
ofType('hello world!', 'string'); //true | ||
</script> | ||
</head> | ||
``` | ||
# Tests | ||
``` | ||
> git clone https://github.com/devrafalko/of-type.git | ||
> cd of-type | ||
> npm install | ||
> npm test //run tests in node | ||
> npm test deep //run tests in node with errors shown | ||
> npm test-web //run tests with karma + chrome | ||
> npm test-err //run tests with karma + chrome with errors shown | ||
``` | ||
# Usage | ||
### `ofType(val,type)` | ||
##### `val` | ||
* the value of any type | ||
### `type(val, type)` | ||
##### `type` **[String|RegExp|null|undefined|Function|Array]** | ||
* Checks if the **`val`** is of the **`type`** type. | ||
#### `val` | ||
It is any value|object which type should be checked. | ||
###### `type` [String] | ||
#### `type` **[String|RegExp|null|undefined|Function|Array]** | ||
The `val` value|object is expected to be of `type` type. There are many ways to check the `val` type. Choose the most convenient one: | ||
`[String]` | ||
* Possible values: | ||
* `'null'`, `'undefined'` | ||
* any value that equals to `val`.`constructor.name`, eg: | ||
`'string'`, `'number'`, `'regexp'`, `'array'`, `'object'`, `'boolean'`,`'buffer'`, etc. | ||
* The [String] `type` is case insensitive: | ||
* `'String'`, `'string'`, `'StRiNg'` checks if the `val` is of `[String]` type | ||
* `'RegExp'`, `'REGEXP'`, `'regexp'` checks if the `val` is of `[RegExp]` type | ||
* The [String] `type` can contain multiple types, separated with `|`: | ||
* `'array|object'` checks if the `val` is of `[Array]` **`OR`** `[Object]` type | ||
* `'undefined|null'` checks if the `val` is of `undefined` **`OR`** `null` type | ||
* See the [samples](#string-type) | ||
* 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]. | ||
`[RegExp]` | ||
* Possible values: | ||
* `/null/`, `/undefined/` | ||
* any value matching the `val`.`constructor.name`, eg: `/String/`, `/Number/`, `/RegExp/`, `/Array/`, `/Object/`, `/Boolean/`,`/Buffer/`, `/Promise/`, etc. | ||
* Use all regular expression's features to match the type in a desired way: | ||
* `/Str/`, `/Err/`, `/Reg/`, `/B/` | ||
* `/.+Error$/`, `/^RegExp$/`, | ||
* `/^[A-Z][a-z]+$/` | ||
* For the case insensitivity use `i` flag: | ||
* `/string/i`, `/regexp/i`, `/TYPEERROR/i` | ||
* For multiple values use regexp `(x|y)` expression: | ||
* `/String|Number/`, `/TypeError|Error/`, `/(obj|str)/i` | ||
* See the [samples](#regexp-type) | ||
###### `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/` | ||
`[Function|Array|null|undefined]` | ||
* Possible values: | ||
* `null`, `undefined` | ||
* any `[Function]` constructor, eg: `String`, `TypeError`, `Promise`, `Array`, etc. | ||
* For multiple values use array: | ||
* `[String, Object, Array, null]` | ||
* `[null, undefined, Boolean]` | ||
* See the [samples](#constructor-null-and-undefined) | ||
###### `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]` | ||
> When you use **bundlers** or **minifiers**, use `[String|RegExp]` `type` **wisely** as bundlers may change the names of functions|constructors|classes in the output file and eg. `type(myInstance, 'MyClass')` that returns `true` before compilation, may return `false` after compilation, if the bundler minifies the `'MyClass'` constructor name. | ||
> When you use **bundlers** or **minifiers**, use [String|RegExp] `type` **wisely** as bundlers may change the names of functions/constructors/classes in the output file and `type(myInstance,"MyClass")` that returns `true` before compilation, may return `false` after compilation, if the bundler minifies the `MyClass` constructor name. | ||
### Extra types: | ||
##### Extra types: | ||
* The **`type`** can contain the value: `'arguments'` or `/arguments/`. It returns `true` for the `arguments` Object | ||
* The **`type`** can contain the value : `'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 **`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`, `NaN`, etc. | ||
* The **`type`** can contain the value: `''` or `[]` or `'any'` or `/any/`, It returns `true` for the **`val`** values of **any type** | ||
`[String] 'arguments'` | `[RegExp] /arguments/` | ||
#### 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 not of the defined **type** or is none of the defined **types** | ||
* The `type` `'arguments'` or `/arguments/` returns `true` for the function's `arguments` object | ||
* See the [samples](#arguments-type) | ||
`[String] 'instance'` | `[RegExp] /instance/` | ||
* The `type` `'instance'` or `/instance/` returns `true` for the instance of the user's class|constructor | ||
* `type(MyInstance, 'instance'); //true` | ||
* It returns `false` for instances of built-in *(native)* constructors | ||
* `[]`, `'hello world'`, `{}` | ||
* It returns `false` for instances that are the `global`|`window`'s properties | ||
* See the [samples](#instance-type) | ||
`[String] 'objectable'` | `[RegExp] /objectable/` | ||
* The `type` `'objectable'` or `/objectable/` returns `true` for the objects that are the instances of `Object` constructor | ||
* `{}`, `[]`, `new String('hello world')`, `new Boolean(1)` | ||
* It returns `false` for the primitive values and simple values | ||
* `'hello world'`, `true`, `10`, `null`, `undefined` | ||
* See the [samples](#objectable-type) | ||
`[String] 'truthy'` | `[RegExp] /truthy/` | ||
* The `type` `'truthy'` or `/truthy/` returns `true` for the values like: | ||
* `'abc'`, `true`, `1`, `-1`, `{}`, `[]`, `function(){}` | ||
* See the [samples](#truthy-type) | ||
`[String] 'falsy'` | `[RegExp] /falsy/` | ||
* The `type` `'falsy'` or `/falsy/` returns `true` for the values like: | ||
* `''`, `false`, `0`, `null`, `undefined`, `NaN` | ||
* See the [samples](#falsy-type) | ||
`[String] 'any'` | `[RegExp] /any/` | `[Array] []` | `[String] ""` | ||
* The `type` `'any'` or `/any/` or empty array `[]` or empty string `""` returns `true` for the values of any type | ||
* See the [samples](#any-type) | ||
### Return value | ||
The function `type()` returns `true` if the `val` argument is of expected `type`. | ||
The function `type()` returns `false` if the `val` argument is not of expected `type`. | ||
# Tips | ||
> missing the `val` or `type` parameter will always return false *(without throwing errors)* | ||
`ofType()` //false | ||
`ofType(undefined,undefined)` //true | ||
`ofType(undefined)` //false | ||
> Missing the `val` or `type` arguments will always return `false` *(without throwing error)*. | ||
```javascript | ||
type(); //false | ||
type(undefined, undefined); //true | ||
type(undefined); //false | ||
``` | ||
# Samples | ||
# Tests | ||
### `[String]` `type` | ||
```javascript | ||
import type from `of-type`; | ||
type('hello world', 'String'); //true | ||
type(10, 'Number'); //true | ||
type(null, 'null'); //true | ||
type(undefined, 'undefined'); //true | ||
type([1,2,3], 'Array'); //true | ||
type([1,2,3], 'Object'); //false | ||
type(true, 'Boolean'); //true | ||
type(type, 'function'); //true | ||
type(/hello/, 'RegExp'); //true | ||
type({ framework: 'React' }, 'Object'); //true | ||
type('hello world', 'string'); //true | ||
type('hello world', 'STRING'); //true | ||
type('hello world', 'str'); //false | ||
type(true, 'BOOLEAN'); //true | ||
type(false, 'BoOlEaN'); //true | ||
type(false, 'Bool'); //false | ||
type(null, 'NULL'); //true | ||
type(new Date(), 'DATE'); //true | ||
type(new Array(1,2,3), 'array'); //true | ||
type(new Buffer(0), 'buffer'); //true | ||
type(new String('hello world'), 'string'); //true | ||
type(()=>{}, 'function'); //true | ||
type((()=>'hello world')(), 'string'); //true | ||
type(Date, 'date'); //false | ||
type(Date, 'function'); //true | ||
type(Array, 'Function'); //true | ||
type(new Error(), 'error'); //true | ||
type(new TypeError(), 'typeerror'); //true | ||
type(new SyntaxError(), 'syntaxerror'); //true | ||
type(new SyntaxError(), 'error'); //false | ||
type(document.createElement('DIV'), 'htmldivelement'); //true | ||
type(document.createElement('DIV'), 'element'); //false | ||
type(document.createElement('LI'), 'HtmlLiElement'); //true | ||
type((function(){ return arguments; })(), 'object'); //true; constructor.name === "Object" | ||
class Name{}; | ||
type(new Name(), 'Name'); //true | ||
type(new Name(), 'name'); //true | ||
type(new Name(), 'object'); //false; constructor.name === 'Name' | ||
type(10, 'string|number'); //true | ||
type(10, 'string|array'); //false | ||
type(null, 'undefined|null'); //true | ||
``` | ||
> git clone https://github.com/devrafalko/of-type.git | ||
> cd of-type | ||
> npm install | ||
> npm test | ||
> npm run test-err //additionally displays error descriptions (when occur) | ||
### `[RegExp]` `type` | ||
```javascript | ||
import type from `of-type`; | ||
type('hello world', /String/); //true | ||
type(10, /Number/); //true | ||
type(null, /null/); //true | ||
type(undefined, /undefined/); //true | ||
type([1,2,3], /Array/); //true | ||
type([1,2,3], /Object/); //false | ||
type(true, /Boolean/); //true | ||
type(type, /Function/); //true | ||
type(/hello/, /RegExp/); //true | ||
type({ framework: 'React' }, /Object/); //true | ||
type('hello world', /string/); //false | ||
type('hello world', /STRING/); //false | ||
type('hello world', /string/i); //true | ||
type('hello world', /STRING/i); //true | ||
type('hello world', /Str/); //true | ||
type('hello world', /^str/i); //true | ||
type(true, /BOOLEAN/); //false | ||
type(false, /BoOlEaN/); //false | ||
type(true, /BOOLEAN/i); //true | ||
type(false, /BoOlEaN/i); //true | ||
type(false, /Bool/); //true | ||
type(false, /bool/i); //true | ||
type(null, /NULL/); //false | ||
type(null, /NULL/i); //true | ||
type({}, /^[A-Z][a-z]+$/); //true | ||
type(true, /^[A-Z][a-z]+$/); //true | ||
type(null, /^[A-Z][a-z]+$/); //false | ||
type(undefined, /^[A-Z][a-z]+$/); //false | ||
type((function(){ return arguments; })(), /object/i); //true; constructor.name === "Object" | ||
type(document.createElement('DIV'), /^html.*element$/i); //true | ||
type(document.createElement('DIV'), /^[a-z]+div[a-z]+$/i); //true | ||
type(document.createElement('A'), /anchor/i); //true | ||
type(document.createElement('UL'), /html[uo]listelement/i); //true | ||
type(document.createElement('OL'), /html[uo]listelement/i); //true | ||
type(10, /string|number/i); //true | ||
type(undefined, /und|null/i); //true | ||
type(new SyntaxError(), /(syntax|type)error/i); //true | ||
type(new TypeError(), /(syntax|type)error/i); //true | ||
type(new Error(), /(syntax|type)?error/i); //true | ||
class Name(){}; | ||
type(new Name(), /Name/); //true | ||
type(new Name(), /name/); //false | ||
type(new Name(), /name/i); //true | ||
type(new Name(), /Object/); //false; constructor.name === 'Name' | ||
``` | ||
# Samples | ||
##### for `types` [String] | ||
### `constructor`, `null` and `undefined` | ||
```javascript | ||
var ofType = require('of-type'); | ||
import type from `of-type`; | ||
ofType("hello world",'string'); //true | ||
ofType(10,'number'); //true | ||
ofType([1,2,3],'array'); //true | ||
ofType([1,2,3],'object'); //false | ||
ofType(true,'boolean'); //true | ||
ofType(/hello/,'regexp'); //true | ||
ofType({name:"Paul"},'object'); //true | ||
ofType({name:"Paul"},'instance'); //false | ||
ofType({name:"Paul"},'instance'); //false | ||
ofType([1,2,3],'function'); //true | ||
type('hello world', String); //true | ||
type(10, Number); //true | ||
type(null, null); //true | ||
type(undefined, undefined); //true | ||
type(null, undefined); //false | ||
type({}.name, undefined); //true | ||
type([1,2,3], Array); //true | ||
type([1,2,3], Object); //false | ||
type(true, Boolean); //true | ||
type(type, Function); //true | ||
type(/hello/, RegExp); //true | ||
type({ framework: 'React' }, Object); //true | ||
type((function(){ return arguments; })(), Object); //true; constructor.name === "Object" | ||
ofType((function(){return arguments;})(),'arguments'); //true | ||
ofType((function(){return arguments;})(),'object'); //true, MIND that the Object is the constructor | ||
ofType(()=>{},'FuNcTiOn'); //true | ||
ofType(ofType,'function'); //true | ||
type(new Date(), Date); //true | ||
type(new Array(1,2,3), Array); //true | ||
type(new Buffer(0), Buffer); //true | ||
type(new String('hello world'), String); //true | ||
ofType(new Date(),'DATE'); //true | ||
ofType(new Array(1,2,3),'array'); //true | ||
ofType(new Buffer(0),'buffer'); //true | ||
ofType(new String("abc"),'string'); //true | ||
ofType((()=>"abc")(),'string'); //true | ||
type(()=>{}, Function); //true | ||
type((()=>'hello world')(), String); //true | ||
ofType(Date,'date'); //false | ||
ofType(Date,'function'); //true | ||
ofType(Array,'function'); //true | ||
ofType(Array,'instance'); //false | ||
type(Date, Date); //false | ||
type(Date, Function); //true | ||
type(Array, Function); //true | ||
ofType(10,'string|number'); //true | ||
ofType(10,'string|array'); //false | ||
ofType(null,'undefined|null'); //true | ||
type(new Error(), Error); //true | ||
type(new TypeError(), TypeError); //true | ||
type(new SyntaxError(), SyntaxError); //true | ||
type(new SyntaxError(), Error); //false | ||
type(new RangeError(), [Error, TypeError, RangeError]); //true | ||
ofType("",'falsy'); //true | ||
ofType(0,'falsy'); //true | ||
ofType(null,'falsy'); //true | ||
ofType(undefined,'falsy'); //true | ||
class Name{}; | ||
type(new Name(), Name); //true | ||
type(new Name(), Object); //false; constructor.name === 'Name' | ||
ofType(10,'any|string'); //true | ||
ofType("hello world",''); //true | ||
ofType(false,'any'); //true | ||
type(10, [String, Number]); //true | ||
type(10, [String, Array]); //false | ||
type(null, [undefined, null]); //true | ||
``` | ||
ofType(true,'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 | ||
### `arguments` type | ||
```javascript | ||
import type from `of-type`; | ||
ofType(new Error(),'error'); //true | ||
ofType(new TypeError(),'typeerror'); //true | ||
ofType(new SyntaxError(),'syntaxerror'); //true | ||
ofType(new SyntaxError(),'error'); //false | ||
function hello(){ | ||
return arguments; | ||
} | ||
ofType(document.createElement('DIV'),'htmldivelement'); //true | ||
ofType(document.createElement('DIV'),'element'); //false | ||
ofType(document.createElement('LI'),'HtmlLiElement'); //true | ||
type(hello(), 'arguments'); //true | ||
type(hello(), 'ARGUMENTS'); //true | ||
type(hello(), 'arg'); //false | ||
type(hello(), /arguments/); //true | ||
type(hello(), /ARGUMENTS/); //false | ||
type(hello(), /ARGUMENTS/i); //true | ||
type(hello(), /arg/); //false | ||
function Name(){}; | ||
ofType(new Name(),'Name'); //true | ||
ofType(new Name(),'name'); //true | ||
ofType(new Name(),'object'); //false, MIND that this is not the instance of Object constructor | ||
ofType(new Name(),'instance') //true | ||
type(hello(), 'arguments|object|instance'); //true | ||
type(hello(), /arguments|undefined/); //true | ||
``` | ||
### `instance` type | ||
```javascript | ||
import type from `of-type`; | ||
class Name{ } | ||
type(new Name(), 'instance'); //true | ||
type(new Name(), 'INSTANCE'); //true | ||
type(new Name(), 'inst'); //false | ||
type(new Name(), /instance/); //true | ||
type(new Name(), /INSTANCE/); //false | ||
type(new Name(), /INSTANCE/i); //true | ||
type(new Name(), /inst/); //false | ||
type({}, 'instance'); //false | ||
type([], 'instance'); //false | ||
type(Array, 'instance'); //false | ||
type(new Error(), /instance/); //false | ||
type('hello world', /instance/) //false | ||
global.Framework = class Framework{ }; | ||
window.Cars = class Cars{ }; | ||
type(new Framework(), 'instance'); //false | ||
type(new Framework(), /instance/); //false | ||
type(new Cars(), 'instance'); //false | ||
type(new Cars(), /instance/); //false | ||
type({}, 'instance|object'); //true | ||
type(new String(), /instance|objectable/); //true | ||
``` | ||
##### for `types` [RegExp] | ||
### `objectable` type | ||
```javascript | ||
var ofType = require('of-type'); | ||
import type from `of-type`; | ||
type('hello world', 'objectable'); //false | ||
type(10, 'objectable'); //false | ||
type(null, 'objectable'); //false | ||
type(undefined, 'objectable'); //false | ||
type(true, 'objectable'); //false | ||
type({}, 'objectable'); //true | ||
type({}.name, 'objectable'); //false | ||
type([1,2,3], 'objectable'); //true | ||
type(/hello/, 'objectable'); //true | ||
type(type, 'objectable'); //true | ||
type((function(){ return arguments; })(), 'objectable'); //true; (arguments instanceof Object) === true | ||
ofType("hello",/string/); //false | ||
ofType("hello",/String/); //true | ||
ofType("hello",/string/i); //true | ||
ofType(10,/string|number/i); //true | ||
ofType(0,/Number/); //true | ||
ofType(!0,/Boolean/); //true | ||
class Name{} | ||
type(new Name(), 'objectable'); //true | ||
type(new String('hello world'), 'objectable'); //true | ||
type(new Number(10), 'objectable'); //true | ||
type(new Error(), 'objectable'); //true | ||
ofType((function(){return arguments;})(),/arguments/); //true | ||
ofType((function(){return arguments;})(),/arg/); //true | ||
ofType((function(){return arguments;})(),/object/i); //true | ||
type({}, 'objectable'); //true | ||
type({}, 'OBJECTABLE'); //true | ||
type({}, 'obj'); //false | ||
type({}, /objectable/); //true | ||
type({}, /OBJECTABLE/); //false | ||
type({}, /OBJECTABLE/i); //true | ||
type({}, /objecta/); //false; as it's unrecognizable custom type | ||
type({}, /obj/i); //true; as it still matches constructor.name === 'Object' | ||
ofType(123,/any/); //true | ||
ofType("123",/ANY/i); //true | ||
ofType([1,2,3],/any/); //true | ||
type(0, 'objectable|falsy'); //true | ||
type('hello world', /objectable|string/i); //true | ||
type({}, /object(able)?/i); //true | ||
type([], /object(able)?/i); //true | ||
``` | ||
ofType(document.createElement('DIV'),/^html.*element$/i); //true | ||
ofType(document.createElement('DIV'),/^[a-z]+div[a-z]+$/i); //true | ||
ofType(document.createElement('A'),/anchor/i); //true | ||
ofType(document.createElement('UL'),/html[uo]listelement/i); //true | ||
ofType(document.createElement('OL'),/html[uo]listelement/i); //true | ||
### `truthy` type | ||
```javascript | ||
import type from `of-type`; | ||
ofType(null,/falsy/); //true | ||
ofType(0,/falsy/); //true | ||
ofType([1,2,3],/truthy/); //true | ||
ofType("",/truthy/); //false | ||
type('hello world', 'truthy'); //true | ||
type('', 'truthy'); //false | ||
type(new String(''), 'truthy'); //true | ||
type(new String('').valueOf(), 'truthy'); //false | ||
type(10, 'truthy'); //true | ||
type(0, 'truthy'); //false | ||
type(null, 'truthy'); //false | ||
type(undefined, 'truthy'); //false | ||
type([1,2,3], 'truthy'); //true | ||
type([], 'truthy'); //true | ||
type(true, 'truthy'); //true | ||
type(false, 'truthy'); //false | ||
type(type, 'truthy'); //true | ||
type(/hello/, 'truthy'); //true | ||
type({ framework: 'React' }, 'truthy'); //true | ||
type({}.name, 'truthy'); //false | ||
ofType(new SyntaxError(),/(syntax|type)error/i); //true | ||
ofType(new TypeError(),/(syntax|type)error/i); //true | ||
ofType(new Error(),/(syntax|type)error/i); //false | ||
type(true, 'truthy'); //true | ||
type(true, 'TRUTHY'); //true | ||
type(true, 'tru'); //false | ||
type(true, /truthy/); //true | ||
type(true, /TRUTHY/); //false | ||
type(true, /TRUTHY/i); //true | ||
type(true, /tru/); //false; as it's unrecognizable custom type | ||
function Name(){}; | ||
ofType(new Name(),/Name/); //true | ||
ofType(new Name(),/name/); //false | ||
ofType(new Name(),/name/i); //true | ||
ofType(new Name(),/Object/); //false, MIND that this is not the instance of Object constructor | ||
ofType(new Name(),/instance/) //true | ||
type(undefined, 'truthy|null'); //false | ||
type(0, 'truthy|number'); //true | ||
type(false, /truthy|Boolean/); //true | ||
``` | ||
##### for `types` [Function|null|undefined] and [Array\<Function|null|undefined\>] | ||
### `falsy` type | ||
```javascript | ||
var ofType = require('of-type'); | ||
import type from `of-type`; | ||
type('hello world', 'falsy'); //false | ||
type('', 'falsy'); //true | ||
type(new String(''), 'falsy'); //false | ||
type(new String('').valueOf(), 'falsy'); //true | ||
type(10, 'falsy'); //false | ||
type(0, 'falsy'); //true | ||
type(null, 'falsy'); //true | ||
type(undefined, 'falsy'); //true | ||
type([1,2,3], 'falsy'); //false | ||
type([], 'falsy'); //false | ||
type(true, 'falsy'); //false | ||
type(false, 'falsy'); //true | ||
type(type, 'falsy'); //false | ||
type(/hello/, 'falsy'); //false | ||
type({ framework: 'React' }, 'falsy'); //false | ||
type({}.name, 'falsy'); //true | ||
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 | ||
type(false, 'falsy'); //true | ||
type(false, 'FALSY'); //true | ||
type(false, 'fal'); //false | ||
type(false, /falsy/); //true | ||
type(false, /FALSY/); //false | ||
type(false, /FALSY/i); //true | ||
type(false, /fal/); //false; as it's unrecognizable custom type | ||
ofType(null,[]); //true | ||
ofType("hello world",[]); //true | ||
ofType(Number,[]); //true | ||
ofType(0,[]); //true | ||
ofType(new Date(),[]); //true | ||
type([], 'falsy|objectable'); //true | ||
type(10, 'falsy|number'); //true | ||
type(true, /falsy|Boolean/); //true | ||
``` | ||
ofType(null,null); //true | ||
ofType(undefined,null); //false | ||
ofType(false,null); //false | ||
### `any` type | ||
```javascript | ||
import type from `of-type`; | ||
ofType({}.name,undefined); //true | ||
ofType(undefined,undefined); //true | ||
ofType(null,undefined); //false | ||
type('hello world', 'any'); //true | ||
type('', 'any'); //true | ||
type(new String(''), 'any'); //true | ||
type(10, 'any'); //true | ||
type(0, /any/); //true | ||
type(null, /any/); //true | ||
type(undefined, /any/); //true | ||
type([1,2,3], /any/); //true | ||
type([], []); //true | ||
type(true, []); //true | ||
type(false, []); //true | ||
type(type, []); //true | ||
type(/hello/, ''); //true | ||
type({ framework: 'React' }, ''); //true | ||
type({}.name, ''); //true | ||
ofType(new RangeError('error'),Error); //false | ||
ofType(new RangeError('error'),[Error,TypeError,RangeError]); //true | ||
type({}, 'any'); //true | ||
type({}, 'ANY'); //true | ||
type({}, 'an'); //false | ||
type({}, /any/); //true | ||
type({}, /ANY/); //false | ||
type({}, /ANY/i); //true | ||
type({}, /an/); //false; as it's unrecognizable custom type | ||
``` |
@@ -1,22 +0,12 @@ | ||
const path = require('path'); | ||
module.exports = { | ||
entry: { | ||
index:'./src/index.js' | ||
}, | ||
output: { | ||
filename: 'of-type.min.js', | ||
path: path.resolve(__dirname, 'prod'), | ||
library:'ofType', | ||
libraryTarget: 'umd', | ||
globalObject: 'this' | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/i, | ||
exclude:/(node_modules)/, | ||
loader:'babel-loader', | ||
options:{ | ||
presets:['env'] | ||
test: /\.js$/, | ||
exclude: /(node_modules)/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
presets: ['@babel/preset-env'] | ||
} | ||
} | ||
@@ -23,0 +13,0 @@ } |
const merge = require('webpack-merge'); | ||
const common = require('./webpack.common.js'); | ||
const UglifyPlugin = require('uglifyjs-webpack-plugin'); | ||
const frontEnd = require('./webpack.frontend.js'); | ||
const backEnd = require('./webpack.backend.js'); | ||
module.exports = merge(common, { | ||
const dev = { | ||
mode: 'development', | ||
optimization: { | ||
minimize: false | ||
}, | ||
watch: true, | ||
@@ -32,17 +30,8 @@ stats: { | ||
usedExports: false | ||
}, | ||
plugins:[ | ||
new UglifyPlugin({ | ||
uglifyOptions:{ | ||
compress:false, | ||
mangle:false, | ||
output:{ | ||
ecma:5, | ||
indent_level:2, | ||
comments:/^@/, | ||
beautify:true | ||
} | ||
} | ||
}) | ||
] | ||
}); | ||
} | ||
}; | ||
module.exports = [ | ||
merge(common, dev, backEnd), | ||
merge(common, dev, frontEnd) | ||
]; |
const merge = require('webpack-merge'); | ||
const common = require('./webpack.common.js'); | ||
const UglifyPlugin = require('uglifyjs-webpack-plugin'); | ||
const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); | ||
const frontEnd = require('./webpack.frontend.js'); | ||
const backEnd = require('./webpack.backend.js'); | ||
module.exports = merge(common, { | ||
const prod = { | ||
mode: 'production', | ||
watch: false, | ||
stats: false, | ||
optimization: { | ||
minimize: false | ||
}, | ||
watch:false, | ||
stats: false, | ||
plugins:[ | ||
new UglifyPlugin({ | ||
uglifyOptions:{ | ||
compress:true, | ||
mangle:true, | ||
output:{ | ||
ecma:5, | ||
indent_level:2, | ||
comments:false, | ||
beautify:false | ||
minimizer: [ | ||
new UglifyJsPlugin({ | ||
uglifyOptions: { | ||
compress: true, | ||
mangle: false, | ||
output: { | ||
indent_level: 2, | ||
comments: false, | ||
beautify: false | ||
} | ||
} | ||
} | ||
}) | ||
] | ||
}); | ||
}) | ||
] | ||
} | ||
}; | ||
module.exports = [ | ||
merge(common, prod, backEnd), | ||
merge(common, prod, frontEnd) | ||
]; |
Sorry, the diff of this file is not supported yet
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
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
617121
19
5384
484
17
1