forked from GolosChain/golos-explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
151 lines (143 loc) · 4.61 KB
/
utils.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
let operationFormatter = (object) => {
let resultStr = '',
operation = {};
for (var key in object) {
let operationValue = escapeHtml(object[key]);
switch (key) {
case 'title': case 'body': case 'memo': continue;
case 'parent_author': operation.parent_author = object[key]; break;
case 'author': case 'comment_author': operation.author = object[key]; break;
case 'parent_permlink': operationValue = `<a target="_blank" href="https://golos.id/@${operation.parent_author}/${operationValue}">${operationValue}</a>`; break;
case 'permlink': case 'comment_permlink': operationValue = `<a target="_blank" href="https://golos.id/@${operation.author}/${operationValue}">${operationValue}</a>`; break;
}
let keyBeauty = key.charAt(0).toUpperCase() + key.slice(1).replace(/_/g, ' ');
if (typeof(object[key]) !== 'object') {
resultStr += `${keyBeauty}: <span class="badge badge-secondary">${operationValue}</span> `;
}
else {
for (var paramsKey in object[key]) {
resultStr += `${keyBeauty} ${paramsKey}: <span class="badge badge-secondary">${object[key][paramsKey]}</span> `;
}
}
}
return resultStr;
};
let operationHumanFormatter = (transaction) => {
switch (transaction[0]) {
//case 'transfer': return `Notification from @robot: ${transaction[1].memo}`; break;
default: return '';
}
};
let entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'`': '`',
'=': '='
};
let escapeHtml = (string) => {
return String(string).replace(/[&<>"'`=\/]/g, (s) => {
return entityMap[s];
});
}
let fractional_part_len = (value) => {
const parts = (Number(value) + '').split('.');
return parts.length < 2 ? 0 : parts[1].length;
}
// https://github.com/steemit/condenser/blob/master/src/app/utils/ParsersAndFormatters.js#L8
let formatDecimal = (value, decPlaces = 2, truncate0s = true) => {
let decSeparator, fl, i, j, sign, thouSeparator, abs_value;
if (value === null || value === void 0 || isNaN(value)) {
return 'NaN';
}
if (truncate0s) {
fl = fractional_part_len(value);
if (fl < 2) fl = 2;
if (fl < decPlaces) decPlaces = fl;
}
decSeparator = '.';
thouSeparator = ',';
sign = value < 0 ? '-' : '';
abs_value = Math.abs(value);
i = parseInt(abs_value.toFixed(decPlaces), 10) + '';
j = i.length;
j = i.length > 3 ? j % 3 : 0;
const decPart = decPlaces
? decSeparator +
Math.abs(abs_value - i)
.toFixed(decPlaces)
.slice(2)
: '';
return [
sign +
(j ? i.substr(0, j) + thouSeparator : '') +
i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thouSeparator),
decPart,
];
}
// https://github.com/steemit/condenser/blob/master/src/app/utils/ParsersAndFormatters.js#L47
let log10 = (str) => {
const leadingDigits = parseInt(str.substring(0, 4));
const log = Math.log(leadingDigits) / Math.LN10 + 0.00000001;
const n = str.length - 1;
return n + (log - parseInt(log));
}
let rep2 = (rep2) => {
if (rep2 == null) return rep2;
let rep = String(rep2);
const neg = rep.charAt(0) === '-';
rep = neg ? rep.substring(1) : rep;
let out = log10(rep);
if (isNaN(out)) out = 0;
out = Math.max(out - 9, 0); // @ -9, $0.50 earned is approx magnitude 1
out = (neg ? -1 : 1) * out;
out = out * 9 + 25; // 9 points per magnitude. center at 25
// base-line 0 to darken and < 0 to auto hide (grep rephide)
out = parseInt(out);
return out;
}
let camelCase = (str) => {
str = str.replace(/(#)/g, '').replace(/\./g, '');
str = str.replace(/-([a-z])/g, (_m, l) => {
return l.toUpperCase();
});
return str.replace(/ ([a-z])/g, (_m, l) => {
return l.toUpperCase();
});
}
let initHtmlElements = ($htmlElements) => {
/*document.addEventListener('DOMContentLoaded', () => {
});*/
for (let name in $htmlElements) {
let nameConst = $htmlElements[name];
nameConst = camelCase(nameConst);
eval('window.$' + nameConst + ' = document.querySelector("' + $htmlElements[name] + '");');
}
};
let paramsToGetQuery = (obj, prefix) => {
let str = [],
p;
for (p in obj) {
if (obj.hasOwnProperty(p)) {
let k = prefix ? prefix + '[' + p + ']' : p,
v = obj[p];
str.push((v !== null && typeof v === 'object') ?
paramsToGetQuery(v, k) :
encodeURIComponent(k) + '=' + encodeURIComponent(v));
}
}
return str.join('&');
};
/*let paramsToGetQuery = (params, prefix) => {
const query = Object.keys(params).map((key) => {
const value = params[key];
if (params.constructor === Array) key = `${prefix}[]`;
else if (params.constructor === Object) key = (prefix ? `${prefix}[${key}]` : key);
if (typeof value === 'object') return paramsToGetQuery(value, key);
else return `${key}=${encodeURIComponent(value)}`;
});
return [].concat.apply([], query).join('&');
}*/