Check native type of value and throw AssertionError if not okey. Clean stack traces. Simplicity. Built on [is-kindof][].
npm install assert-kindof> Check native type of value and throw AssertionError if not okey. Clean stack traces. Simplicity. Built on [is-kindof][].
[![code climate][codeclimate-img]][codeclimate-url]
[![code style][standard-img]][standard-url]
[![linux build][travis-img]][travis-url]
[![windows build][appveyor-img]][appveyor-url]
[![code coverage][coverage-img]][coverage-url]
[![dependency status][david-img]][david-url]
[![paypal donate][paypalme-img]][paypalme-url]
You might also be interested in kind-of-extra.
is.not.array(val)actual, expected, operator and value props_(TOC generated by verb using markdown-toc)_
```
$ npm install assert-kindof --save
or install using yarn
``
$ yarn add assert-kindof
`js`
const assertKindof = require('assert-kindof')
is object with methods with same names as in this package.Example
`js
var assertKindof = require('assert-kindof')assertKindof.is.array(123) // => false
assertKindof.is.array([11, 22, 33]) // => true
assertKindof.array([11, 22, 33]) // => not throws
try {
assertKindof.array(123) // => AssertionError: number !== array
} catch (err) {
console.log(err.message) // => 'number !== array'
}
`$3
> Check value is array, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.array([1, 2, 3]) // => not throws
assert.array(123) // => AssertionError: number !== arraytry {
assert.array({ foo: 'bar' }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be array'
console.log(err.actual) // => object
console.log(err.expected) // => array
console.log(err.value) // => { foo: 'bar' }
}
`$3
> Check value is boolean, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.boolean(true) // => not throws
assert.boolean(false) // => not throws
assert.boolean(123) // => AssertionError: number !== boolean
assert.boolean(null) // => AssertionError: null !== booleantry {
assert.boolean([1, 2, 3], 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be boolean'
console.log(err.actual) // => array
console.log(err.expected) // => boolean
console.log(err.value) // => [1, 2, 3]
}
`$3
> Check value is buffer, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.buffer(new Buffer('foo')) // => not throws
assert.buffer(123) // => AssertionError: number !== buffertry {
assert.buffer(true, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be buffer'
console.log(err.actual) // => boolean
console.log(err.expected) // => buffer
console.log(err.value) // => true
}
`$3
> Check value is date, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.date(new Date()) // => not throws
assert.date(123) // => AssertionError: number !== datetry {
assert.date({ a: 'b' }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be date'
console.log(err.actual) // => object
console.log(err.expected) // => date
console.log(err.value) // => { a: 'b' }
}
`$3
> Check value is error, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.error(new Error()) // => not throws
assert.error(new TypeError()) // => not throws
assert.error(123) // => AssertionError: number !== errortry {
assert.error({ a: 'b' }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be error'
console.log(err.actual) // => object
console.log(err.expected) // => error
console.log(err.value) // => { a: 'b' }
}
`$3
> Check value is function, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.function(function noop () {}) // => not throws
assert.function((a, b) => {}) // => not throws
assert.function(123) // => AssertionError: number !== errorassert.function(function * noop () {})
// => AssertionError: generatorfunction !== function
try {
assert.function({ a: 'b' }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be function'
console.log(err.actual) // => object
console.log(err.expected) // => function
console.log(err.value) // => { a: 'b' }
}
`$3
> Check value is generator, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var generator = (function * gen () { yield 42 })()
var genFn = function * genFn () {}
var noop = () => { return 123 }assert.generator(generator) // => not throws
assert.generator(genFn) // => AssertionError: generatorfunction !== generator
assert.generator(noop) // => AssertionError: function !== generator
assert.generator(123) // => AssertionError: number !== generator
try {
assert.generator({ a: 'b' }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be generator'
console.log(err.actual) // => object
console.log(err.expected) // => generator
console.log(err.value) // => { a: 'b' }
}
`$3
> Check value is generator function, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var generator = (function * gen () { yield 42 })()
var genFn = function * genFn () {}
var noop = () => { return 123 }assert.generatorfunction(genFn) // => not throws
assert.generatorfunction(generator) // => AssertionError: generator !== generatorfunction
assert.generatorfunction(noop) // => AssertionError: function !== generatorfunction
assert.generatorfunction(123) // => AssertionError: number !== generatorfunction
try {
assert.generatorfunction({ a: 'b' }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be generatorfunction'
console.log(err.actual) // => object
console.log(err.expected) // => generatorfunction
console.log(err.value) // => { a: 'b' }
}
`$3
> Check value is ES2015/ES6 Map, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.map(new Map()) // => not throws
assert.map(new WeakMap()) // => AssertionError: weakmap !== map
assert.map(123) // => AssertionError: number !== maptry {
assert.map(123, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be map'
console.log(err.actual) // => number
console.log(err.expected) // => map
console.log(err.value) // => { a: 'b' }
}
`$3
> Check value is null, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.null(null) // => not throws
assert.null({ a: 'b' }) // => AssertionError: object !== null
assert.null(123) // => AssertionError: number !== nulltry {
assert.null(123, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be null'
console.log(err.actual) // => number
console.log(err.expected) // => null
console.log(err.value) // => 123
}
`$3
> Check value is number, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.number(123) // => not throws
assert.number({ a: 'b' }) // => AssertionError: object !== number
assert.number(null) // => AssertionError: null !== numbertry {
assert.number([111, 222], 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be number'
console.log(err.actual) // => array
console.log(err.expected) // => number
console.log(err.value) // => [111, 222]
}
`$3
> Check value is object, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.object({ aaa: 'bbb' }) // => not throws
assert.object([1, 2, 3]) // => AssertionError: array !== object
assert.object(null) // => AssertionError: null !== objecttry {
assert.object([111, 222], 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be object'
console.log(err.actual) // => array
console.log(err.expected) // => object
console.log(err.value) // => [111, 222]
}
`$3
> Check value is promise, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.promise(Promise.resolve(123)) // => not throws
assert.promise(Promise.reject(new Error('foo'))) // => not throwsassert.promise(new Map()) // => AssertionError: map !== promise
assert.promise(123) // => AssertionError: number !== promise
try {
assert.promise({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be promise'
console.log(err.actual) // => object
console.log(err.expected) // => promise
console.log(err.value) // => { a: 1 }
}
`$3
> Check value is regexp, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.regexp(/foo ba?r abz/i) // => not throws
assert.regexp(new RegExp('aa bb')) // => not throwsassert.regexp(new Map()) // => AssertionError: map !== regexp
assert.regexp(123) // => AssertionError: number !== regexp
try {
assert.regexp({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be regexp'
console.log(err.actual) // => object
console.log(err.expected) // => regexp
console.log(err.value) // => { a: 1 }
}
`$3
> Check value is ES2015/ES6 Set, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.set(new Set()) // => not throws
assert.set(new Map()) // => AssertionError: map !== set
assert.set(123) // => AssertionError: number !== settry {
assert.set({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be set'
console.log(err.actual) // => object
console.log(err.expected) // => set
console.log(err.value) // => { a: 1 }
}
`$3
> Check value is stream, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var through2 = require('through2')
assert.stream(through2()) // => not throws
assert.stream(through2.obj()) // => not throwsassert.stream(new Map()) // => AssertionError: map !== stream
assert.stream(123) // => AssertionError: number !== stream
try {
assert.stream({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be stream'
console.log(err.actual) // => object
console.log(err.expected) // => stream
console.log(err.value) // => { a: 1 }
}
`$3
> Check value is string, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var fn = function aa () { return 123 }
assert.string('foo bar baz') // => not throws
assert.string(fn.toString()) // => not throws
assert.string(new String('abc')) // => not throwsassert.string(new Map()) // => AssertionError: map !== string
assert.string(123) // => AssertionError: number !== string
try {
assert.string({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be string'
console.log(err.actual) // => object
console.log(err.expected) // => string
console.log(err.value) // => { a: 1 }
}
`$3
> Check value is Symbol, if not throws AssertionError.var assert = require('assert-kindof')*
Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
assert.symbol(Symbol()) // => not throwsassert.symbol(new Map()) // => AssertionError: map !== symbol
assert.symbol(123) // => AssertionError: number !== symbol
try {
assert.symbol({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be symbol'
console.log(err.actual) // => object
console.log(err.expected) // => symbol
console.log(err.value) // => { a: 1 }
}
`$3
> Check value is undefined, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.undefined() // => not throws
assert.undefined(undefined) // => not throwsassert.undefined(new Map()) // => AssertionError: map !== undefined
assert.undefined(123) // => AssertionError: number !== undefined
try {
assert.undefined({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be undefined'
console.log(err.actual) // => object
console.log(err.expected) // => undefined
console.log(err.value) // => { a: 1 }
}
`$3
> Check value is ES2015/ES6 WeakMap, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.weakmap(new WeakMap()) // => not throwsassert.weakmap(new WeakSet()) // => AssertionError: weakset !== weakmap
assert.weakmap(new Map()) // => AssertionError: map !== weakmap
assert.weakmap(123) // => AssertionError: number !== weakmap
try {
assert.weakmap({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be weakmap'
console.log(err.actual) // => object
console.log(err.expected) // => weakmap
console.log(err.value) // => { a: 1 }
}
`$3
> Check value is ES2015/ES6 WeakSet, if not throws AssertionError.Params
*
value {any}: value to be checked
* message {String|Function}: error message; if function gets fn(actual, expected, value) signature
* returns {Undefined}: nothing is returned, throws if not okey Example
`js
var assert = require('assert-kindof')
assert.weakmap(new WeakSet()) // => not throwsassert.weakset(new WeakMap()) // => AssertionError: weakmap !== weakset
assert.weakset(new Map()) // => AssertionError: map !== weakset
assert.weakset(123) // => AssertionError: number !== weakset
try {
assert.weakset({ a: 1 }, 'expect
val to be {expected}')
} catch (err) {
console.log(err.message) // => 'expect val to be weakset'
console.log(err.actual) // => object
console.log(err.expected) // => weakset
console.log(err.value) // => { a: 1 }
}
`Related
- always-done: Handle completion and errors with elegance! Support for streams, callbacks, promises, child processes, async/await and sync functions. A drop-in replacement… more | homepage
- assertit: Thin sugar layer on top of testit framework, is-kindof and assert. | testit framework, is-kindof and assert."" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">homepage
- is-kindof: Check type of given javascript value. Support promises, generators, streams, and native types. Built on [kind-of][] lib. | homepage
- kind-of-extra: Additional functionality to [kind-of][] type check utility. Support promises, generators, streams, errors. | homepage
- kind-of-types: List of all javascript types. Used and useful for checking, validation, sanitizing and testing. Like isStream, isPromise, isWeakset and etc. | homepage
- kind-of: Get the native type of a value. | homepage
- mukla: Small, parallel and fast test framework with suppport for async/await, promises, callbacks, streams and observables. Targets and works at node.js… more | homepage
- try-catch-callback: try/catch block with a callback, used in [try-catch-core][]. Use it when you don't care about asyncness so much and don't… more | homepage
- try-catch-core: Low-level package to handle completion and errors of sync or asynchronous functions, using [once][] and [dezalgo][] libs. Useful for and… more | homepage
- try-read-json: Graceful reading of JSON value, using JSON.parse with support for optional callback | homepageContributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guidelines for advice on opening issues, pull requests, and coding standards.
If you need some help and can spent some cash, feel free to contact me at CodeMentor.io too.In short: If you want to contribute to that project, please follow these things
1. Please DO NOT edit README.md, CHANGELOG.md and .verb.md files. See "Building docs" section.
2. Ensure anything is okey by installing the dependencies and run the tests. See "Running tests" section.
3. Always use
npm run commit to commit changes instead of git commit, because it is interactive and user-friendly. It uses [commitizen][] behind the scenes, which follows Conventional Changelog idealogy.
4. Do NOT bump the version in package.json. For that we use npm run release, which is [standard-version][] and follows Conventional Changelog idealogy.Thanks a lot! :)
Building docs
Documentation and that readme is generated using [verb-generate-readme][], which is a [verb][] generator, so you need to install both of them and then run verb command like that`
$ npm install verbose/verb#dev verb-generate-readme --global && verb
`_Please don't edit the README directly. Any changes to the readme must be made in .verb.md._
Running tests
Clone repository and run the following in that cloned directory`
$ npm install && npm test
``+ github/tunnckoCore
+ twitter/tunnckoCore
+ codementor/tunnckoCore
*
_This file was generated by verb-generate-readme, v0.4.3, on March 10, 2017._
_Project scaffolded using [charlike][] cli._
[always-done]: https://github.com/hybridables/always-done
[async-done]: https://github.com/gulpjs/async-done
[charlike]: https://github.com/tunnckocore/charlike
[clean-stacktrace]: https://github.com/tunnckocore/clean-stacktrace
[commitizen]: https://github.com/commitizen/cz-cli
[dezalgo]: https://github.com/npm/dezalgo
[is-kindof]: https://github.com/tunnckocore/is-kindof
[kind-of-extra]: https://github.com/tunnckocore/kind-of-extra
[kind-of-types]: https://github.com/tunnckocore/kind-of-types
[kind-of]: https://github.com/jonschlinkert/kind-of
[once]: https://github.com/isaacs/once
[standard-version]: https://github.com/conventional-changelog/standard-version
[try-catch-core]: https://github.com/hybridables/try-catch-core
[verb-generate-readme]: https://github.com/verbose/verb-generate-readme
[verb]: https://github.com/verbose/verb
[license-url]: https://www.npmjs.com/package/assert-kindof
[license-img]: https://img.shields.io/npm/l/assert-kindof.svg
[downloads-url]: https://www.npmjs.com/package/assert-kindof
[downloads-img]: https://img.shields.io/npm/dt/assert-kindof.svg
[codeclimate-url]: https://codeclimate.com/github/tunnckoCore/assert-kindof
[codeclimate-img]: https://img.shields.io/codeclimate/github/tunnckoCore/assert-kindof.svg
[travis-url]: https://travis-ci.org/tunnckoCore/assert-kindof
[travis-img]: https://img.shields.io/travis/tunnckoCore/assert-kindof/master.svg?label=linux
[appveyor-url]: https://ci.appveyor.com/project/tunnckoCore/assert-kindof
[appveyor-img]: https://img.shields.io/appveyor/ci/tunnckoCore/assert-kindof/master.svg?label=windows
[coverage-url]: https://codecov.io/gh/tunnckoCore/assert-kindof
[coverage-img]: https://img.shields.io/codecov/c/github/tunnckoCore/assert-kindof/master.svg
[david-url]: https://david-dm.org/tunnckoCore/assert-kindof
[david-img]: https://img.shields.io/david/tunnckoCore/assert-kindof.svg
[standard-url]: https://github.com/feross/standard
[standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
[paypalme-url]: https://www.paypal.me/tunnckoCore
[paypalme-img]: https://img.shields.io/badge/paypal-donate-brightgreen.svg