-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (46 loc) · 1.23 KB
/
index.js
File metadata and controls
54 lines (46 loc) · 1.23 KB
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
/*
** Class for add params to URL and parse
** Designed for Videsk™
*/
module.exports = class ParamsParser {
constructor() {
this.parameters = '?';
this.arrayParams = [];
}
_parse(key, value){
return { key, value };
}
_clean(){
// Clean all variables
this.parameters = '?';
this.arrayParams = [];
}
add(key, value) {
// Add to temp array
this.arrayParams.push(this._parse(key, value));
}
byObj(obj) {
// Add various key and values through object
Object.keys(obj).map(key =>
(Array.isArray(obj[key]))
? this.byArray(key, obj[key])
: this.add(key, obj[key]));
}
byArray(key, array) {
// Add various values with the same key
array.map(e => this.add(key, e));
}
parse() {
// Add parameters to URL
this.arrayParams.map((e, i) => {
this.parameters += `${e.key}=${e.value}`;
if (i < (this.arrayParams.length - 1)) this.parameters += '&';
});
// Add to temp variable
const temp = this.parameters;
// Clean all
this._clean();
// Return parameters
return temp;
}
};