compiletime
v0.0.5
Published
Compile time JavaScript checks, asserts that an Object implements an Interface, and that attributes have expected data types.
Downloads
3
Readme
compiletime
Checks that can be made to JS code before deploying it.
Not necessarily "compile time" checks but just type checks I feel the lack of when writing JS.
Designed to be easy to remove the asserts from code before deploying.
Always use ct. as the variable so we can grep for it. Always use var ct = require( so we can grep for that too.
var ct = require('../lib/compiletime.js').ct;
The idea of the interfaces is to try to detect at "compile time" that problems exist, JavaScript does not really have the concept of compile time so here we mock it with a boolean flag.
This might be defined in a far off /lib somewhere.
var Car {
doors : 0,
go : function() {}
};
Here we define constructing a Car that does not technicall inherit Car, we could use nodes inherit, or mixins or whatever JS trick we like.
var OldBanger = function() {
this.doors = 4;
}
OldBanger.prototype.go = function() {
return "Phut Phut Bang!";
};
// This line's job is simply to blow up if Car has changed incompatably
ct.implements(OldBanger, Car);
In this case ct will only blow up when new SomeThing() is called
var SomeThing = function(car) {
ct.implements(car, Car);
this.car = car;
};
Of couse if you use semver and don't pump versions and ensure backwards compatiability compiletime is not needed.
If you are rattling out code fast plan to change a lot before sharing it compiletime is handy.
To delete all the ct. asserts in code run
compiletime-off.sh some.js
This parses your code looking for "ct." as a string! it will delete stuff from you code, backup or commit first.
You can also run compiletime-off.sh
on code to be deployed or so you don't have a runtime dependency on compiletime.