forked from antonmedv/numbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrencies.ts
69 lines (58 loc) · 2.5 KB
/
currencies.ts
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
export type CurrencyCode = string & { kind: 'currency_code' }
export type CurrencyRates = { [currency: CurrencyCode]: number }
export type CurrencyInfo = {
name: CurrencyCode
dp: number
}
export const currencies: { [id: CurrencyCode]: CurrencyInfo } = require('./data/currencies.json')
export const cryptoCurrencies: { [id: CurrencyCode]: CurrencyInfo } = require('./data/crypto.json')
export let currenciesList = new Set(Object.keys(currencies).concat(Object.keys(cryptoCurrencies)))
export function updateCurrenciesList(keys: string[]) {
keys.forEach(id => {
if (id.toUpperCase() != id) {
throw new Error(`The currency ${id} is in lowercase!`)
}
})
currenciesList = new Set(keys)
}
export const currencySignsToCode = new Map<string, CurrencyCode>()
currencySignsToCode.set('$', 'USD' as CurrencyCode)
currencySignsToCode.set('$', 'USD' as CurrencyCode)
currencySignsToCode.set('﹩', 'USD' as CurrencyCode)
currencySignsToCode.set('₽', 'RUB' as CurrencyCode)
currencySignsToCode.set('€', 'EUR' as CurrencyCode)
currencySignsToCode.set('£', 'GBP' as CurrencyCode)
currencySignsToCode.set('฿', 'THB' as CurrencyCode)
currencySignsToCode.set('¥', 'JPY' as CurrencyCode)
currencySignsToCode.set('₣', 'FRF' as CurrencyCode)
currencySignsToCode.set('₩', 'KRW' as CurrencyCode)
export const currencySigns = new Set(currencySignsToCode.keys())
const currencyWordsToCode: [RegExp, CurrencyCode][] = [
[/^dollars?$/i, 'USD' as CurrencyCode],
[/^ro?ubl(es?)?$/i, 'RUB' as CurrencyCode],
[/^euros?$/i, 'EUR' as CurrencyCode],
[/^baht$/i, 'THB' as CurrencyCode],
[/^bitcoins?$/i, 'BTC' as CurrencyCode],
[/^руб(л(ь|и|ей|ях?|ями?)?)?$/i, 'RUB' as CurrencyCode],
[/^доллар(ы|ов|ами?|ах)?$/i, 'USD' as CurrencyCode],
[/^бат(ах)?$/i, 'THB' as CurrencyCode],
[/^битко[йи]н(ы|ами?|ов|ах)?$/i, 'BTC' as CurrencyCode],
]
function findCurrencyCodeByWord(word: string): CurrencyCode | undefined {
for (let [r, code] of currencyWordsToCode) {
if (r.test(word)) return code
}
return undefined
}
export function findCurrencyCode(word: string): CurrencyCode | undefined {
word = word.toUpperCase()
let code = currencySignsToCode.get(word)
if (code) return code
code = findCurrencyCodeByWord(word)
if (code) return code
if (currenciesList.has(word)) return word as CurrencyCode
return undefined
}
export function findCurrencyInfo(code: CurrencyCode): CurrencyInfo | undefined {
return currencies[code] || cryptoCurrencies[code] || undefined
}