Skip to content

Commit

Permalink
Add checker() function that returns a named checker
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoulb committed Apr 6, 2018
1 parent 7398026 commit 37c7b93
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ Please see (CONTRIBUTING.md)

## Changelog

- 4.5.0
- Add `checker()` function to return the boolean checker function itself.
- 4.4.0
- Add `json` checker to check for JSON-friendly values (null, true, false, finite numbers, strings, plain objects, plain arrays)
- 4.3.0
Expand Down
17 changes: 17 additions & 0 deletions lib/classes/Blorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ class Blorker {
this._find = this._find.bind(this);
}

/**
* Return a specific checker.
* @param {string|Function|Array|Object} type A single stringy type reference (e.g. 'str'), functional shorthand type reference (e.g. `String`), or an object/array with list of types (e.g. `{name:'str'}` or `['str', 'num']`).
* @return {function} The specified checker or undefined if it doesn't exist.
*/
checker(type) {
// Check args.
runChecker(this._checkers.string, type, "arguments[0]", BlorkError);

// Return the checker.
const checker = this._checkers[type];
if (checker) return checker;

// Not found.
throwError(BlorkError, "Checker not found", type, "arguments[0]");
}

/**
* Check values against types.
* Throws an error if a value doesn't match a specified type.
Expand Down
3 changes: 2 additions & 1 deletion lib/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const format = require("./functions/format");
const { ANY } = require("./constants");

// Make a default instance.
const { args, check, add, throws } = blork();
const { args, check, add, checker, throws } = blork();

// Exports.
exports.blork = blork;
exports.args = args;
exports.check = check;
exports.add = add;
exports.checker = checker;
exports.throws = throws;
exports.format = format;
exports.debug = debug;
Expand Down
5 changes: 3 additions & 2 deletions lib/functions/blork.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = function blork() {
return {
check: blorker.check.bind(blorker),
args: blorker.args.bind(blorker),
throws: blorker.throws.bind(blorker),
add: blorker.add.bind(blorker)
add: blorker.add.bind(blorker),
checker: blorker.checker.bind(blorker),
throws: blorker.throws.bind(blorker)
};
};
17 changes: 17 additions & 0 deletions test/exports.checker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const BlorkError = require("../lib/errors/BlorkError");
const { checker } = require("../lib/exports");

// Tests.
describe("exports.check()", () => {
test("Getting and using a checker works correctly", () => {
expect(checker("string")("abc")).toBe(true);
expect(checker("string")(123)).toBe(false);
});
test("Throw BlorkError if passing a non-string", () => {
expect(() => checker(123)).toThrow(BlorkError);
expect(() => checker(false)).toThrow(BlorkError);
});
test("Throw BlorkError if asking for non-existant checker", () => {
expect(() => checker("abc")).toThrow(BlorkError);
});
});

0 comments on commit 37c7b93

Please sign in to comment.