blowfish/src/utils.js

86 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2020-10-10 15:37:05 +02:00
import { app, shell } from 'electron'
import { formatCurrency } from '@coingecko/cryptoformat'
import axios from 'axios'
2019-05-06 14:02:49 +02:00
2020-03-22 00:49:10 +01:00
const fetchData = async (url) => {
2020-02-09 03:36:19 +01:00
try {
2020-02-25 04:10:06 +01:00
const response = await axios(url)
2020-02-09 03:36:19 +01:00
if (response.status !== 200) {
2020-02-25 04:10:06 +01:00
return console.error('Non-200 response: ' + response.status)
2020-02-09 03:36:19 +01:00
}
2020-02-25 04:10:06 +01:00
return response.data
2020-02-09 03:36:19 +01:00
} catch (error) {
2020-02-25 04:10:06 +01:00
console.error('Error parsing json: ' + error.message)
2020-02-09 03:36:19 +01:00
}
}
2020-03-22 00:49:10 +01:00
const isFiat = (currency) => currency === 'eur' || currency === 'usd'
2019-06-04 20:35:37 +02:00
2020-03-22 00:49:10 +01:00
const openUrl = (url) => {
2019-05-06 14:02:49 +02:00
shell.openExternal(url)
}
2020-03-22 00:49:10 +01:00
const rgbaToHex = (color) => {
const r = color.substr(0, 2)
const g = color.substr(2, 2)
const b = color.substr(4, 2)
// const a = color.substr(6, 2)
return '#' + r + g + b
}
const locale =
2019-05-20 21:21:05 +02:00
typeof navigator !== 'undefined' ? navigator.language : () => app.getLocale()
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
2020-03-22 00:49:10 +01:00
const numberFormatter = (value) =>
new Intl.NumberFormat(locale, {
minimumFractionDigits: 0,
maximumFractionDigits: 4
}).format(value)
2020-03-22 00:49:10 +01:00
const formatOcean = (value) => {
const numberformatted = new Intl.NumberFormat(locale, {
minimumFractionDigits: 0,
maximumFractionDigits: 4,
style: 'currency',
currency: 'EUR' // fake currency symbol to replace later
}).format(value)
return numberformatted.replace(/EUR/, 'Ọ').replace(/€/, 'Ọ')
}
2019-06-04 20:35:37 +02:00
const formatFiat = (value, currency) => {
const numberformatted = new Intl.NumberFormat(locale, {
minimumFractionDigits: 0,
maximumFractionDigits: 4,
style: 'currency',
currency: currency.toUpperCase()
}).format(value)
return numberformatted
}
const cryptoFormatter = (value, currency) => {
if (currency === 'ocean') {
return formatOcean(value)
2019-06-04 20:35:37 +02:00
} else if (isFiat(currency)) {
return formatFiat(value, currency)
} else {
2019-05-20 21:21:05 +02:00
return formatCurrency(value, currency.toUpperCase(), locale)
.replace(/BTC/, 'Ƀ')
.replace(/ETH/, 'Ξ')
}
}
2020-10-10 15:37:05 +02:00
export {
openUrl,
rgbaToHex,
locale,
numberFormatter,
2020-02-09 03:36:19 +01:00
cryptoFormatter,
fetchData
}