Skip to content

Commit

Permalink
Return the target of successful checks, release version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pnann committed Jan 16, 2016
1 parent c194cd7 commit 3934863
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 220 deletions.
47 changes: 26 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var blah = null;
check(blah).is.a.string(); //Throws "Check failed: null was not a String"
check(blah, "blah").is.a.string(); //Throws "Check failed: blah (null) was not a String"
check(blah).is.not.a.string(); //OK!

var result = check(blah).is.null(); //OK! All checks return the value passed in, so result === blah.
```

A check will throw when its assertion fails and will otherwise do nothing. For instance, `check(null).is.a.string()`
Expand All @@ -38,7 +40,8 @@ will throw an Error, where `check(null).is.null()` will not.

`not` negates the check, so `check(null).is.not.a.string()` will not throw.

An optional name can be given with the object to check and will be included in the Error when thrown.
An optional name can be given with the object to check and will be included in the Error when thrown. All checks return
the original value passed in and typing is preserved for TypeScript consumers.

#### Named instances

Expand All @@ -52,14 +55,16 @@ var blah = null;
check(blah).is.a.string(); //Throws "[Logger] Check failed: null was not a String"
check(blah, "blah").is.a.string(); //Throws "[Logger] Check failed: blah (null) was not a String"
check(blah).is.not.a.string(); //OK!

var result = check(blah).is.null(); //OK! All checks return the value passed in, so result === blah.
```

## API

See [check-preconditions.d.ts](check-preconditions.d.ts) for TypeScript type definitions.

```javascript
interface Check {
interface Check<T> {

/**
* Passthrough properties, these have no affect on the instance. They can be called ad nauseum and in any order.
Expand All @@ -68,9 +73,9 @@ interface Check {
* check(blah).is.an.object();
* //etc.
*/
is: Check;
a: Check;
an: Check;
is: Check<T>;
a: Check<T>;
an: Check<T>;

/**
* Inverts the instance's check. This can only be used once per Check instance, so you can't not not something.
Expand All @@ -80,33 +85,33 @@ interface Check {
* check(blah).is.a.string(); //OK!
* check(blah).is.not.a.string(); //Throws
*/
not: Check;
not: Check<T>;

/**
* Type checks. These will throw if the target is not of the given type. Number includes NaN.
*/
function(): void;
object(): void;
number(): void;
string(): void;
array(): void;
null(): void;
undefined(): void;
true(): void;
false(): void;
function(): T;
object(): T;
number(): T;
string(): T;
array(): T;
null(): T;
undefined(): T;
true(): T;
false(): T;

/**
* Check that the target is empty. This check passes if the target is any of the following:
* - an object with no enumerable own properties.
* - an array with no elements
* - an empty string
*/
empty(): void;
empty(): T;

/**
* Check whether the target exists. This is defined as being neither null nor undefined.
*/
exists(): void;
exists(): T;
}

declare module "check-preconditions" {
Expand All @@ -116,16 +121,16 @@ declare module "check-preconditions" {
*
* @param {string} baseName - A baseName to link created Checks to. Usually a Class or Module name.
*/
export function of(baseName: string): (target: any, name?: string) => Check
export function of(baseName: string): <T>(target: T, name?: string) => Check<T>

/**
* Check the given target with an optional name. If a name is given it will be included in the Error thrown
* when the Check fails.
*
* @param {any} target - A thing to check against.
* @param {T} target - A thing to check against.
* @param {string} [name] - An optional name for the target.
*/
export function check(target: any, name?: string): Check
export function check<T>(target: T, name?: string): Check<T>
}
```
Expand Down
38 changes: 19 additions & 19 deletions check-preconditions.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface Check {
interface Check<T> {

/**
* Passthrough properties, these have no affect on the instance. They can be called ad nauseum and in any order.
Expand All @@ -7,9 +7,9 @@ interface Check {
* check(blah).is.an.object();
* //etc.
*/
is: Check;
a: Check;
an: Check;
is: Check<T>;
a: Check<T>;
an: Check<T>;

/**
* Inverts the instance's check. This can only be used once per Check instance, so you can't not not something.
Expand All @@ -19,33 +19,33 @@ interface Check {
* check(blah).is.a.string(); //OK!
* check(blah).is.not.a.string(); //Throws
*/
not: Check;
not: Check<T>;

/**
* Type checks. These will throw if the target is not of the given type. Number includes NaN.
*/
function(): void;
object(): void;
number(): void;
string(): void;
array(): void;
null(): void;
undefined(): void;
true(): void;
false(): void;
function(): T;
object(): T;
number(): T;
string(): T;
array(): T;
null(): T;
undefined(): T;
true(): T;
false(): T;

/**
* Check that the target is empty. This check passes if the target is any of the following:
* - an object with no enumerable own properties.
* - an array with no elements
* - an empty string
*/
empty(): void;
empty(): T;

/**
* Check whether the target exists. This is defined as being neither null nor undefined.
*/
exists(): void;
exists(): T;
}

declare module "check-preconditions" {
Expand All @@ -55,14 +55,14 @@ declare module "check-preconditions" {
*
* @param {string} baseName - A baseName to link created Checks to. Usually a Class or Module name.
*/
export function of(baseName: string): (target: any, name?: string) => Check
export function of(baseName: string): <T>(target: T, name?: string) => Check<T>

/**
* Check the given target with an optional name. If a name is given it will be included in the Error thrown
* when the Check fails.
*
* @param {any} target - A thing to check against.
* @param {T} target - A thing to check against.
* @param {string} [name] - An optional name for the target.
*/
export function check(target: any, name?: string): Check
export function check<T>(target: T, name?: string): Check<T>
}
2 changes: 1 addition & 1 deletion lib/check-preconditions.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/src/Check.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var Check = (function () {
var target = this.targetName ? this.targetName + " (" + this.target + ")" : "" + this.target;
throw new Error(prefix + "Check failed: " + target + " was " + (this.negated ? "" : "not ") + caseString);
}
return this.target;
};
return Check;
})();
Expand Down
41 changes: 0 additions & 41 deletions lib/tst/CheckStaticTest.js

This file was deleted.

103 changes: 0 additions & 103 deletions lib/tst/CheckTest.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "check-preconditions",
"version": "0.1.0",
"version": "0.2.0",
"description": "A small and simple ES3 compatible preconditions library for Node and the browser.",
"author": "Paul Nann",
"license": "MIT",
Expand Down
Loading

0 comments on commit 3934863

Please sign in to comment.