forked from biocad-cloud/data.ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivator.ts
115 lines (98 loc) · 3.96 KB
/
Activator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* object creator helper module
*/
module Activator {
/**
* MetaReader对象和字典相似,只不过是没有类型约束,并且为只读集合
*/
export function CreateMetaReader<V>(nameValues: NamedValue<V>[] | IEnumerator<NamedValue<V>>): TypeScript.Data.MetaReader {
return new TypeScript.Data.MetaReader(Activator.CreateObject(nameValues));
}
/**
* @param properties 如果这个属性定义集合是一个object,则应该是一个IProperty接口的字典对象
*/
export function CreateInstance(properties: object | NamedValue<IProperty>[]): any {
let target: object = Object.create({});
let propertyInfo: IProperty;
if (Array.isArray(properties)) {
for (let property of <NamedValue<IProperty>[]>properties) {
propertyInfo = property.value;
Object.defineProperty(target, property.name, <PropertyDescriptor>{
get: propertyInfo.get,
set: propertyInfo.set
});
}
} else {
for (let name in <object>properties) {
propertyInfo = properties[name];
Object.defineProperty(target, name, <PropertyDescriptor>{
get: propertyInfo.get,
set: propertyInfo.set
});
}
}
return target;
}
/**
* 利用一个名称字符串集合创建一个js对象
*
* @param names object的属性名称列表
* @param init 使用这个函数为该属性指定一个初始值
*/
export function EmptyObject<V>(names: string[] | IEnumerator<string>, init: (() => V) | V): object {
var obj: object = {};
if (typeof init == "function") {
// 通过函数来进行初始化,则每一个属性值可能会不一样
// 例如使用随机数函数来初始化
let create: Delegate.Func<V> = <any>init;
if (Array.isArray(names)) {
names.forEach(name => obj[name] = create());
} else {
names.ForEach(name => obj[name] = create());
}
} else {
// 直接是一个值的时候,则所有的属性值都是一样的
if (Array.isArray(names)) {
names.forEach(name => obj[name] = <V>init);
} else {
names.ForEach(name => obj[name] = <V>init);
}
}
return obj;
}
/**
* 从键值对集合创建object对象,键名或者名称属性会作为object对象的属性名称
*/
export function CreateObject<V>(nameValues: NamedValue<V>[] |
IEnumerator<NamedValue<V>> |
MapTuple<string, V>[] |
IEnumerator<MapTuple<string, V>>): object {
let obj: object = {};
let type = TypeScript.Reflection.$typeof(nameValues);
if (type.isArray && type.class == "MapTuple") {
(<MapTuple<string, V>[]>nameValues).forEach(map => obj[map.key] = map.value);
} else if (type.isArray && type.class == "NamedValue") {
(<NamedValue<V>[]>nameValues).forEach(nv => obj[nv.name] = nv.value);
} else if (type.class == "IEnumerator") {
var seq = <IEnumerator<any>>nameValues;
type = seq.ElementType;
if (type.class == "MapTuple") {
(<IEnumerator<MapTuple<string, V>>>nameValues)
.ForEach(map => {
obj[map.key] = map.value;
});
} else if (type.class == "NamedValue") {
(<IEnumerator<NamedValue<V>>>nameValues)
.ForEach(nv => {
obj[nv.name] = nv.value;
});
} else {
console.error(type);
throw `Unsupport data type: ${type.class}`;
}
} else {
throw `Unsupport data type: ${JSON.stringify(type)}`;
}
return obj;
}
}