-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
67 lines (61 loc) · 1.46 KB
/
helper.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
67
const {
CALLBACK_DATA_SEPARATOR: cbsep,
BUTTON_POSITION_SEPARATOR: btnsep,
FEATURE_PREFIX_SEPARATOR : fsep,
CHECK_ICON
} = require("./constants");
/**
* Make Inline Button
* @param {string} text
* @param {Object} callbackData
*/
function makeButton(text, callbackData) {
const { prefix, action, params } = callbackData;
return {
text: text,
callback_data: `${prefix}${cbsep}${action}${cbsep}${params}`
};
}
/**
* Toogle check icon from given text
* @param {string} text
*/
function toogleCheckIcon(text) {
if (text.includes(CHECK_ICON)) return text.split(CHECK_ICON).pop();
return CHECK_ICON + text;
}
/**
* Encode keyboard positition to string formated
* @param {int} row
* @param {int} col
*/
function encodePosition(row, col) {
return `${row}${btnsep}${col}`;
}
/**
* Decode params to keyboard postition
* @param {string} dataPosition
*/
function decodePosition(dataPosition) {
const position = dataPosition.split(btnsep);
return {
row: parseInt(position[0]),
col: parseInt(position[1])
};
}
/**
* decode callback data
* @param {string} data
*/
function decodeCallbackData(data) {
const [prefix, method, params] = data.split(cbsep);
const [featureName, owner] = prefix.split(fsep);
return { prefix, method, params, featureName, owner };
}
module.exports = {
makeButton,
decodePosition,
encodePosition,
toogleCheckIcon,
decodeCallbackData
};