Skip to content

Commit

Permalink
Fix currency-converter
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Dec 11, 2024
1 parent 05b5348 commit e39ba6c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 302 deletions.
45 changes: 8 additions & 37 deletions packages/currency-converter/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as cheerio from 'cheerio'
import { Convert } from 'easy-currencies'

export type CurrencyCode =
| 'AFN'
Expand Down Expand Up @@ -155,16 +155,17 @@ export type CurrencyCode =

type CurrencyPair = { expiryDate: Date, rate: number }

export const currencies: CurrencyCode[] = ['AFN', 'ALL', 'DZD', 'AOA', 'ARS', 'AMD', 'AWG', 'AUD', 'AZN', 'BSD', 'BHD', 'BBD', 'BDT', 'BYN', 'BZD', 'BMD', 'BTN', 'XBT', 'BOB', 'BAM', 'BWP', 'BRL', 'BND', 'BGN', 'BIF', 'XPF', 'KHR', 'CAD', 'CVE', 'KYD', 'FCFA', 'CLP', 'CLF', 'CNY', 'CNY', 'COP', 'CF', 'CHF', 'CDF', 'CRC', 'HRK', 'CUC', 'CZK', 'DKK', 'DJF', 'DOP', 'XCD', 'EGP', 'ETB', 'FJD', 'GMD', 'GBP', 'GEL', 'GHS', 'GTQ', 'GNF', 'GYD', 'HTG', 'HNL', 'HKD', 'HUF', 'ISK', 'INR', 'IDR', 'IRR', 'IQD', 'ILS', 'JMD', 'JPY', 'JOD', 'KMF', 'KZT', 'KES', 'KWD', 'KGS', 'LAK', 'LBP', 'LSL', 'LRD', 'LYD', 'MOP', 'MKD', 'MGA', 'MWK', 'MYR', 'MVR', 'MRO', 'MUR', 'MXN', 'MDL', 'MAD', 'MZN', 'MMK', 'NAD', 'NPR', 'ANG', 'NZD', 'NIO', 'NGN', 'NOK', 'OMR', 'PKR', 'PAB', 'PGK', 'PYG', 'PHP', 'PLN', 'QAR', 'RON', 'RUB', 'RWF', 'SVC', 'SAR', 'RSD', 'SCR', 'SLL', 'SGD', 'SBD', 'SOS', 'ZAR', 'KRW', 'VES', 'LKR', 'SDG', 'SRD', 'SZL', 'SEK', 'CHF', 'TJS', 'TZS', 'THB', 'TOP', 'TTD', 'TND', 'TRY', 'TMT', 'UGX', 'UAH', 'AED', 'USD', 'UYU', 'UZS', 'VND', 'XAF', 'XOF', 'YER', 'ZMW', 'ETH', 'EUR', 'LTC', 'TWD', 'PEN']

class CurrencyConverter {
currencyFrom: string
currencyTo: string
currencyAmount: number
convertedValue: number
isDecimalComma: boolean
isRatesCaching: boolean
ratesCacheDuration: number
ratesCache: Record<string, CurrencyPair>
currencyCode: CurrencyCode[] = ['AFN', 'ALL', 'DZD', 'AOA', 'ARS', 'AMD', 'AWG', 'AUD', 'AZN', 'BSD', 'BHD', 'BBD', 'BDT', 'BYN', 'BZD', 'BMD', 'BTN', 'XBT', 'BOB', 'BAM', 'BWP', 'BRL', 'BND', 'BGN', 'BIF', 'XPF', 'KHR', 'CAD', 'CVE', 'KYD', 'FCFA', 'CLP', 'CLF', 'CNY', 'CNY', 'COP', 'CF', 'CHF', 'CDF', 'CRC', 'HRK', 'CUC', 'CZK', 'DKK', 'DJF', 'DOP', 'XCD', 'EGP', 'ETB', 'FJD', 'GMD', 'GBP', 'GEL', 'GHS', 'GTQ', 'GNF', 'GYD', 'HTG', 'HNL', 'HKD', 'HUF', 'ISK', 'INR', 'IDR', 'IRR', 'IQD', 'ILS', 'JMD', 'JPY', 'JOD', 'KMF', 'KZT', 'KES', 'KWD', 'KGS', 'LAK', 'LBP', 'LSL', 'LRD', 'LYD', 'MOP', 'MKD', 'MGA', 'MWK', 'MYR', 'MVR', 'MRO', 'MUR', 'MXN', 'MDL', 'MAD', 'MZN', 'MMK', 'NAD', 'NPR', 'ANG', 'NZD', 'NIO', 'NGN', 'NOK', 'OMR', 'PKR', 'PAB', 'PGK', 'PYG', 'PHP', 'PLN', 'QAR', 'RON', 'RUB', 'RWF', 'SVC', 'SAR', 'RSD', 'SCR', 'SLL', 'SGD', 'SBD', 'SOS', 'ZAR', 'KRW', 'VES', 'LKR', 'SDG', 'SRD', 'SZL', 'SEK', 'CHF', 'TJS', 'TZS', 'THB', 'TOP', 'TTD', 'TND', 'TRY', 'TMT', 'UGX', 'UAH', 'AED', 'USD', 'UYU', 'UZS', 'VND', 'XAF', 'XOF', 'YER', 'ZMW', 'ETH', 'EUR', 'LTC', 'TWD', 'PEN']
currencyCode: CurrencyCode[] = currencies
currencies: Record<CurrencyCode, string> = {
'AFN': 'Afghan Afghani',
'ALL': 'Albanian Lek',
Expand Down Expand Up @@ -323,13 +324,11 @@ class CurrencyConverter {
from: string,
to: string,
amount: number,
isDecimalComma?: boolean
}) {
this.currencyFrom = ''
this.currencyTo = ''
this.currencyAmount = 1
this.convertedValue = 0
this.isDecimalComma = false
this.isRatesCaching = false
this.ratesCacheDuration = 0
this.ratesCache = {}
Expand All @@ -343,9 +342,6 @@ class CurrencyConverter {

if (params['amount'] !== undefined)
this.amount(params['amount'])

if (params['isDecimalComma'] !== undefined)
this.setDecimalComma(params['isDecimalComma'])
}
}

Expand Down Expand Up @@ -380,14 +376,6 @@ class CurrencyConverter {
return this
}

setDecimalComma(isDecimalComma: boolean) {
if (typeof isDecimalComma !== 'boolean')
throw new TypeError('isDecimalComma should be a boolean')

this.isDecimalComma = isDecimalComma
return this
}

replaceAll(text: string, queryString: string, replaceString: string) {
let text_ = ''
for (let i = 0; i < text.length; i++) {
Expand Down Expand Up @@ -440,29 +428,12 @@ class CurrencyConverter {
})
} else {
return new Promise((resolve, reject) => {
const url = `https://www.google.com/search?q=${this.currencyAmount}+${this.currencyFrom}+to+${this.currencyTo}+&hl=en`
// console.log('URL:', url)
fetch(url)
.then((res) => res.text())
.then((html) => resolve(html))
Convert(this.currencyAmount).from(this.currencyFrom).to(this.currencyTo)
.then((res) => resolve(res))
.catch((err) => reject(err))
}).then((body) => {
return cheerio.load(body as string)
})
.then(($) => {
return $('.iBp4i').text().split(' ')[0]
})
.then((rates: string) => {
if (this.isDecimalComma) {
if (rates.includes('.'))
rates = this.replaceAll(rates, '.', '')
if (rates.includes(','))
rates = this.replaceAll(rates, ',', '.')
} else {
if (rates.includes(','))
rates = this.replaceAll(rates, ',', '')
}
const ratesNum = parseFloat(rates) / this.currencyAmount
.then((rates: unknown) => {
const ratesNum = rates as number / this.currencyAmount
if (this.isRatesCaching) {
this.addRateToRatesCache(currencyPair, ratesNum)
}
Expand Down
Loading

0 comments on commit e39ba6c

Please sign in to comment.