0.1.5 • Published 10 years ago
shapecube v0.1.5
shapecube
add type checks to your javascript functions.
how to build and test
npm test runs all tests. The tests are also a good starting point to look for examples.
npm run build transpiles the es6 files to es5 and runs eslint.
how to use it
First run npm install shapecube and then include shapecube in your project:
import shapecube from "shapecube";
function add(a, b) {
shapecube.check({Number: a}, {Number: b});
return a + b;
}more examples
basic usage
shapecube.check({Number: 1});
shapecube.check({Number: "1"}); // throws an error
shapecube.check({Number: "1", message: "custom error message"}); // throws an error with a custom error messageerror handler
shapecube.config({
errorHandler(err) {
console.log(err);
}
});
shapecube.check({Number: "1"}); // logs error to console instead of throwing itcustom types
var newType = shapecube.createType("newType", {a: shapecube.types.Number, b: shapecube.types.Number}, "newType consists of only numbers");
const a = {a: 1, b: "2"};
shapecube.check({newType: a}); // throws "newType consists of only numbers"shapecube.createType("t", {
a: "Number",
b: {
c: "String",
t: "Array"
}
});
shapecube.check({
t: {
a: 1,
b: {
c:"2",
t:["2"]
}
}
});