assert-kindof

Check native type of value and throw AssertionError if not okey. Clean stack traces. Simplicity. Built on [is-kindof][].

Usage no npm install needed!

<script type="module">
  import assertKindof from 'https://cdn.skypack.dev/assert-kindof';
</script>

README

assert-kindof NPM version mit license NPM monthly downloads npm total downloads

Check native type of value and throw AssertionError if not okey. Clean stack traces. Simplicity. Built on is-kindof.

code climate code style linux build windows build code coverage dependency status paypal donate

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

.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 = 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'
}

.array

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]) // => not throws
assert.array(123) // => AssertionError: number !== array

try {
  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' }
}

.boolean

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) // => not throws
assert.boolean(false) // => not throws
assert.boolean(123) // => AssertionError: number !== boolean
assert.boolean(null) // => AssertionError: null !== boolean

try {
  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]
}

.buffer

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')) // => not throws
assert.buffer(123) // => AssertionError: number !== buffer

try {
  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
}

.date

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()) // => not throws
assert.date(123) // => AssertionError: number !== date

try {
  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' }
}

.error

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()) // => not throws
assert.error(new TypeError()) // => not throws
assert.error(123) // => AssertionError: number !== error

try {
  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' }
}

.function

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 () {}) // => not throws
assert.function((a, b) => {}) // => not throws
assert.function(123) // => AssertionError: number !== error

assert.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' }
}

.generator

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) // => 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' }
}

.generatorfunction

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) // => 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' }
}

.map

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()) // => not throws
assert.map(new WeakMap()) // => AssertionError: weakmap !== map
assert.map(123) // => AssertionError: number !== map

try {
  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' }
}

.null

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) // => not throws
assert.null({ a: 'b' }) // => AssertionError: object !== null
assert.null(123) // => AssertionError: number !== null

try {
  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
}

.number

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) // => not throws
assert.number({ a: 'b' }) // => AssertionError: object !== number
assert.number(null) // => AssertionError: null !== number

try {
  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]
}

.object

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' }) // => not throws
assert.object([1, 2, 3]) // => AssertionError: array !== object
assert.object(null) // => AssertionError: null !== object

try {
  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]
}

.promise

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)) // => not throws
assert.promise(Promise.reject(new Error('foo'))) // => not throws

assert.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 }
}

.regexp

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) // => not throws
assert.regexp(new RegExp('aa bb')) // => not throws

assert.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 }
}

.set

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()) // => not throws
assert.set(new Map()) // => AssertionError: map !== set
assert.set(123) // => AssertionError: number !== set

try {
  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 }
}

.stream

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()) // => not throws
assert.stream(through2.obj()) // => not throws

assert.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 }
}

.string

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') // => not throws
assert.string(fn.toString()) // => not throws
assert.string(new String('abc')) // => not throws

assert.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 }
}

.symbol

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()) // => not throws

assert.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 }
}

.undefined

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() // => not throws
assert.undefined(undefined) // => not throws

assert.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 }
}

.weakmap

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()) // => not throws

assert.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 }
}

.weakset

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()) // => not throws

assert.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. | 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

  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

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.