Skip to content

Instantly share code, notes, and snippets.

@Deltachaos
Created March 27, 2012 20:17
Show Gist options
  • Save Deltachaos/2219922 to your computer and use it in GitHub Desktop.
Save Deltachaos/2219922 to your computer and use it in GitHub Desktop.
StrongTypes for JavaScript
/**
* StrongTypes (http://github.com/fireflyjs/strongtypesjs)
* Copyright 2012, Maximilian Ruta <mr@xtain.net> (http://deltachaos.de/)
*
* This is a Type System for JavaScript. It is desinged to run
* on node.js and in the Browser. Tested with:
* • Chromium 15
* • Firefox 8
* • Opera 11.52
* • Internet Explorer 9
*
* @license XTAIN Public License (http://foundation.xtain.net/licenses/xpl.html)
* @copyright Copyright 2012, Maximilian Ruta <mr@xtain.net> (http://deltachaos.de/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the XTAIN Public License as
* published by the XTAIN Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the XTAIN Public License
* along with this program. If not, see <http://foundation.xtain.net/licenses/xpl.html>.
*/
(function StrongTypes(undefined) {
var StrongTypes = this;
/* ===========================================
# Basic Types
=========================================== */
// # Boolean
this.Boolean = function(n) {
Boolean.call(n);
if(!(n === true || n === false)) {
throw new BooleanFormatException();
}
return Boolean(n);
};
this.Boolean.prototype = new Boolean();
// # String
/**
*
* @throws {StringFormatException}
*/
this.String = function(n) {
String.call(n);
if(typeof n !== "string") {
throw new StringFormatException();
}
return String(n);
};
this.String.prototype = new String();
// # Function
this.Function = function(n) {
//Function.call(this);
if(typeof n != "function") {
throw new FunctionFormatException();
}
return n;
};
this.Function.prototype = new Function();
// # Object
this.Object = function(n) {
Object.call(n);
if(typeof n !== "object" || (Array.isArray && Array.isArray(n)) || n instanceof Array) {
throw new ObjectFormatException();
}
return n;
};
this.Object.prototype = new Object();
// # Array
this.Array = function(n) {
Array.call(n);
if(typeof n !== "object" || !((Array.isArray && Array.isArray(n)) || n instanceof Array)) {
throw new ArrayFormatException();
}
return n;
};
this.Array.prototype = new Array();
// # Integer
function Integer(n) {
Number.call(this);
if(typeof n != "number" || n % 1 !== 0) {
throw new IntegerFormatException();
}
return Number(n);
};
Integer.prototype = new Number();
this.Integer = Integer;
// # Double
function Double(n) {
Number.call(this);
if(typeof n != "number") {
throw new DoubleFormatException();
}
return Number(n);
};
Double.prototype = new Number();
this.Double = Double;
// # Struct
function Struct(n) {
n = StrongTypes.Object(n);
var StructFunction = function(values) {
values = StrongTypes.Object(values);
var vars = StructFunction.prototype.vars;
var createdObject = new Object();
for(var i in values) {
var value = values[i];
if(typeof vars[i] !== "undefined") {
createdObject[i] = new vars[i](value);
} else {
//throw new
}
}
console.log(StructFunction.prototype.vars);
console.log(vars);
};
StructFunction.prototype.vars = n;
return StructFunction;
}
this.Struct = Struct;
/* ===========================================
# Basic Classes declaration
=========================================== */
// # Exception
function Exception(message, type) {
Error.apply(this, arguments);
this.parents = new Array();
var firstClass = Exception;
do {
this.parents.push(firstClass);
firstClass = firstClass.caller;
} while(firstClass.caller !== null && firstClass.caller.prototype instanceof Exception);
this.name = firstClass.name;
if(this.name === undefined) {
this.name = /function[\s]*([a-zA-Z0-9]+?)\(/.exec(firstClass.toString())[1];
}
this.name = StrongTypes.String(this.name);
this.message = String(message);
if(type !== undefined) {
this.type = Integer(type);
if (this.Types !== undefined) {
this.type = new this.Types(this.type);
}
}
if(Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
} else {
this.toString = function ExceptionToString() {
return StrongTypes.String(this.name) + ': ' + StrongTypes.String(this.message);
};
}
};
Exception.prototype = new Error();
Exception.prototype.constructor = Exception;
this.Exception = Exception;
/*
// Exapmles:
function TestException(message, code) {
Exception.apply(this, arguments);
};
TestException.prototype = new Exception();
function TestException2(message, code) {
TestException.apply(this, arguments);
};
TestException2.prototype = new TestException();
var e = new TestException2("sfsdf");
console.log(e instanceof Types.Exception);
throw e;
*/
// # Enum
function Enum(enumValue) {
if(typeof enumValue == "object") {
}
var enumCounter = enumValue;
var enumData = new Object();
var enums = new Array();
var enumKeys = new Object();
for(var i in this) {
try {
this[i] = Integer(this[i]);
} catch(e) {
switch(true) {
case e instanceof IntegerFormatException:
continue;
break;
default:
throw e;
}
}
enums.push(this[i]);
enumKeys[this[i]] = i;
Enum[i] = this[i];
}
enums.sort();
enums.reverse();
var enumObj = new Object();
for(var i in enums) {
var key = enumKeys[enums[i]];
var value = enums[i];
this[key] = false;
if(value <= enumCounter) {
this[key] = true;
enumCounter = enumCounter - value;
}
}
this.enumData = enumData;
this.enumKeys = enumKeys;
};
Enum.prototype.get = function EnumGet() {
var value = 0;
for(var i in this.enumData) {
try {
this.enumData[i] = Integer(this.enumData[i]);
} catch(e) { switch(true) {
case e instanceof IntegerFormatException:
continue;
break;
default:
throw e;
} }
if(this[i]) {
value = value + this.enumData[i];
}
}
return value;
};
Enum.prototype.check = function EnumCheck(value) {
if(this.enumKeys[value] === undefined) {
throw new EnumCheckException();
}
return this[this.enumKeys[value]];
};
this.Enum = Enum;
/* ===========================================
# Basic Exceptions
=========================================== */
// # StrongTypingException
function StrongTypingException(message, code) {
Exception.apply(this, arguments);
};
StrongTypingException.prototype = new Exception();
this.StrongTypingException = StrongTypingException;
// # FormatException
function FormatException(message, code) {
if(message === undefined) {
message = "Input var was not in a correct format";
}
StrongTypingException.apply(this, arguments);
};
FormatException.prototype = new StrongTypingException();
this.FormatException = FormatException;
// # FunctionFormatException
function FunctionFormatException(message, code) {
FormatException.call(this, message, code);
};
FunctionFormatException.prototype = new FormatException();
this.FunctionFormatException = FunctionFormatException;
// # IntegerFormatException
function IntegerFormatException(message, code) {
FormatException.call(this, message, code);
};
IntegerFormatException.prototype = new FormatException();
this.IntegerFormatException = IntegerFormatException;
// # DoubleFormatException
function DoubleFormatException(message, code) {
FormatException.call(this, message, code);
};
DoubleFormatException.prototype = new FormatException();
this.DoubleFormatException = DoubleFormatException;
// # BooleanFormatException
function BooleanFormatException(message, code) {
FormatException.call(this, message, code);
};
BooleanFormatException.prototype = new FormatException();
this.BooleanFormatException = BooleanFormatException;
// # StringFormatException
function StringFormatException(message, code) {
FormatException.call(this, message, code);
};
StringFormatException.prototype = new FormatException();
this.StringFormatException = StringFormatException;
// # ObjectFormatException
function ObjectFormatException(message, code) {
FormatException.call(this, message, code);
};
ObjectFormatException.prototype = new FormatException();
this.ObjectFormatException = ObjectFormatException;
// # ArrayFormatException
function ArrayFormatException(message, code) {
FormatException.call(this, message, code);
};
ArrayFormatException.prototype = new FormatException();
this.ArrayFormatException = ArrayFormatException;
// # EnumCheckException
function EnumCheckException(message, code) {
if(message === undefined) {
message = "Value dose not exists in Enum";
}
StrongTypingException.call(this, message, code);
};
EnumCheckException.prototype = new StrongTypingException();
this.EnumCheckException = EnumCheckException;
// # StructCheckException
function StructCheckException(message, code) {
if(message === undefined) {
message = "Value dose not exists in Enum";
}
StrongTypingException.call(this, message, code);
};
StructCheckException.prototype = new StrongTypingException();
this.StructCheckException = StructCheckException;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment