-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCandy.ts
162 lines (135 loc) · 3.96 KB
/
Candy.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @author afu
* @license MIT
*/
import IWebApplication from './web/IWebApplication';
/**
* 辅助类
*/
class Candy {
/**
* 应用实例
*/
static app: IWebApplication = null;
/**
* @property {Map<String, String>} pathAliases 路径别名
*/
static pathAliases: Map<string, string> = new Map([['@candy', __dirname]]);
/**
* @property {String} defaultExtension 默认文件扩展名
*/
static defaultExtension: string = '.js';
/**
* 别名路径转换真实路径
*
* @param {String} alias 路径别名
* @return {String} 路径
*/
static getPathAlias(alias: string): string {
// not starts with @
if (64 !== alias.charCodeAt(0)) {
return alias;
}
// 截取开头作为别名
let pos = alias.indexOf('/');
let root = -1 === pos ? alias : alias.substring(0, pos);
if (Candy.pathAliases.has(root)) {
return -1 === pos
? Candy.pathAliases.get(root)
: Candy.pathAliases.get(root) + alias.substring(pos);
}
return '';
}
/**
* 设置路径别名
*
* @param {String} alias 路径别名
* @param {String} path 路径
*/
static setPathAlias(alias: string, path: string): void {
// not starts with @
if (64 !== alias.charCodeAt(0)) {
alias = '@' + alias;
}
// '/'
if (47 === path.charCodeAt(path.length - 1)) {
path = path.substring(0, path.length - 1);
}
Candy.pathAliases.set(alias, path);
}
/**
* 删除路径别名
*
* @param {String} alias 路径别名
*/
static deletePathAlias(alias: string): void {
// not starts with @
if (64 !== alias.charCodeAt(0)) {
alias = '@' + alias;
}
Candy.pathAliases.delete(alias);
}
/**
* 创建对象
*
* @param {any} clazz 以某个别名开头的类全名或类配置
* @param {any} parameters 构造函数参数
* @return {any} 类实例
*/
static createObject<T = any>(clazz: any, ...parameters: any[]): T {
if ('string' === typeof clazz) {
return Candy.createObjectAsString<T>(clazz, ...parameters);
}
return Candy.createObjectAsDefinition<T>(clazz, ...parameters);
}
/**
* 字符串方式创建对象
*
* @param {String} classPath 类路径
* @param {any} parameters 构造函数参数
*/
static createObjectAsString<T = any>(classPath: string, ...parameters: any[]): T {
let ClassName = Candy.include(classPath, true);
return new ClassName(...parameters);
}
/**
* 配置方式创建对象
*
* @param {any} definition 类配置
* @param {any} parameters 构造函数参数
*/
static createObjectAsDefinition<T = any>(definition: any, ...parameters: any[]): T {
let properties = Candy.configure({}, definition);
let ClassName = Candy.include(definition.classPath, true);
let instance = new ClassName(...parameters);
delete properties.classPath;
Candy.configure(instance, properties);
return instance;
}
/**
* 导入一个类文件
*
* @param {String} clazz 类全名
* @param {Boolean} isAlias 是否别名路径
*/
static include(clazz: string, isAlias: boolean = true): any {
let realClass = isAlias ? Candy.getPathAlias('@' + clazz) : clazz;
// 文件不存在抛出异常
// todo
return require(realClass + Candy.defaultExtension);
}
/**
* 对象配置
*
* @param {any} object 需要配置的对象
* @param {any} properties 配置项
* @return 源对象
*/
static configure(object: any, properties: any): any {
for (let key in properties) {
object[key] = properties[key];
}
return object;
}
}
export = Candy;