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 and value 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 = require('assert-kindof')
API
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 = require('assert-kindof')
assertKindof.is.array(123)
assertKindof.is.array([11, 22, 33])
assertKindof.array([11, 22, 33])
try {
assertKindof.array(123)
} catch (err) {
console.log(err.message)
}
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
var assert = require('assert-kindof')
assert.array([1, 2, 3])
assert.array(123)
try {
assert.array({ foo: 'bar' }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.boolean(true)
assert.boolean(false)
assert.boolean(123)
assert.boolean(null)
try {
assert.boolean([1, 2, 3], 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.buffer(new Buffer('foo'))
assert.buffer(123)
try {
assert.buffer(true, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.date(new Date())
assert.date(123)
try {
assert.date({ a: 'b' }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.error(new Error())
assert.error(new TypeError())
assert.error(123)
try {
assert.error({ a: 'b' }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.function(function noop () {})
assert.function((a, b) => {})
assert.function(123)
assert.function(function * noop () {})
try {
assert.function({ a: 'b' }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var generator = (function * gen () { yield 42 })()
var genFn = function * genFn () {}
var noop = () => { return 123 }
assert.generator(generator)
assert.generator(genFn)
assert.generator(noop)
assert.generator(123)
try {
assert.generator({ a: 'b' }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var generator = (function * gen () { yield 42 })()
var genFn = function * genFn () {}
var noop = () => { return 123 }
assert.generatorfunction(genFn)
assert.generatorfunction(generator)
assert.generatorfunction(noop)
assert.generatorfunction(123)
try {
assert.generatorfunction({ a: 'b' }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.map(new Map())
assert.map(new WeakMap())
assert.map(123)
try {
assert.map(123, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.null(null)
assert.null({ a: 'b' })
assert.null(123)
try {
assert.null(123, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.number(123)
assert.number({ a: 'b' })
assert.number(null)
try {
assert.number([111, 222], 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.object({ aaa: 'bbb' })
assert.object([1, 2, 3])
assert.object(null)
try {
assert.object([111, 222], 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.promise(Promise.resolve(123))
assert.promise(Promise.reject(new Error('foo')))
assert.promise(new Map())
assert.promise(123)
try {
assert.promise({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.regexp(/foo ba?r abz/i)
assert.regexp(new RegExp('aa bb'))
assert.regexp(new Map())
assert.regexp(123)
try {
assert.regexp({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.set(new Set())
assert.set(new Map())
assert.set(123)
try {
assert.set({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var through2 = require('through2')
assert.stream(through2())
assert.stream(through2.obj())
assert.stream(new Map())
assert.stream(123)
try {
assert.stream({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var fn = function aa () { return 123 }
assert.string('foo bar baz')
assert.string(fn.toString())
assert.string(new String('abc'))
assert.string(new Map())
assert.string(123)
try {
assert.string({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
assert.symbol(Symbol())
assert.symbol(new Map())
assert.symbol(123)
try {
assert.symbol({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.undefined()
assert.undefined(undefined)
assert.undefined(new Map())
assert.undefined(123)
try {
assert.undefined({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.weakmap(new WeakMap())
assert.weakmap(new WeakSet())
assert.weakmap(new Map())
assert.weakmap(123)
try {
assert.weakmap({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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
var assert = require('assert-kindof')
assert.weakmap(new WeakSet())
assert.weakset(new WeakMap())
assert.weakset(new Map())
assert.weakset(123)
try {
assert.weakset({ a: 1 }, 'expect `val` to be {expected}')
} catch (err) {
console.log(err.message)
console.log(err.actual)
console.log(err.expected)
console.log(err.value)
}
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. | 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 of git 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.