-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (104 loc) · 3.22 KB
/
Copy pathindex.js
File metadata and controls
117 lines (104 loc) · 3.22 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
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
/**
* Boxyname constructor take options in parameters. At the moment, there are prefix and base count.
*
* @param {Object} options
*/
var Boxyname = function (options) {
this.options = options || {
prefix: 'bx',
base: 16
};
this.b = this.options.base;
this.p = this.options.prefix;
}
/**
* We can get an hexadecimal return from a decimal.
*
* @param {integer} decimal
*/
Boxyname.prototype.get = function (decimal) {
return this.p + this.pad(parseInt(decimal).toString(this.b));
}
/**
* Internal function to retrieve next or prev names.
*
* @param {string} direction
* @param {string} hexadecimal
* @param {integer} iteration
* @callback cb
*/
Boxyname.prototype.side = function (direction, hexadecimal, iteration, cb) {
var hexa = hexadecimal.replace(this.p, '');
var decimal = parseInt(hexa, this.b);
var obj = {};
obj[1] = (direction === 'next') ? this.get(decimal + 1) : this.get(decimal - 1);
if (iteration > 1) {
for (var i = 2; i <= iteration; i++) {
obj[i] = (direction === 'next') ? this.get(decimal + i) : this.get(decimal - i);
}
}
if (cb) {
return cb(obj);
} else {
return obj;
}
}
/**
* We can get 1 or X next hexadecimalS from current hexadecimal.
* This function returns a raw object but eventually yan can make a callback to add your logic.
*
* @param {string} hexadecimal
* @param {integer} iteration
* @callback cb
*/
Boxyname.prototype.next = function (hexadecimal, iteration = 1, cb = null) {
return this.side('next', hexadecimal, iteration, cb);
}
/**
* We can get 1 or X previous hexadecimalS from current hexadecimal.
* This function returns a raw object but eventually yan can make a callback to add your logic.
*
* @param {string} hexadecimal
* @param {integer} iteration
* @callback cb
*/
Boxyname.prototype.prev = function (hexadecimal, iteration = 1, cb = null) {
return this.side('previous', hexadecimal, iteration, cb);
}
/**
*
* Potentially, we can maybe add a padding for the result.
* Example : bx1 -> bx001
*
* @param {integer} decimal
*
*/
Boxyname.prototype.pad = function pad(decimal, width = 3, value = 0) {
return decimal.length >= width ? decimal : new Array(width - decimal.length + 1).join(value) + decimal;
}
var boxyname = new Boxyname();
function awaitForSomeAws(server, time) {
let maybeAnError = Math.random() >= 0.8;
return new Promise(function (resolve, reject) {
if (maybeAnError) {
setTimeout(() => reject('Error ' + server + ' created.'), time);
} else {
setTimeout(() => resolve('Server ' + server + ' created.'), time);
}
});
}
console.log(boxyname.get(1)); // Get the 9999th server
console.log(boxyname.prev('bxfff', 10)); // Get 10 server name previous to bxfff
console.log(boxyname.next('bx1', 100)); // Get 10 server name previous to bx005
boxyname.next('bx1', 10, function (servers) {
for (k in servers) {
// Oh! Maybe we can simulate a call to an api like AWS :)
awaitForSomeAws(servers[k], k * 10)
.then(function (val) {
console.log(val);
})
.catch(function (val) {
console.log(val);
});
}
});