static-type-js
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -5,3 +5,3 @@ { | ||
"description": "Prototype inheritance and type system for js", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -322,3 +322,3 @@ describe('Typejs', function () { | ||
}); | ||
expect(message).toBe('TypeError key: username, value: "number" (1), is expected to be: "string" type.'); | ||
expect(message).toBe('key: username, value: "number" (1), is expected to be: "string" type.'); | ||
message = tryCatch(function () { | ||
@@ -332,11 +332,11 @@ us.username = null | ||
}); | ||
expect(message).toBe('TypeError key: username, value: "function" (function () { }), is expected to be: "string" type.'); | ||
expect(message).toBe('key: username, value: "function" (function () { }), is expected to be: "string" type.'); | ||
message = tryCatch(function () { | ||
us.username = new RegExp; | ||
}); | ||
expect(message).toBe('TypeError key: username, value: "regexp" (/(?:)/), is expected to be: "string" type.'); | ||
expect(message).toBe('key: username, value: "regexp" (/(?:)/), is expected to be: "string" type.'); | ||
message = tryCatch(function () { | ||
us.date = 1; | ||
}); | ||
expect(message).toBe('TypeError key: date, value: "number" (1), is expected to be: "date" type.'); | ||
expect(message).toBe('key: date, value: "number" (1), is expected to be: "date" type.'); | ||
@@ -398,3 +398,3 @@ message = tryCatch(function () { | ||
}); | ||
expect(message).toBe('TypeError key: one, value: "string" (test), is expected to be: "number" type.'); | ||
expect(message).toBe('key: one, value: "string" (test), is expected to be: "number" type.'); | ||
}); | ||
@@ -435,2 +435,35 @@ | ||
it('setter should al ways be correct type', function () { | ||
var message = tryCatch(function () { | ||
Type.create({ | ||
username: Type.STIRNG, | ||
date: "invalid" | ||
}, { | ||
_construct: function (username) { | ||
this.username = username; | ||
this.date = new Date; | ||
} | ||
}); | ||
}); | ||
expect(message).toBe('Type must be valid type, provided is: invalid'); | ||
var message = tryCatch(function () { | ||
Type.create({ | ||
username: Type.STIRNG, | ||
date: "null" | ||
}, { | ||
_construct: function (username) { | ||
this.username = username; | ||
this.date = new Date; | ||
} | ||
}); | ||
}); | ||
expect(message).toBe('type cannot be:null'); | ||
}); | ||
xit('benchmark group', function () { | ||
@@ -437,0 +470,0 @@ var Test = Type.create({ |
56
type.js
@@ -29,7 +29,4 @@ (function (root, factory) { | ||
return function () { | ||
var tmp = this._super, ret; | ||
this._super = _super[name]; | ||
ret = fn.apply(this, arguments); | ||
this._super = tmp; | ||
return ret; | ||
return fn.apply(this, arguments); | ||
}; | ||
@@ -119,13 +116,11 @@ } | ||
*/ | ||
Type.defineProperty = function (iType, obj, key) { | ||
var type = iType; | ||
Type.defineProperty = function (type, obj, key) { | ||
if (!Type.isValidType(type)) { | ||
throw new Error('Type must be valid type, provided is: ' + type); | ||
} | ||
Object.defineProperty(obj, key, { | ||
set: function (nVal) { | ||
//console.log(this); | ||
// if initial value is undefined or null | ||
if (!type && Type.isInitialized(nVal)) { | ||
type = Type.getType(nVal); | ||
// assert type | ||
} else if (Type.isInitialized(nVal) && !Type.assert(type, nVal)) { | ||
throw new TypeError('TypeError key: ' + key + ', value: "' + Type.getType(nVal) + '" (' + nVal + '), is expected to be: "' + type + '" type.'); | ||
if (Type.isInitialized(nVal) && !Type.assert(type, nVal)) { | ||
throw new TypeError('key: ' + key + ', value: "' + Type.getType(nVal) + '" (' + nVal + '), is expected to be: "' + type + '" type.'); | ||
} | ||
@@ -146,6 +141,9 @@ this.__dynamic__[key] = nVal; | ||
Type.BOOLEAN = "boolean"; | ||
Type.FUNCTION = "function"; | ||
Type.DATE = "date"; | ||
// init | ||
Type.UNDEFINED = "undefined"; | ||
Type.NULL = "null"; | ||
Type.FUNCTION = "function"; | ||
Type.DATE = "date"; | ||
/** | ||
@@ -159,2 +157,30 @@ * @since 0.0.1 | ||
*/ | ||
Type.isValidType = function isValidType(type) { | ||
switch (type) { | ||
case Type.OBJECT: | ||
case Type.STIRNG: | ||
case Type.ARRAY: | ||
case Type.REGEX: | ||
case Type.NUMBER: | ||
case Type.BOOLEAN: | ||
case Type.FUNCTION: | ||
case Type.DATE: | ||
return true; | ||
break; | ||
case Type.UNDEFINED: | ||
case Type.NULL: | ||
throw new Error('type cannot be:' + type); | ||
break; | ||
} | ||
return false; | ||
}; | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Type#assert | ||
* | ||
* @description | ||
* Assert type | ||
*/ | ||
Type.assert = function (type, value) { | ||
@@ -161,0 +187,0 @@ return type === Type.getType(value); |
33003
806