A prototype handling javascript library. Features: classes, enums and traits.
Copy the file lib/typeproto.js
to your web server. Then, use the global tp
object.
Type into the terminal: npm install typeproto
. Then, use the typeproto
module.
function/pseudo-constructor
// In node.js, uncomment the line below
// var tp = require("typeproto");
var MyClass = tp.Class({
//options
});
type: function/constructor (optional)
The class's parent
type: function (optional)
The class's constructor function
type: object (optional)
The prototype's properties. The constructor of the class can be passed as a property of this object.
type: object (optional)
The static members of the class.
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");
},
});
function/constructor
// 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));
Arguments: (name: string, methods: object)
// 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
});
type: map-like object
type: string
Arguments: (parent: tp.Trait)
Return value: tp.Trait (this)
Inherits from a parent trait
Arguments: (tested: object)
Return value: boolean
Tests if an object implements the trait
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.
function/constructor
// In node.js, uncomment the line below
// var tp = require("typeproto");
var MyEnum = new tp.Enum({
//values
});
console.log(MyEnum.validate(MyEnum.SOME_VALUE));
Arguments: (values: object)
// In node.js, uncomment the line below
// var tp = require("typeproto");
var Direction = new tp.Enum({
LEFT: {},
CENTER: {},
RIGHT: {},
});
console.log(Direction.LEFT);
Arguments: (value: object)
Return value: string
Arguments: (value: object)
Return value: boolean (whether value is part of the enum or not)
Arguments: (x: object, y: object)
Return value: boolean (whether the objects are the same for the enum)
Arguments: ()
Return value: array (of string)
// 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);
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")