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

condicio

v2.0.0

Published

A preconditions library for JavaScript similar to Google's Preconditions from Guava.

Downloads

8

Readme

Condicio

What

A simple library similar to Google's Preconditions in Guava. The current version is 2.0.0.

Why

I got tired of writing boilerplate code to check my arguments and their types, and I didn't want to bring in a huge dependency like jQuery or Underscore simply to do parameter and argument checking. I couldn't really find anything out there that did the job so I decided to roll my own.

How

All you need is condicio.js or condicio.min.js from the dist directory. Simply include that in your code and you're good to go. Condicio is UMD-compatible, so you can use it with RequireJS or node, if you wish. Condicio is also available from npm.

Examples

Checking an argument:

function divide(numerator, denominator) {
    condicio.checkArgument(denominator != 0);
    ...
}

Ensuring an argument is not null:

function frobulateGromulator(droob) {
    condicio.checkNotNull(droob, "Droob cannot be null!");
    ...
}

Ensuring that arguments are of a specific type:

function render(name, callback) {
    condicio.checkIsString(name);
    condicio.checkIsFunction(callback, "{0} callback must be a function.", [name]);
    ...
}

There are also functions that return a boolean instead of throwing an exception:

function render(name, callbackOrString) {
    if(condicio.isFunction(callbackOrString)) {
        ...
    } else if(condicio.isString(callbackOrString)) {
        ...
    } else {
        throw new TypeError("Second argument must be a function or a string.");
    }
}

API

All check functions take an optional message and arguments parameter. message can be provided for custom error messages. arguments is an array of values than can be used for interpolation. For example, if message is "The argument must be {0} and {1}", and arguments is the array ["one", "two"], then the interpolated error-message will be "The argument must be one and two". This is useful for custom, dynamic, error-messages.

In addition to throwing standard error-types (i.e., TypeError, ReferenceError, and RangeError), the API exposes the following errors:

  • condicio.IllegalArgumentError: Thrown when an invalid or illegal argument is supplied.
  • condicio.IllegalStateError: Thrown when the state of the calling instance or context is invalid.

These are full-fledged Error objects.

The API supports the following functions to check your arguments or the state of the calling-instance or context:

checkArgument(expression, [message, [arguments]])

Ensures that the expression involving one or more arguments to the function is true. If expression is false, a condicio.IllegalArgumentError will be thrown.


checkState(expression, [message, [arguments]])

Ensures that the expression involving the state of the calling instance or context, but not involving any parameters to the calling function, is true. If expression is false, a condicio.IllegalStateError will be thrown.


checkNotNull(reference, [message, [arguments]])

Ensures that reference is not null. Throws a TypeError if reference is null.


checkNotUndefined(reference, [message, [arguments]])

Ensures that reference is not undefined. Throws a ReferenceError if reference is undefined.


checkElementIndex(index, size, [message, [arguments]])

Ensures that index specifies a valid element in an array or string of size size. Valid indexes range from 0 to size - 1. Throws RangeError if index is negative or not less than size. Throws condicio.IllegalArgumentError if size is negative.


checkPositionIndex(index, size, [message, [arguments]])

Ensures that index specifies a valid position in an array or string of size size. Valid positions range from 0 to size. This precondition is typically used to test for a valid position to add a new element.Throws condicio. RangeError if index is negative or greater than size. Throws IllegalArgumentError is size is negative.


checkPositionIndexes(start, end, size, [message, [arguments]])

Ensures that start and end specify valid positions in an array or string of size size, and are in order. Throws RangeError if either index is negative or is greater than size, or if end is less than start. Throws condicio.IllegalArgumentError if size is negative.


checkObjectDirectProperty(object, property, [message, [arguments]])

Ensures that the property property exists and is a direct property of object. Throws ReferenceError if the property is not a direct property of object.


checkObjectProperty(object, property, [message, [arguments]])

Ensures that the property property exists and is a property (direct or inherited via prototype chain) of object. Throws ReferenceError if the property is not a property (direct or inherited via prototype chain) of object.


checkIsBoolean(object, [message, [arguments]])

Ensures that object is a boolean. Throws TypeError if object is not a boolean.


checkIsNumber(object, [message, [arguments]])

Ensures that object is a number. Throws TypeError if object is not a number.


checkIsString(object, [message, [arguments]])

Ensures that object is a string. Throws TypeError if object is not a string.


checkIsArray(object, [message, [arguments]])

Ensures that object is an array. Throws TypeError if object is not a array.


checkIsObject(object, [message, [arguments]])

Ensures that object is an object. Throws TypeError if object is not a object.


checkIsFunction(object, [message, [arguments]])

Ensures that object is a function. Throws TypeError if object is not a function.


checkIsType(object, type, [message, [arguments]])

Ensures that object is an instance of type. Throws TypeError if object is not an instance of type.


isNull(reference)

Returns true if reference is null.


isUndefined(reference)

Returns true if reference is undefined.


isElementIndexInvalid(index, size)

Returns true if index does not specify a valid element in an array or string of size size. Valid indexes range from 0 to size - 1.


isPositionIndexInvalid(index, size)

Returns true if index does not specify a valid position in an array or string of size size. Valid positions range from 0 to size. This precondition is typically used to test for a valid position to add a new element.


arePositionIndexesInvalid(start, end, size)

Returns true if start and end do not specify valid positions in an array or string of size size, and are in order.


isObjectDirectProperty(object, property)

Returns true if the property property exists and is a direct property of object.


isObjectProperty(object, property)

Returns true if the property property exists and is a property (direct or inherited via prototype chain) of object.


isBoolean(object)

Returns true if object is a boolean.


isNumber(object)

Returns true if object is a number.


isString(object)

Returns true if object is a string.


isArray(object)

Returns true if object is an array.


isObject(object)

Returns true if object is an object.


isFunction(object)

Returns true if object is a function.


isType(object, type)

Returns true if object is an instance of type.