-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (49 loc) · 1.34 KB
/
index.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
exports = module.exports;
exports.baseUrl = '';
exports.pathArray = [];
exports.init = (baseUrl) =>
{
this.pathArray = [];
this.baseUrl = baseUrl;
return this;
};
exports.addToPath = (entity, id = null) => {
if (entity) {
this.pathArray.push(entity);
if (id) {
this.pathArray.push(id);
}
}
return this;
};
exports.build = () => {
if (this.baseUrl.slice(-1) === '/') {
this.baseUrl = this.baseUrl.substring(0, this.baseUrl.length - 1);
}
return this.baseUrl + this.pathArray.map(value => `/${value}`).join('')
};
exports.urlToPath = (url) =>
{
const splitProtocol = url.split('//');
let splitUrl;
if(splitProtocol && splitProtocol.length > 1) {
splitUrl = splitProtocol[1].split('/')
} else if(splitProtocol && splitProtocol.length > 0) {
splitUrl = splitProtocol[0].split('/')
}
if(splitUrl && splitUrl.length>0) {
this.baseUrl = splitUrl[0];
this.pathArray = splitUrl.slice(1);
}
return this;
};
exports.getEntityId = (entity) => {
if(this.pathArray && this.pathArray.length>0) {
const entityIndex = this.pathArray.indexOf(entity);
if(entityIndex > 0 && entityIndex < this.pathArray.length - 1) {
return this.pathArray[entityIndex+1];
}
return null;
}
return null;
};