-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathMixin.js
66 lines (63 loc) · 1.73 KB
/
PathMixin.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
64
65
66
/**
* check, is name correct?
* @param {String} name your name
* @return {RPC} this
*/
function checkName(name) {
if (typeof name !== 'string') {
throw new TypeError('name is not a string');
}
if (!name.length) {
throw new TypeError('name is empty');
}
return this;
}
/**
* check, is module name correct?
* @param {String} name lib.module name
* @return {Array} name splited by dot
*/
function checkModuleName(name) {
const split = name.split('.');
if (split.length !== 2) {
if (split.length !== 1) {
throw new TypeError('invalid name of module, it should be string with two dot notated values');
}
split.unshift('main');
}
return split;
}
/**
* create doted path from action params
* @param {Object} action
* @param {Boolean} [isArray=false] path will be an array, not a string
* @param {Boolean} [addMethod=false] add method or event into a path
* @return {String|Array}
*/
function makePathFromAction(action, isArray = false, addMethod = false) {
if (isArray) {
let path = [];
if (action.lib) path[0] = action.lib + '';
else path[0] = 'main';
path[1] = action.module + '';
if (addMethod) {
if (action.method) path[2] = action.method + '';
else if (action.event) path[2] = action.event + '';
}
return path;
}
let path = '';
if (action.lib) path += action.lib + '.';
path += action.module;
if (addMethod) {
if (action.method) path += '.' + action.method;
else if (action.event) path += '.' + action.event;
}
return path;
}
module.exports = function(Class) {
Class.prototype.checkName = checkName;
Class.prototype.checkModuleName = checkModuleName;
Class.prototype.makePathFromAction = makePathFromAction;
return Class;
};