1
0
mirror of https://github.com/kremalicious/blowfish.git synced 2024-06-28 00:27:45 +02:00
blowfish/src/utils.js

86 lines
2.1 KiB
JavaScript
Raw Normal View History

const { app, shell } = require('electron')
const { formatCurrency } = require('@coingecko/cryptoformat')
2020-02-25 04:10:06 +01:00
const axios = require('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/, 'Ξ')
}
}
module.exports = {
openUrl,
rgbaToHex,
locale,
numberFormatter,
2020-02-09 03:36:19 +01:00
cryptoFormatter,
fetchData
}