typist.js
v0.1.1
Published
Type checking in JS
Downloads
1
Maintainers
Readme
Typist
Type check function return values at run time, or type check any value any time.
Install
npm install typist.js --save
All in one example
Seamlessly type check the return type as well as input types for the life of the function.
var makeArray = type.takes(Array, String, Number)
.does(function(input, foo, bar) {
input.push(foo, bar);
return input;
})
.returns(Array)
.done();
makeArray([], "1", 2); // ["1", 2]
makeArray([], 1, 2); // Throws TypistError
Or you can use the features separately but all together for the same effect.
var type = require("typist");
var makeArray = type(Array, function(input, foo, bar) {
type.checks([Array, input], [String, foo], [Number, bar]);
input.push(foo, bar);
return input;
});
makeArray([], 1, 2); // throws TypistError
makeArray([], "1", 2); // ["1", 2]
Return Values
Typist creates a curried function of your type and will check against that type any time a value is returned from it. Ensure your function will always return what you expect it to.
var type = require("typist");
var makeArray = type(Array, function(input) {
return input.push("Foo");
});
makeArray("string"); // throws TypistError
makeArray([]); // ["Foo"]
Any Value
Type check any value any time
var type = require("typist");
type.is.array(["Foo"]); // true
type.is.string(["Bar"]); // false
type.array(["Foo"]); // ["Foo"]
type.array("Bar"); // throws TypistError
You can also type check many values, which is useful for checking all your function arguments at the top of the function.
var type = require("typist");
var makeArray = function(input, foo, bar) {
type.checks([Array, input], [String, foo], [Number, bar]);
input.push(foo, bar);
return input;
});
makeArray([], 1, 2); // throws TypistError
makeArray([], "1", 2); // ["1", 2]
Currently Supporting
I'm adding more types as I have time, but currently have:
- Object
- Array
- String
- Number
- Function
- Boolean
- Date
- RegEpx
- Error
Testing
You will need to install grunt-cli
if you haven't already.
Run grunt test
to run tests and coverage. Coverage is saved in coverage
folder.