-
Notifications
You must be signed in to change notification settings - Fork 3
/
Candy.js
63 lines (63 loc) · 2.12 KB
/
Candy.js
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
"use strict";
class Candy {
static getPathAlias(alias) {
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 '';
}
static setPathAlias(alias, path) {
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);
}
static deletePathAlias(alias) {
if (64 !== alias.charCodeAt(0)) {
alias = '@' + alias;
}
Candy.pathAliases.delete(alias);
}
static createObject(clazz, ...parameters) {
if ('string' === typeof clazz) {
return Candy.createObjectAsString(clazz, ...parameters);
}
return Candy.createObjectAsDefinition(clazz, ...parameters);
}
static createObjectAsString(classPath, ...parameters) {
let ClassName = Candy.include(classPath, true);
return new ClassName(...parameters);
}
static createObjectAsDefinition(definition, ...parameters) {
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;
}
static include(clazz, isAlias = true) {
let realClass = isAlias ? Candy.getPathAlias('@' + clazz) : clazz;
return require(realClass + Candy.defaultExtension);
}
static configure(object, properties) {
for (let key in properties) {
object[key] = properties[key];
}
return object;
}
}
Candy.app = null;
Candy.pathAliases = new Map([['@candy', __dirname]]);
Candy.defaultExtension = '.js';
module.exports = Candy;