typeproto
A prototype handling javascript library. Features: classes, enums and traits.
How to use
In browser:
Copy the file lib/typeproto.js
to your web server. Then, use the global tp
object.
In node.js:
Type into the terminal: npm install typeproto
. Then, use the typeproto
module.
Reference
tp.Class
Type
function/pseudo-constructor
Usage
// In node.js, uncomment the line below
// var tp = require("typeproto");
var MyClass = tp.Class({
//options
});
Options reference
parent
(optional)
type: function/constructor (optional)
The class's parent
constructor
(optional)
type: function (optional)
The class's constructor function
prototype
(optional)
type: object (optional)
The prototype's properties. The constructor of the class can be passed as a property of this object.
statics
(optional)
type: object (optional)
The static members of the class.
objects especial members
__super__
A function that calls the parent's constructor on the current object. Only exists if the class has a parent. Example:
// In node.js, uncomment the line below
// var tp = require("typeproto");
var Parent = tp.Class({
constructor: function (name) {
this.name = name;
},
});
var Child = tp.Class({
constructor: function () {
this.__super__("Joe");
},
});
tp.Trait
Type
function/constructor
Usage
// In node.js, uncomment the line below
// var tp = require("typeproto");
var MyTrait = new tp.Trait("MyTrait", {
//methods
});
MyTrait.implementFor(someObj, {
//methods
});
console.log(Stack.validate(someObj));
Class Reference
tp.Trait.prototype.constructor
Arguments: (name: string, methods: object)
Example Usage
// In node.js, uncomment the line below
// var tp = require("typeproto");
var Stack = new tp.Trait("Stack", {
push: null, //abstract method
pop: null, //abstract method
truncate: function () {
while (this.pop() !== undefined);
}, // default method
});
tp.Trait.prototype.methods
type: map-like object
tp.Trait.prototype.name
type: string
tp.Trait.prototype.fromParent
Arguments: (parent: tp.Trait)
Return value: tp.Trait (this)
Inherits from a parent trait
tp.Trait.prototype.validate
Arguments: (tested: object)
Return value: boolean
Tests if an object implements the trait
tp.Trait.prototype.implementFor
Arguments: (implemented: object, methods: object)
Return value: object (implemented)
Implements the default methods and the passed methods via argument. Doesn't override the object's methods.
tp.Enum
Type
function/constructor
Usage
// In node.js, uncomment the line below
// var tp = require("typeproto");
var MyEnum = new tp.Enum({
//values
});
console.log(MyEnum.validate(MyEnum.SOME_VALUE));
Class Reference
tp.Enum.prototype.constructor
Arguments: (values: object)
Example Usage
// In node.js, uncomment the line below
// var tp = require("typeproto");
var Direction = new tp.Enum({
LEFT: {},
CENTER: {},
RIGHT: {},
});
console.log(Direction.LEFT);
tp.Enum.prototype.keyOf
Arguments: (value: object)
Return value: string
tp.Enum.prototype.validate
Arguments: (value: object)
Return value: boolean (whether value is part of the enum or not)
tp.Enum.prototype.eq
Arguments: (x: object, y: object)
Return value: boolean (whether the objects are the same for the enum)
tp.Enum.prototype.keys
Arguments: ()
Return value: array (of string)
Serialization-proof
// In node.js, uncomment the line below
// var tp = require("typeproto");
var Direction = new tp.Enum({
LEFT: {},
CENTER: {},
RIGHT: {},
});
Direction.eq(JSON.parse(JSON.stringify(Direction.LEFT)), Direction.LEFT);
Full example:
var assert = require("assert");
var tp = require("./");
var GrandParent = new tp.Class({
prototype: {
field: 5,
},
});
var Parent = new tp.Class({
parent: GrandParent,
constructor: function (field) {
this.field = field;
},
});
var Child = new tp.Class({
parent: Parent,
prototype: {
constructor: function () {
this.__super__(3);
},
sayHello: function () {
return Child.MESSAGE + this.field;
},
},
statics: {
MESSAGE: "My field is ",
},
});
assert(!(new Parent instanceof Child));
assert(new Child instanceof Child);
assert(new Child instanceof Parent);
assert(new Child instanceof GrandParent);
assert(new Child().sayHello() == "My field is 3");
var MyEnum = new tp.Enum({
FOO: {
toString: function () {
return "Foo";
},
},
BAR: {
toString: function () {
return "Bar";
},
},
});
assert(MyEnum.validate(MyEnum.FOO));
assert(!MyEnum.validate({}));
assert(MyEnum.eq(MyEnum.FOO, MyEnum.FOO));
assert(!MyEnum.eq(MyEnum.BAR, MyEnum.FOO));
assert.deepEqual(MyEnum.keys(), ["FOO", "BAR"]);
var ToString = new tp.Trait("ToString", {
toString: null,
});
var Encodable = new tp.Trait("Encodable", {
encode: null,
asEncodedArray: function () {
return this.encode().split().map(function (val) {
return val.charCodeAt(0);
});
},
}).fromParent(ToString);
Encodable.implementFor(Child.prototype, {
encode: function () {
return String.fromCharCode(this.field);
},
});
assert(Encodable.validate(new Child));
assert(!Encodable.validate(new Parent));
console.log("Success")