This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tz.js
102 lines (90 loc) · 3.3 KB
/
tz.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
import BigNumber from "bignumber.js";
var bs58check = require("bs58check");
var dayjs = require("dayjs");
export function checkAddress(address) {
if (address === undefined && address == null) return false;
if (typeof address !== "string") return false;
if (address.length !== 36 && !address.startsWith("KT")) return false;
try {
if (bs58check.decode(address) === undefined) return false;
} catch (e) {
return false;
}
return true;
}
export function checkOperation(operation) {
if (operation === undefined && operation == null) return false;
if (typeof operation !== "string") return false;
if (operation.length != 51 && !operation.startsWith("o")) return false;
if (bs58check.decode(operation) === undefined) return false;
return true;
}
export function checkKeyHash(keyhash) {
if (keyhash === undefined && keyhash == null) return false;
if (typeof keyhash !== "string") return false;
if (keyhash.length != 54 && !keyhash.startsWith("expr")) return false;
if (bs58check.decode(keyhash) === undefined) return false;
return true;
}
export function shortcut(value, tail = 4) {
if (!value) return '';
let head = 0;
if (value.startsWith('tz') || value.startsWith('KT')) {
head = 3
} else if (value.startsWith('o')) {
head = 1;
} else if (value.startsWith('expr')) {
head = 4;
} else {
return value;
}
return `<span class="font-weight-light">${value.slice(0, head)}</span>` +
`${value.slice(head, head + tail)}` +
`<i class="v-icon notranslate mdi mdi-dots-horizontal" style="font-size: 16px;"></i>` +
`${value.slice(value.length - tail)}`
}
export function formatDatetime(timestamp, minDate = {val: 1, unit: "day"}) {
let d = dayjs(timestamp);
if (timestamp) {
if (d.year() < dayjs().year()) return d.format("D MMM'YY HH:mm");
if (d.add(minDate.val, minDate.unit).isBefore(dayjs())) return d.format("D MMM HH:mm");
return d.fromNow();
}
}
export function formatDate(timestamp) {
let d = dayjs(timestamp);
if (timestamp) {
if (d.year() < dayjs().year()) return d.format("D MMM'YY");
if (d.add(1, "days").isBefore(dayjs())) return d.format("D MMM");
return d.fromNow();
}
}
export function plural(count, noun, suffix = 's') {
return `${count} ${noun}${count !== 1 ? suffix : ''}`;
}
export function urlExtractBase58(url) {
const matches = url.match(/\b(tz|KT|o|expr)[1-9A-HJ-NP-Za-km-z]{34,}\b/);
if (matches && matches.length > 0) {
return matches[0];
}
}
export function numberToLocalizeString(number, maximumFractionDigits) {
if (maximumFractionDigits >= 0 && maximumFractionDigits <= 20) {
return number.toLocaleString(undefined, {
maximumFractionDigits
});
} else {
const stringed = String(number);
const intPart = stringed.substring(0, stringed.indexOf('.'));
if (maximumFractionDigits === 0) {
return intPart;
}
return new BigNumber(number).precision(intPart.length + maximumFractionDigits);
}
}
export function round(value, decimals) {
if (decimals > 20) {
return parseFloat(new BigNumber(value).toFixed(decimals));
}
return parseFloat((value / 10 ** decimals).toFixed(decimals));
}