assert-kindof
Check native type of value and throw AssertionError if not okey. Clean stack traces. Simplicity. Built on is-kindof.
You might also be interested in kind-of-extra.
Highlights
- simplicity: pretty simple and stable codebase, built on kind-of and kind-of-extra
- flexibility: expose methods for each javascript type, using kind-of-types
- better coverage: ensures that your code will not have many branches
- clean stack traces: clean and small stack traces, using clean-stacktrace
- type checking: exposes is-kindof methods for returning booleans
- negations: support "not" modifier, e.g.
is.not.array(val)
- errors: enhanced error objects with
actual
,expected
,operator
andvalue
props - messages: customizable and clean error messages
Table of Contents
(TOC generated by verb using markdown-toc)
Install
Install with npm
$ npm install assert-kindof --save
or install using yarn
$ yarn add assert-kindof
Usage
For more use-cases see the tests
const assertKindof =
API
.is
All methods from is-kindof are also exposed, so check its docs. That
.is
is object with methods with same names as in this package.
Example
var assertKindof = assertKindofis // => falseassertKindofis // => true assertKindof // => not throws try assertKindof // => AssertionError: number !== array catch err console // => 'number !== array'
.array
Check
value
is array, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => AssertionError: number !== array try assert catch err console // => 'expect `val` to be array' console // => object console // => array console // => { foo: 'bar' }
.boolean
Check
value
is boolean, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => not throwsassert // => AssertionError: number !== booleanassert // => AssertionError: null !== boolean try assert catch err console // => 'expect `val` to be boolean' console // => array console // => boolean console // => [1, 2, 3]
.buffer
Check
value
is buffer, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => AssertionError: number !== buffer try assert catch err console // => 'expect `val` to be buffer' console // => boolean console // => buffer console // => true
.date
Check
value
is date, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => AssertionError: number !== date try assert catch err console // => 'expect `val` to be date' console // => object console // => date console // => { a: 'b' }
.error
Check
value
is error, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => not throwsassert // => AssertionError: number !== error try assert catch err console // => 'expect `val` to be error' console // => object console // => error console // => { a: 'b' }
.function
Check
value
is function, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => not throwsassert // => AssertionError: number !== error assert// => AssertionError: generatorfunction !== function try assert catch err console // => 'expect `val` to be function' console // => object console // => function console // => { a: 'b' }
.generator
Check
value
is generator, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var generator = { 42 }var {}var { return 123 } assert // => not throwsassert // => AssertionError: generatorfunction !== generatorassert // => AssertionError: function !== generatorassert // => AssertionError: number !== generator try assert catch err console // => 'expect `val` to be generator' console // => object console // => generator console // => { a: 'b' }
.generatorfunction
Check
value
is generator function, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var generator = { 42 }var {}var { return 123 } assert // => not throws assert // => AssertionError: generator !== generatorfunctionassert // => AssertionError: function !== generatorfunctionassert // => AssertionError: number !== generatorfunction try assert catch err console // => 'expect `val` to be generatorfunction' console // => object console // => generatorfunction console // => { a: 'b' }
.map
Check
value
is ES2015/ES6 Map, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => AssertionError: weakmap !== mapassert // => AssertionError: number !== map try assert catch err console // => 'expect `val` to be map' console // => number console // => map console // => { a: 'b' }
.null
Check
value
is null, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => AssertionError: object !== nullassert // => AssertionError: number !== null try assert catch err console // => 'expect `val` to be null' console // => number console // => null console // => 123
.number
Check
value
is number, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => AssertionError: object !== numberassert // => AssertionError: null !== number try assert catch err console // => 'expect `val` to be number' console // => array console // => number console // => [111, 222]
.object
Check
value
is object, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assertobject aaa: 'bbb' // => not throwsassertobject1 2 3 // => AssertionError: array !== objectassertobjectnull // => AssertionError: null !== object try assertobject111 222 'expect `val` to be {expected}' catch err console // => 'expect `val` to be object' console // => array console // => object console // => [111, 222]
.promise
Check
value
is promise, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => not throws assert // => AssertionError: map !== promiseassert // => AssertionError: number !== promise try assert catch err console // => 'expect `val` to be promise' console // => object console // => promise console // => { a: 1 }
.regexp
Check
value
is regexp, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => not throws assert // => AssertionError: map !== regexpassert // => AssertionError: number !== regexp try assert catch err console // => 'expect `val` to be regexp' console // => object console // => regexp console // => { a: 1 }
.set
Check
value
is ES2015/ES6 Set, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throwsassert // => AssertionError: map !== setassert // => AssertionError: number !== set try assert catch err console // => 'expect `val` to be set' console // => object console // => set console // => { a: 1 }
.stream
Check
value
is stream, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var through2 = assert // => not throwsassert // => not throws assert // => AssertionError: map !== streamassert // => AssertionError: number !== stream try assert catch err console // => 'expect `val` to be stream' console // => object console // => stream console // => { a: 1 }
.string
Check
value
is string, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var { return 123 }assert // => not throwsassert // => not throwsassert // => not throws assert // => AssertionError: map !== stringassert // => AssertionError: number !== string try assert catch err console // => 'expect `val` to be string' console // => object console // => string console // => { a: 1 }
.symbol
Check
value
is Symbol, if not throws AssertionError.
var assert = require('assert-kindof')*
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
assert // => not throws assert // => AssertionError: map !== symbolassert // => AssertionError: number !== symbol try assert catch err console // => 'expect `val` to be symbol' console // => object console // => symbol console // => { a: 1 }
.undefined
Check
value
is undefined, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assertundefined // => not throwsassertundefinedundefined // => not throws assertundefined // => AssertionError: map !== undefinedassertundefined123 // => AssertionError: number !== undefined try assertundefined a: 1 'expect `val` to be {expected}' catch err console // => 'expect `val` to be undefined' console // => object console // => undefined console // => { a: 1 }
.weakmap
Check
value
is ES2015/ES6 WeakMap, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throws assert // => AssertionError: weakset !== weakmapassert // => AssertionError: map !== weakmapassert // => AssertionError: number !== weakmap try assert catch err console // => 'expect `val` to be weakmap' console // => object console // => weakmap console // => { a: 1 }
.weakset
Check
value
is ES2015/ES6 WeakSet, if not throws AssertionError.
Params
value
{any}: value to be checkedmessage
{String|Function}: error message; if function getsfn(actual, expected, value)
signaturereturns
{Undefined}: nothing is returned, throws if not okey
Example
var assert = assert // => not throws assert // => AssertionError: weakmap !== weaksetassert // => AssertionError: map !== weaksetassert // => AssertionError: number !== weakset try assert catch err console // => 'expect `val` to be weakset' console // => object console // => weakset console // => { 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
andassert
. | 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 | homepage
Contributing
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
- Please DO NOT edit README.md, CHANGELOG.md and .verb.md files. See "Building docs" section.
- Ensure anything is okey by installing the dependencies and run the tests. See "Running tests" section.
- Always use
npm run commit
to commit changes instead ofgit commit
, because it is interactive and user-friendly. It uses commitizen behind the scenes, which follows Conventional Changelog idealogy. - 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
Author
Charlike Mike Reagent
License
Copyright © 2015, 2017, Charlike Mike Reagent. Released under the MIT License.
This file was generated by verb-generate-readme, v0.4.3, on March 10, 2017.
Project scaffolded using charlike cli.