Comparing version 4.0.0 to 4.0.1
{ | ||
"name": "blork", | ||
"description": "Blork! Mini runtime type checking in Javascript", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"license": "0BSD", | ||
@@ -6,0 +6,0 @@ "author": "Dave Houlbrooke <dave@shax.com>", |
@@ -54,7 +54,7 @@ # Blork! Mini runtime type checking in Javascript | ||
`check()` can be passed three arguments: | ||
`check()` accepts three arguments: | ||
1. `value` | The value to check | ||
2. `type` | The type to check the value against (list of types is available below) | ||
3. An optional string name/prefix for the value, which is prepended to any error message thrown to help debugging | ||
1. `value` The value to check | ||
2. `type` The type to check the value against (list of types is available below) | ||
3. `prefix=""` An optional string name/prefix for the value, which is prepended to any error message thrown to help debugging | ||
@@ -158,2 +158,8 @@ ```js | ||
`add()` accepts three arguments: | ||
1. `name` The name of the custom checker you'll use to reference it later | ||
2. `checker` A function that accepts a single argument, `value`, and returns `true` or `false`. | ||
3. `description=""` An description for the value the checker will accept, e.g. "lowercase string" or "unique username", that is shown in the error message. Defaults to the value of `name`. | ||
```js | ||
@@ -208,3 +214,3 @@ import { add, check } from "blork"; | ||
To change the error object Blork throws when a type doesn't match, use the `throws()` function. | ||
To change the error object Blork throws when a type doesn't match, use the `throws()` function. It accepts a single argument a custom class (can be _any_ class, not just classes extending `Error`). | ||
@@ -300,5 +306,6 @@ ```js | ||
check("abc", "lower"); // No error. | ||
check(100, "whole"); // No error. | ||
check(100, "integer"); // No error. | ||
check([1, 2, 3], "array+"); // No error. | ||
check(new Date(2180, 1, 1), "future"); // No error. | ||
check(new Map([[1, 1], [2, 2]]), "map+"); // No error. | ||
@@ -308,3 +315,3 @@ // Fail. | ||
check({}, "object+"); // Throws TypeError "Must be object with one or more properties (received Object(0))" | ||
check([], "array+"); // Throws TypeError "Must berray with one or more items (received Array(0))" | ||
check([], "array+"); // Throws TypeError "Must be array with one or more items (received Array(0))" | ||
``` | ||
@@ -347,8 +354,11 @@ | ||
```js | ||
add("catty", v => v.toLowerCase().indexOf("cat")); // Checks that cat | ||
// Pass. | ||
check("this cat is crazy!", "lower & catty"); // No error. | ||
check("THIS cat is crazy!", "string & catty"); // No error. | ||
check("THIS CAT IS CRAZY", "upper & catty"); // No error. | ||
// Fail. | ||
check(null, "str & num"); // Throws TypeError "Must be string and number (received null)" | ||
check("THIS CAT IS CRAZY", "lower & catty"); // Throws TypeError "Must be lowercase string and catty" | ||
check("THIS DOG IS CRAZY", "string & catty"); // Throws TypeError "Must be string and catty" | ||
``` | ||
@@ -376,3 +386,3 @@ | ||
You can pass in _any_ class name, and Blork will check the value using `instanceof` and generate a corresponding error message if the type doesn't match. | ||
You can pass in _any_ class name, and Blork will check the value using `instanceof` and generate a corresponding error message if the type doesn't match. | ||
@@ -379,0 +389,0 @@ Using `Object` and `Array` constructors will work also and will allow any object that is `instanceof Object` or `instanceof Array`. _Note: this is not the same as e.g. the `'object'` and `'array'` string types, which only allow plain objects an arrays (but will reject objects of custom classes extending `Object` or `Array`)._ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
90544
490
0