|
本帖最后由 Nameker 于 2020-11-16 06:18 编辑
Route = {
Router:{},
find:function(link)
{
let path = 'Route.Router';
let parent = null;
let key = null;
if (link) {
route = link.split('.')
route.forEach(function(keys,index,array){
const regex = /([a-zA-Z0-9\u4e00-\u9fa5]+)?([?<=\[](\d+)[?=\]])?/gm;
if (regexdata = regex.exec(keys)) {
if (typeof regexdata[1] != 'undefined') {
if (index == array.length - 1) {
parent = path;
key = regexdata[1];
}
path += `[${JSON.stringify(regexdata[1])}]`
Class = new Function('return Object.prototype.toString.call('+path+')')()
switch(Class)
{
case '[object Object]':
break;
case '[object Array]':
break;
case '[object String]':
break;
case '[object Undefined]':
new Function('return '+ path + ' = {}')()
break;
default:
break;
}
}
if (typeof regexdata[3] != 'undefined') {
if (index == array.length - 1) {
parent = path;
key = regexdata[3];
}
path += `[${parseInt(regexdata[3])}]`
Class = new Function('return Object.prototype.toString.call('+path+')')()
switch(Class)
{
case '[object Object]':
break;
case '[object Array]':
break;
case '[object String]':
break;
case '[object Undefined]':
new Function('return '+ path + ' = {}')()
break;
default:
break;
}
}
}
})
} else {
parent = path;
key = parent;
Class = new Function('return Object.prototype.toString.call('+path+')')()
switch(Class)
{
case '[object Object]':
break;
case '[object Array]':
break;
case '[object String]':
new Function('return '+ path + ' = {}')()
break;
case '[object Undefined]':
new Function('return '+ path + ' = {}')()
break;
default:
break;
}
}
let parenttype = new Function('return Object.prototype.toString.call('+parent+')')()
let type = new Function('return Object.prototype.toString.call('+path+')')()
return {'parent':parent,'parenttype':parenttype,'path':path,'key':key,'type':type}
},
load:function(link,data)
{
route = this.find(link || '');
new Function(route.path + ' = ' + JSON.stringify(data))()
},
free:function(link)
{
route = this.find(link || '');
remove = function(route,type,keys)
{
if (route.parenttype=='[object Array]')
{
new Function('route.parent.splice('+route.keys+',1)')
} else if(route.parenttype=='[object Object]')
{
new Function('delete ' + route.path)()
}
}
new Function('remove('+JSON.stringify(route)+')')()
},
route:function(link,data)
{
route = this.find(link || '');
if (Object.prototype.toString.call(data) == '[object Undefined]') {
value = new Function( 'return ' + route.path )()
if (Object.prototype.toString.call(value) == '[object Object]' || Object.prototype.toString.call(value) == '[object Array]') {
return JSON.stringify(JSON.stringify(value))
} else {
return value
}
} else {
new Function(route.path + ' = ' + JSON.stringify(data || {}))()
}
},
count:function(link)
{
route = this.find(link || '');
count = function (data) {
i = 0;
for(keys in data){
i++;
}
return i;
}
return new Function( 'return count(' + route.path+')' )()
},
}
console.log(Route.route('',''))
console.log(Route.route())
Route.route('this.data',[])
Route.route('this.data[0]','逸豪方明')
Route.route('this.data[1]','逸豪方明')
Route.route('this.data[2]','逸豪方明')
Route.route('this.data[3]','逸豪方明')
console.log(Route.route())
.版本 2
.支持库 spec
.程序集 Route, , 公开
.程序集变量 JsrtRuntime, Jsrt_Runtime
.程序集变量 JsrtContext, Jsrt_Context
.子程序 _初始化, , , 当基于本类的对象被创建后,此方法会被自动调用
.判断开始 (JsrtRuntime.Create () ≠ #JsNoError)
JsrtRuntime.Dispose ()
.switch (JsrtRuntime.CreateContext (JsrtContext) ≠ #JsNoError)
JsrtRuntime.Dispose ()
.switch (JsrtContext.SetCurrentContext () = #JsNoError)
JsrtContext.RunScript (#Route)
.默认
JsrtRuntime.Dispose ()
调试输出 (“XRoute Error!”)
.判断结束
.子程序 _销毁, , , 当基于本类的对象被销毁前,此方法会被自动调用
JsrtRuntime.Dispose ()
.子程序 load, Jsrt_Value, 公开
.参数 link, 文本型, 可空
.参数 data, 文本型, 可空
.局部变量 JsrtValue, Jsrt_Value
.局部变量 Code, 整数型
Code = JsrtContext.RunScript (“Route.load('” + link + “',” + data + “)”, , , JsrtValue)
.if (Code ≠ #JsNoError)
调试输出 (取程序名称 (1), “Error”, Code)
.如果真结束
return (JsrtValue)
.子程序 free, Jsrt_Value, 公开
.参数 link, 文本型, 可空
.参数 data, 文本型, 可空
.局部变量 JsrtValue, Jsrt_Value
.局部变量 Code, 整数型
Code = JsrtContext.RunScript (“Route.free('” + link + “',” + data + “)”, , , JsrtValue)
.if (Code ≠ #JsNoError)
调试输出 (取程序名称 (1), “Error”, Code)
.如果真结束
return (JsrtValue)
.子程序 Route, Jsrt_Value, 公开
.参数 link, 文本型, 可空
.参数 data, 文本型, 可空
.局部变量 JsrtValue, Jsrt_Value
.局部变量 Code, 整数型
.判断开始 (是否为空 (link))
Code = JsrtContext.RunScript (“Route.route()”, , , JsrtValue)
.switch (是否为空 (data))
Code = JsrtContext.RunScript (“Route.route('” + link + “')”, , , JsrtValue)
.默认
Code = JsrtContext.RunScript (“Route.route('” + link + “',” + data + “)”, , , JsrtValue)
.判断结束
.if (Code ≠ #JsNoError)
调试输出 (取程序名称 (1), “Error”, Code)
.如果真结束
return (JsrtValue)
.子程序 count, Jsrt_Value, 公开
.参数 link, 文本型, 可空
.局部变量 JsrtValue, Jsrt_Value
.局部变量 Code, 整数型
Code = JsrtContext.RunScript (“Route.count('” + link + “')”, , , JsrtValue)
.if (Code ≠ #JsNoError)
调试输出 (取程序名称 (1), “Error”, Code)
.如果真结束
return (JsrtValue)
|
-
评分
-
查看全部评分
|