Uni-Logo

TreatJS Sample Applications

back to TreatJS

Base Contracts

(TreatJS 1.x)

function typeOfNumberFunction (arg) {
  return (typeof arg) === 'number';
};
var typeOfNumber = Contract.Base(typeOfNumberFunction);

Run Online

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;
});

Run Online

NonEmpty = Contract.Base(function (arg) {
  return !(arg.length==0);
});
NotEmpty = Contract.Contract.Not(Contract.Base(function (arg) {
  return (arg.length==0);
}));

Run Online

Function Contracts

(TreatJS 1.x)

function cmpUnchecked(x, y) {
  return (x > y);
}
var cmp = Contract.assert(cmpUnchecked,
  Contract.SFunction(typeOfNumber, typeOfNumber, typeOfBoolean)
);

Run Online

var sortNumbers = Contract.SFunction(
  instanceOfArray, Contract.SFunction(
    typeOfNumber, typeOfNumber, typeOfBoolean
  ), instanceOfArray);

Run Online

var funNumNumBool = 
  Contract.AFunction([typeOfNumber, typeOfNumber], typeOfBoolean);

Run Online

var LengthTwo = Contract.Base(function (args) {
  return (args.length == 2);
});
var TwoArgs = Contract.Function(LengthTwo, Any);

Run Online

Method Contracts

(TreatJS 1.x)

var foreachSpec = Contract.AMethod(
  [Contract.SFunction(typeOfNumber, Any, isUndefined)],
  isUndefined,
  instanceOfArray);

Run Online

Object Contracts

(TreatJS 1.x)

var lentypeOfNumber = Contract.Object(Contract.StringMap({length:typeOfNumber}));

Run Online

var lentypeOfNumber = Contract.AObject({length:typeOfNumber});

Run Online

var funNumNumBool2 = 
  Contract.Function(
    Contract.AObject([typeOfNumber, typeOfNumber]),
    typeOfBoolean
);

Run Online

var arraySpec = Contract.AObject({
  length:typeOfNumber,
  foreach:Contract.SFunction(
    Contract.SFunction(typeOfNumber, Any, isUndefined),
    isUndefined
)});

Run Online

var arraySpec = Contract.AObject(Contract.RegExpMap([
  Contract.Mapping(/get(^Day+Month+Year$)/,
    Contract.SFunction(Any, typeOfNumber))]))

Run Online

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));

Run Online

var funNumNumBool-twoargs =
  Contract.And (TwoArgs, funNumNumBool);

Run Online

var cmpNumOrString = Contract.Or(
  funNumNumBool, 
  Contract.SFunction(
    typeOfString, typeOfString, typeOfBoolean
  ));

Run Online

var numGreater = Contract.Not(
  Contract.SFunction(typeOfNumber, Contract.Not(GreaterThanZero)
));

Run Online

var numGreater2 = Contract.Or(
  Contract.SFunction(typeOfNumber, GreaterThanZero),
  Contract.SFunction(Contract.Not(typeOfNumber), Any)
);

Run Online

var numGreater = Contract.Not(
  Contract.Function(typeOfNumber, Contract.Not(GreaterThanZero)
));

Run Online

var numGreater2 = Contract.Or(
  Contract.Function(typeOfNumber, GreaterThanZero),
  Contract.Function(Contract.Not(typeOfNumber), Any)
);

Run Online

Contract.With Contracts

(TreatJS 1.x)

var instanceOfArrayComplete = Contract.With({Array:Array}, instanceOfArray);

Run Online

function withEffects(arg) {
  influencedVariable = arg;
  return true;
}
var InjuringCheck = Contract.With({withEffects:withEffects},
  Contract.Base(function (arg) {
    return withEffects(arg);
}));

Run Online

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();

Run Online

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);
}

Run Online

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);
}

Run Online

var LinkedListCtor = Contract.Constructor(function ctorFun() {
  return Contract.Object({
    val:typeOfNumber,
    next:Contract.Constructor(fun)
  });
});

Run Online

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);

Run Online

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));

Run Online