npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

assert-argument

v2.0.0

Published

simple assertion module for checking function arguments

Downloads

123

Readme

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