TreatJS Sample Applications
back to TreatJS
- Base Contracts
- Function Contracts
- Method Contracts
- Object Contracts
- Combinator Contracts
- Contract.With Contracts
- Contract Contract.Constructors
- Dependent Contracts
Base Contracts
(TreatJS 1.x)
function typeOfNumberFunction (arg) {
return (typeof arg) === 'number';
};
var typeOfNumber = Contract.Base(typeOfNumberFunction);
var typeOfBoolean = Contract.Base(function (arg) {
return (typeof arg) === 'boolean';
});
var typeOfString = Contract.Base(function (arg) {
return (typeof arg) === 'string';
});
var instanceOfArray = Contract.Base(function (arg) {
return (arg instanceof Array);
});
var isUndefined = Contract.Base(function (arg) {
return (arg === undefined);
});
var Any = Contract.Base(function (arg) {
return true;
});
NonEmpty = Contract.Base(function (arg) {
return !(arg.length==0);
});
NotEmpty = Contract.Contract.Not(Contract.Base(function (arg) {
return (arg.length==0);
}));
Function Contracts
(TreatJS 1.x)
function cmpUnchecked(x, y) {
return (x > y);
}
var cmp = Contract.assert(cmpUnchecked,
Contract.SFunction(typeOfNumber, typeOfNumber, typeOfBoolean)
);
var sortNumbers = Contract.SFunction(
instanceOfArray, Contract.SFunction(
typeOfNumber, typeOfNumber, typeOfBoolean
), instanceOfArray);
var funNumNumBool =
Contract.AFunction([typeOfNumber, typeOfNumber], typeOfBoolean);
var LengthTwo = Contract.Base(function (args) {
return (args.length == 2);
});
var TwoArgs = Contract.Function(LengthTwo, Any);
Method Contracts
(TreatJS 1.x)
var foreachSpec = Contract.AMethod(
[Contract.SFunction(typeOfNumber, Any, isUndefined)],
isUndefined,
instanceOfArray);
Object Contracts
(TreatJS 1.x)
var lentypeOfNumber = Contract.Object(Contract.StringMap({length:typeOfNumber}));
var lentypeOfNumber = Contract.AObject({length:typeOfNumber});
var funNumNumBool2 =
Contract.Function(
Contract.AObject([typeOfNumber, typeOfNumber]),
typeOfBoolean
);
var arraySpec = Contract.AObject({
length:typeOfNumber,
foreach:Contract.SFunction(
Contract.SFunction(typeOfNumber, Any, isUndefined),
isUndefined
)});
var arraySpec = Contract.AObject(Contract.RegExpMap([
Contract.Mapping(/get(^Day+Month+Year$)/,
Contract.SFunction(Any, typeOfNumber))]))
Combinator Contracts
(TreatJS 1.x)
var GreaterThanZero = Contract.Base(function (arg) {
return (arg > 0);
});
var int = Contract.assert(1, Contract.And(typeOfNumber, GreaterThanZero));
var key = Contract.assert('k', Contract.Or(typeOfNumber, typeOfString));
var val = Contract.assert(1, Contract.Not(isUndefined));
var funNumNumBool-twoargs =
Contract.And (TwoArgs, funNumNumBool);
var cmpNumOrString = Contract.Or(
funNumNumBool,
Contract.SFunction(
typeOfString, typeOfString, typeOfBoolean
));
var numGreater = Contract.Not(
Contract.SFunction(typeOfNumber, Contract.Not(GreaterThanZero)
));
var numGreater2 = Contract.Or(
Contract.SFunction(typeOfNumber, GreaterThanZero),
Contract.SFunction(Contract.Not(typeOfNumber), Any)
);
var numGreater = Contract.Not(
Contract.Function(typeOfNumber, Contract.Not(GreaterThanZero)
));
var numGreater2 = Contract.Or(
Contract.Function(typeOfNumber, GreaterThanZero),
Contract.Function(Contract.Not(typeOfNumber), Any)
);
Contract.With Contracts
(TreatJS 1.x)
var instanceOfArrayComplete = Contract.With({Array:Array}, instanceOfArray);
function withEffects(arg) {
influencedVariable = arg;
return true;
}
var InjuringCheck = Contract.With({withEffects:withEffects},
Contract.Base(function (arg) {
return withEffects(arg);
}));
Contract Contract.Constructors
(TreatJS 1.x)
var ctor = Contract.Constructor(function() {
var type = undefined;
var SetType = Contract.Base(function (ary) {
type = typeof ary[0];
return true;
});
var CheckType = Contract.Base(function (arg) {
return type === typeof arg;
});
return Contract.SFunction(
SetType, Contract.SFunction(
CheckType, CheckType, typeOfBoolean
), instanceOfArray);
});
var sortContract = ctor.build();
var NArgsCtor = Contract.Constructor(function(n) {
var LengthN = Contract.Base(function (args) {
return args.length == n;
});
return Contract.Function (LengthN, Any);
});
function NArgs (n) {
return NArgsCtor.build (n);
}
var NArgsCtor = Contract.Constructor(function(n) {
var LengthN = Contract.Base(function (args) {
return args.length == n;
});
return Contract.Function (LengthN, Any);
});
function NArgs (n) {
return NArgsCtor.build (n);
}
var LinkedListCtor = Contract.Constructor(function ctorFun() {
return Contract.Object({
val:typeOfNumber,
next:Contract.Constructor(fun)
});
});
Dependent Contracts
(TreatJS 1.x)
var EqLengthCtor = Contract.Constructor(function(input) {
return Contract.Base(function (result) {
return (input.length == result.length);
});
});
var PreserveLength = Contract.Dependent(EqLengthCtor);
var NotChangedCtor = Contract.Constructor(function() {
var oldvalue = obj[prop];
return Contract.Base(function (arg) {
return (oldvalue === obj[prop]);
});
});
function PropNotChanged (obj, prop) =
Contract.With({obj:obj, prop:prop},
Contract.Dependent(NotChangedCtor));