Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lcaballero/check-that
This is a little library that is very similar to Google's Guava checkArgs library, but for JavaScript / CoffeeScript.
https://github.com/lcaballero/check-that
Last synced: 23 days ago
JSON representation
This is a little library that is very similar to Google's Guava checkArgs library, but for JavaScript / CoffeeScript.
- Host: GitHub
- URL: https://github.com/lcaballero/check-that
- Owner: lcaballero
- License: epl-1.0
- Created: 2014-07-02T18:26:47.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-07-12T00:49:33.000Z (over 10 years ago)
- Last Synced: 2024-04-26T14:21:52.707Z (8 months ago)
- Language: CoffeeScript
- Size: 266 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme-preview.html
- License: license
Awesome Lists containing this project
README
Previewing Readme.md
Introduction
This is a little library that is very similar to Google's Guava checkArgs library.
Though similar, it is not a direct port of that library's interface. Mostly due to JavaScript's dynamic nature this library leans towards a more functional interface.
Installation
%> npm install check-that [--save]
Usage
A typical example is shown below. This example demonstrates checking the value of 'name'. The value, in this case, must exist and be non-null. In this case the default 'exists' function tests that the value is not
undefined
and notnull
. But, if the value is eithernull
orundefined
the check will throw an exception with the last functionelseThrowIt
.
`coffee
In CoffeeScript:
CheckThat = require('CheckThat')
{ exists, notEmpty, checkThat } = CheckThat
importantFunc: (name) -> checkThat('Name must exist and be non-empty', name, exists, notEmpty, elseThrowIt) ...
`
`javascript // In JavaScript:
CheckThat = require('CheckThat')
var exists = CheckThat.exists, notEmpty = CheckThat.notEmpty, checkThat = CheckThat.checkThat;
function importantFunc(name) { checkThat('Name must exist and be non-empty', name, exists, notEmpty, elseThrowIt) ... }
`
The
checkThat
signature ischeckThat(message, val, predicates..., cb)
. Take note that thecb
function appears after the var-argspredicate
appears as the second to last parameter. This is a bit unusual, and so how it works needs some explanation. If the list of predicates is 1 then thecb
function defaults toelseThrowIt
. Which means that if theval
fails to pass the lone predicate anError
will be thrown, in this special case.The code below is an example where an
Error
will be thrown since only one predicate function is provided.
coffee someFunction: (val) -> checkThat('Name must exist', val, exists)
As an alternative to the function above where
exists
sits as the last predicate there another function is provided bycheck-that
withcheckExists
.
coffee someFunction: (val) -> checkExists(val, 'Name must exists')
checkExists
is built on top ofcheckThat
and the other predicate functions to produce a somewhat simpler interface for the same result.API
checkThat(message, val, predicates..., cb)
This is the work horse of the library. The typical use is shown above. It accepts a message and a value to run through the predicates, if the number of predicates is 1 then the signature/logic assumes no callback, and checkThat will throw an Error (instead of running the callback). In the case where the number of predicates is greater than 1 the last function is considered the callback, and will be called and passed an
Error
wrapping the message if the value fails any of the predicates. Else the error value to the callback will benull
.
exists(val)
notEmpty(val)
checkConstraints(val, predicates)
Returns true if all the predicates validate the value, else it returns false, and does not throw an Error.
checkExists(value, [message])
If the value is not
null
and is notundefined
. If the value isnull
orundefined
then anError
is thrown with the a default message if one is not provided.
checkIndex(arr, index, [message])
Given an any object with a length property this function will validate that index is both non-negative and less then the length. Else it will throw an
Error
with the given message or a default message.
checkNonEmpty(aString, [message])
Given a string this function will test to see if the string, when trimmed, is non-empty. If the string is made of of only whitespace then an
Error
will be thrown.
elseThrowIt(err)
In the code above
elseThrowIt
is shown as the last parameter tocheckThat
. It acts as a callback where no custom callback is provided. If any of the predicates fail to validate the the value (by returning false) then thencheckThat
will generate anError
with a message and pass it toelseThrowIt
. In which case the error will be non-null andelseThrowIt
will literally throw the error. However, if the all predicates validate the function then error will be null when passed toelseThrowIt
and it will returntrue
without throwing anError
.License
See license file.
The use and distribution terms for this software are covered by the Eclipse Public License 1.0, which can be found in the file 'license' at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.