precond

WebJar for precond

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

precond
Last Version

Last Version

0.2.3
Release Date

Release Date

Type

Type

jar
Description

Description

precond
WebJar for precond
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/MathieuTurcotte/node-precond

Download precond

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/precond/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>precond</artifactId>
    <version>0.2.3</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/precond/
implementation 'org.webjars.npm:precond:0.2.3'
// https://jarcasting.com/artifacts/org.webjars.npm/precond/
implementation ("org.webjars.npm:precond:0.2.3")
'org.webjars.npm:precond:jar:0.2.3'
<dependency org="org.webjars.npm" name="precond" rev="0.2.3">
  <artifact name="precond" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='precond', version='0.2.3')
)
libraryDependencies += "org.webjars.npm" % "precond" % "0.2.3"
[org.webjars.npm/precond "0.2.3"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Preconditions for Node.js

Build Status NPM version

Precondition checks for Node.js inspired by Guava's precondition checking utilities.

Installation

npm install precond

Unit tests

npm test

Overview

Precond provides a set of functions to verify arguments and state correctness

It lets you rewrite constructs like the following

if (!this.isConnected) {
    throw new Error('Client should be connected before calling X.');
}

into a more compact and declarative check bellow.

precond.checkState(this.isConnected, 'Client should be ...');

Note that even though the throw statement is wrapped in a function, the call stack will still start from the calling function. So the previous examples would both produce the same stack trace.

All arguments after the message will be used to format the actual error message that will be thrown.

The following precondition checks are provded:

  • checkArgument(value, [messageFormat, [formatArgs, ...]])
  • checkState(value, [messageFormat, [formatArgs, ...]])
  • checkIsDef(value, [messageFormat, [formatArgs, ...]]) -> value
  • checkIsDefAndNotNull(value, [messageFormat, [formatArgs, ...]]) -> value
  • checkIsString(value, [messageFormat, [formatArgs, ...]]) -> value
  • checkIsArray(value, [messageFormat, [formatArgs, ...]]) -> value
  • checkIsNumber(value, [messageFormat, [formatArgs, ...]]) -> value
  • checkIsBoolean(value, [messageFormat, [formatArgs, ...]]) -> value
  • checkIsFunction(value, [messageFormat, [formatArgs, ...]]) -> value
  • checkIsObject(value, [messageFormat, [formatArgs, ...]]) -> value

API

Static functions

precond.checkArgument(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be truthy
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is true. Throws an IllegalArgumentError if value is false.

precond.checkState(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be truthy
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is true. Throws an IllegalStateError if value is false.

precond.checkIsDef(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be defined
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is defined (could be null). Throws an IllegalArgumentError if value is undefined. Returns the value of the value that was validated.

precond.checkIsDefAndNotNull(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be defined and not null
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is defined and not null. Throws an IllegalArgumentError if value is undefined or null. Returns the value of the value that was validated.

precond.checkIsString(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be a string
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is a string or a String object. Throws an IllegalArgumentError if value isn't a string. Returns the value of the value that was validated.

precond.checkIsArray(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be an array
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is an array. Throws an IllegalArgumentError if value isn't an array. Returns the value of the value that was validated.

precond.checkIsNumber(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be a number
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is a number. Throws an IllegalArgumentError if value isn't a number. Returns the value of the value that was validated.

precond.checkIsBoolean(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be a boolean
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is a boolean. Throws an IllegalArgumentError if value isn't a boolean. Returns the value of the value that was validated.

precond.checkIsFunction(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be a function
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is a function. Throws an IllegalArgumentError if value isn't a function. Returns the value of the value that was validated.

precond.checkIsObject(value, [messageFormat, [formatArgs, ...]])

  • value: the value that is required to be an object
  • messageFormat: error message format template
  • formatArgs: arguments to be substituted into the message template

Ensures that value is an object. Throws an IllegalArgumentError if value isn't an object. Returns the value of the value that was validated.

Class precond.IllegalArgumentError

Extends Error and is thrown to signal illegal arguments.

Class precond.IllegalStateError

Extends Error and is thrown to signal that the program or object has reached an illegal state.

License

This code is free to use under the terms of the MIT license.

Versions

Version
0.2.3