-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCurrency.gs
85 lines (73 loc) · 2.26 KB
/
Currency.gs
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
/**
* A collection of static utility functions for currencies.
*/
var Currency = class Currency {
constructor() {
}
/**
* Array of supported fiat currency tickers.
* Limited to those supported by CryptoCompare.
* @type {string[]}
* @static
*/
static get validFiats() {
return ['USD', 'EUR', 'CAD', 'AUD', 'GBP', 'CHF', 'JPY'];
}
/**
* Regular expression to loosly validate cryptocurrency format.
* @type {RegExp}
* @static
*/
static get cryptoRegExp() {
return /^[\w$@]{1,9}$/;
}
/**
* Determines whether the currency ticker is a valid fiat.
* Only fiat currencies in the valid fiats list those supported by CryptoComapre will return true.
* @param {string} ticker - The currency ticker in question.
* @return {boolean} Whether the currency is a valid fiat currency.
* @static
*/
static isFiat(ticker) {
return Currency.validFiats.includes(ticker);
}
/**
* Determines whether the currency ticker is a valid cryptocurrency.
* All currencies that are not valid fiats and pass the loose regular expresion validation will return true.
* @param {string} ticker - The currency ticker in question.
* @return {boolean} Whether the currency is a valid cryptocurrency.
* @static
*/
static isCrypto(ticker) {
return !Currency.validFiats.includes(ticker) && Currency.cryptoRegExp.test(ticker);
}
/**
* Returns the valid number of decimal digits of a given currency ticker.
* @param {string} ticker - The fiat or cryptocurrency ticker.
* @return {number} - The valid number of decimal digits.
* @static
*/
static decimalDigits(ticker) {
if (ticker === 'JPY') {
return 0;
}
else if (ticker === 'ADA') {
return 6;
}
else if (Currency.isFiat(ticker)) {
return 2;
}
else {
return 8;
}
}
/**
* Returns the number of subunit in a unit of a given currency ticker (e.g 100 cents in 1 USD, or 100,000,000 satoshi in 1 BTC).
* @param {string} ticker - The fiat or cryptocurrency ticker.
* @return {number} - The number of subunit in a unit of the currency ticker (e.g 100 cents in 1 USD, or 100,000,000 satoshi in 1 BTC).
* @static
*/
static subunits(ticker) {
return 10 ** Currency.decimalDigits(ticker);
}
};