2.0.0 • Published 2 years ago

assert-argument v2.0.0

Weekly downloads
11
License
ISC
Repository
github
Last release
2 years ago

assert-argument

npm standard conduct

About

A small helper library for making assertions about function arguments.

This project has two main goals:

  1. provide methods that are focused on making assertions about argument types
  2. be an alternative to node's assert package that doesn't include all the node-specific dependencies like buffer and fs

For this use case I typically don't use the methods on node's assert module, but instead have found it useful to have a few type-related methods so that instead of doing things like: assert(value && typeof value === 'string') I can do: assert.isString(value)

Why make/use this?

I'm not a fan of using tools like flow or typescript, but I am a fan of writing defensively against mistakes. So here we are.

There are probably a lot of similar projects. For instance, if you don't care about type assertions and just want a really tiny assert module, check out nanoassert, it's cool.

This isn't meant to be a comprehensive type checking library, just a small collection of assertions for types that come up most often for me as function params.

Install

npm install --save assert-argument

Usage

Any time the type of an argument doesn't match the assertion, an ArgumentError will be thrown.

var assert = require('assert-argument')

function aReallyCoolFunction (str, options, callback) {
  assert.isString(str, 'first param is required and must be a string')
  assert.isObject(options, 'second param is required and must be an object')
  assert.isNumber(options.num, 'options.num must be a number')
  assert.isFunction(callback, 'third param must be a function')
  // do some cool stuff
}

Methods

var assert = require('assert-argument')

// check that value is truthy
assert(value, message)

// type-specific assertions
assert.isBoolean(value, message)
assert.isString(value, message)
assert.isNumber(value, message)
assert.isInteger(value, message)
assert.isObject(value, message)
assert.isArray(value, message)
assert.isFunction(value, message)
assert.isRegExp(value, message)
assert.isPromise(value, message)
assert.isDate(value, message)
assert.isError(value, message)
assert.isBuffer(value, message)

Any time the assertion fails an error will be thrown with the format of ArgumentError: <message>, where <message> is the string you passed in as the second argument to describe the assertion.

Documentation

Contributing

Contributions are welcome! Please read the contributing guidelines first. Also see the tests readme for detailed development instructions, and check out the note above about when we might add more type assertions.

Conduct

It's important that this project contributes to a friendly, safe, and welcoming environment for all, particularly for folks that are historically underrepresented in technology. Read this project's code of conduct

Change log

Read about the changes to this project in CHANGELOG.md. The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

License

ISC