-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtbank.js
196 lines (169 loc) · 4.94 KB
/
mtbank.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"use strict";
/* global chrome:false */
/**
* Convert number to formatted string
* Example:
* numberFormat(1000.12, 3, ",", " ") // "1 000,120"
*
* @param {String|Number} number
* @param {Number} [decimals=2]
* @param {String} [decimal_separator="."]
* @param {String} [thousands_separator=","]
* @returns {String}
*/
function numberFormat(number, decimals, decimal_separator, thousands_separator) {
number = +number;
if(Number.isNaN(number)) {
return '0';
}
if(typeof decimal_separator !== 'string') {
decimal_separator = '.';
}
if(typeof thousands_separator !== 'string') {
thousands_separator = ',';
}
var c = decimals === -1 ?
(number.toString().split('.')[1] || '').length : // preserve decimals
(Number.isNaN(decimals = Math.abs(decimals)) ? 2 : decimals),
s = number < 0 ? "-" : "",
i = String(parseInt(number = Math.abs(number).toFixed(c))),
j = i.length > 3 ? i.length % 3 : 0;
return s + (j ? i.substr(0, j) + thousands_separator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_separator) +
(c ? decimal_separator + Math.abs(number - i).toFixed(c).slice(2) : "");
}
/**
*
* @param {string} number
* @returns {number}
*/
function toNumber(number) {
var sum = number
.replace(/\s|\'/g, '')
.replace(/,/g, '.');
return Number.isNaN(+sum)?
sum :
+sum;
}
/**
*
* @param {string} number
* @returns {NumberSeparators}
*/
function numberSeparators(number) {
var separators = number.match(/(\d{1,3}([^\d])?)*?\d{1,3}([.,])\d+/);
return separators ?
{
thousands: separators[2] || '',
decimals: separators[3] || '.'
} :
{
thousands: '',
decimals: '.'
};
}
/**
*
* @param {NodeList} dom_items
* @returns {Array.<HTMLElement>}
*/
function destructure(dom_items) {
return [].slice.apply(dom_items);
}
var RATE_TEMPLATE =
'<tr class="rate-box $0">' +
'<td>' +
'<div class="summ balance">$1</div>' +
'</td>' +
'<td>' +
'<div class="currency">$2</div>' +
'</td>' +
'</tr>';
/**
*
* @param {Element} card_box
* @param {Rate} rate_data
*/
function fill_rate(card_box, rate_data) {
var convert_currency = rate_data.currency;
var balance = card_box.querySelector('.balance').textContent;
var separators = numberSeparators(balance);
console.log(toNumber(balance) / rate_data.cell);
card_box.querySelector('.balance-table tbody').innerHTML += RATE_TEMPLATE
.replace('$0', convert_currency.toLowerCase())
.replace('$1', numberFormat(
toNumber(balance) / rate_data.cell,
2, separators.decimals, separators.thousands
))
.replace('$2', convert_currency);
}
/**
*
*/
function initializeCurrencies() {
/**
* Table of BYN and EUR/USD conversions
*
* @type {NodeList}
*/
var conversation_table = document.querySelectorAll('.conversion-table tr td');
/**
*
* @type {Object.<string, Rate>}
*/
var rates = {};
var items = destructure(conversation_table);
for(var i = 0; i < items.length; i += 3) {
var name = items[i].textContent;
var rate_buy = items[i + 1];
var rate_cell = items[i + 2];
rates[name] = {
currency: name,
buy: toNumber(rate_buy.textContent),
cell: toNumber(rate_cell.textContent)
};
}
/**
* Cards, deposits etc
*
* @type {NodeList}
*/
var product_boxes = document.querySelectorAll('.product-body');
destructure(product_boxes).forEach(function(card_box) {
var currency = card_box.querySelector('.currency').textContent;
switch (currency) {
case 'BYN':
fill_rate(card_box, rates.USD);
fill_rate(card_box, rates.EUR);
break;
case 'USD':
fill_rate(card_box, {
currency: 'BYN',
buy: 1 / rates.USD.cell,
cell: 1 / rates.USD.buy
});
fill_rate(card_box, {
currency: 'EUR',
buy: rates["EUR/USD"].cell,
cell: rates["EUR/USD"].buy
});
break;
case 'EUR':
fill_rate(card_box, {
currency: 'BYN',
buy: 1 / rates.EUR.cell,
cell: 1 / rates.EUR.buy
});
fill_rate(card_box, {
currency: 'USD',
buy: rates["EUR/USD"].buy,
cell: rates["EUR/USD"].cell
});
break;
}
});
}
if (document.querySelector('#products-content')) {
initializeCurrencies();
} else {
document.addEventListener("DOMContentLoaded", initializeCurrencies);
}