-
Notifications
You must be signed in to change notification settings - Fork 0
js-ts #1
Copy link
Copy link
Open
Description
/*
* @parma errorPrefix {string} - 错误消息前缀
* @parma data {object}
* @parma data.v {any}
* @parma data.type {string} eg: "string" "number" "array" "boolean" "object"
**/
function assertType(errorPrefix, data) {
switch(data.type) {
case "array":
if (data.v.constructor !== Array) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be Array")
}
break
case 'number':
if (data.v.constructor !== Number) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be Number")
}
break
case "object":
if (data.v.constructor !== Object) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be Object")
}
break
case "string":
if (data.v.constructor !== String) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be String")
}
break
case "boolean":
if (data.v.constructor !== Boolean) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be Boolean")
}
break
default:
throw new Error("typejson:assertType(data) " + data.type + " not find")
}
}
assertType("typejson:", {
v: 0,
type: "array",
})interface iAssertTypeData {
v :any
type: AssertTypeDataType
}
enum AssertTypeDataType {
String = "String",
Number = "Number",
Boolean = "Boolean",
Array = "Array",
Object = "Object",
}
interface AssertTypeDataTypeSwitchHandle {
String() :void
Number() :void
Boolean() :void
Array() :void
Object() :void
}
function AssertTypeDataTypeSwitch(type :AssertTypeDataType, handle: AssertTypeDataTypeSwitchHandle) {
switch (type) {
case AssertTypeDataType.Array:
handle.Array()
break
case AssertTypeDataType.Boolean:
handle.Boolean()
break
case AssertTypeDataType.Number:
handle.Number()
break
case AssertTypeDataType.Object:
handle.Object()
break
case AssertTypeDataType.String:
handle.String()
break
default:
throw new Error("typejson:assertType(data) AssertTypeDataTypeSwitch " + type + " not find")
}
}
function assertType(errorPrefix: string, data: iAssertTypeData) {
AssertTypeDataTypeSwitch(data.type, {
Array(): void {
if (data.v.constructor !== Array) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be Array")
}
},
Boolean(): void {
if (data.v.constructor !== Boolean) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be Boolean")
}
},
Number(): void {
if (data.v.constructor !== Number) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be Number")
}
},
Object(): void {
if (data.v.constructor !== Object) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be Object")
}
},
String(): void {
if (data.v.constructor !== String) {
throw new Error(errorPrefix + "can not be " + data.v.constructor.name + ", must be String")
}
}
})
}
assertType("typejson ", {
v: 0,
type: AssertTypeDataType.Array
})Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels