Module: CurrencyConverter

JavaScript currency converter
Source:

Methods

(inner) buildUrl(queryParams) → {string}

Builds API endpoint url from SETTINGS.API.url, SETTINGS.API.queryParams, and query parameters passed in
Parameters:
Name Type Description
queryParams object Query parameter key pair values
Source:
Returns:
Type
string

(inner) cacheFromLocalStorage() → {undefined}

Sets the private CACHED_RATES object to the value from localStorage
Source:
Returns:
Type
undefined

(inner) cacheRate(rateName, value, date) → {undefined}

Caches given rate to the memory. If SETTINGS.CACHE_TO_LOCAL_STORAGE is true, entire CACHED_RATES object will be cached to localStorage as well
Parameters:
Name Type Description
rateName string Conversion rate name in RATETO_RATEFROM format. This is the key under which the conversion object will be mapped in the CACHED_RATES private object
value number Conversion rate value
date date Converesion rate caching date
Source:
Returns:
Type
undefined

(inner) cacheToLocalStorage() → {undefined}

Caches the private CACHED_RATES object to local storage
Source:
Returns:
Type
undefined

(inner) config(settings)

Overrides default CurrencyConverter settings
Parameters:
Name Type Description
settings object Overrides CurrencyConverter settings object
Properties:
Name Type Description
settings.CACHE_TO_LOCAL_STORAGE number Cache conversion rate to local storage, if available
settings.RATES_VALIDITY_HOURS number Cached conversion rate validity in hours
settings.LOCAL_STORAGE_VARIABLE_NAME string Variable name where the rates will be cached in local storage
settings.API object object API configuration object
Properties
Name Type Description
url string API Endpoint url
queryParams object Query parameters key pair values
Properties
Name Type Description
apiKey string API key for non-free version of the API
compact string API response object type
Source:

(inner) convertAmount(amount, fromCurrency, toCurrency) → {Promise.<conversionObject>}

Converts given amount from given currency to given currency
Parameters:
Name Type Description
amount number Amount of money converting
fromCurrency string Currency converting from
toCurrency string Currency converting to
Properties:
Name Type Description
conversionObject.value number converted amount
conversionObject.rate number conversion rate
conversionObject.expired boolean is the rate expired (considering RATES_VALIDITY_HOURS)
Source:
Returns:
Promise to the conversionObject
Type
Promise.<conversionObject>

(inner) fetchQuote(fromCurrency, toCurrency) → {Promise.<number>}

Returns conversion rate from the API
Parameters:
Name Type Description
fromCurrency string Currency converting from
toCurrency string Currency converting to
Source:
Returns:
Resolves to conversion rate number
Type
Promise.<number>

(inner) getConfig() → {settings}

Returns CurrencyConverter settings object
Properties:
Name Type Description
settings.CACHE_TO_LOCAL_STORAGE number Cache conversion rate to local storage, if available
settings.RATES_VALIDITY_HOURS number Cached conversion rate validity in hours
settings.LOCAL_STORAGE_VARIABLE_NAME string Variable name where the rates will be cached in local storage
settings.API object object API configuration object
Properties
Name Type Description
url string API Endpoint url
queryParams object Query parameters key pair values
Properties
Name Type Description
apiKey string API key for non-free version of the API
compact string API response object type
Source:
Returns:
Type
settings

(inner) getConversionInProgress(query) → {Promise.<conversionObject>}

Returns a promise in progress for the given query
Parameters:
Name Type Description
query string RATETO_RATEFROM string
Source:
Returns:
Promise to the conversionObject
Type
Promise.<conversionObject>

(inner) getFromLocalStorage(key) → {object}

Retrieves a value from the local storage for the given key. On error returns empty object
Parameters:
Name Type Description
key key Key for the given value
Source:
Returns:
Type
object

(inner) getRate(fromCurrency, toCurrency) → {Promise.<number>}

Returns conversion rate. If the conversion rate is already available in the cache, and not expired, that rate is used. If the conversion rate is not available in the cache, API rate fetch is attempted. If the rate is available in the cache but expired, API rate fetch is attempted. If the rate is available in the cache and expired, and API rate fetch fails, expired rate is returned if available.
Parameters:
Name Type Description
fromCurrency string Currency converting from
toCurrency string Currency converting to
Source:
Returns:
Resolves to conversion rate number
Type
Promise.<number>

(inner) getRateFromCache(query) → {conversionObject}

Returns the conversion rate object form memory for the given query
Parameters:
Name Type Description
query string RATETO_RATEFROM string
Properties:
Name Type Description
conversionObject.value number converted amount
conversionObject.rate number conversion rate
Source:
Returns:
Type
conversionObject

(inner) isConversionInProgress(query) → {Boolean}

Returns boolean wether the API call for given query is in progress
Parameters:
Name Type Description
query string RATETO_RATEFROM string
Source:
Returns:
Type
Boolean

(inner) isDateExpired(date) → {Boolean}

Returns boolean wether the input date is lesser then the SETTINGS.RATES_VALIDITY_HOURS setting
Parameters:
Name Type Description
date string Date string
Source:
Returns:
Type
Boolean

(inner) isLocalStorageAvailable() → {Boolean}

Tests the existence of the localStorage object on the global object
Source:
Returns:
Type
Boolean

(inner) isObject(value) → {Boolean}

Returns boolean wether the passed in variable is an object or not
Parameters:
Name Type Description
value object Object we are testing
Source:
Returns:
Type
Boolean

(inner) isRateCached(query) → {Boolean}

Returns boolean wether the conversion object for the given query is cached in memory
Parameters:
Name Type Description
query string RATETO_RATEFROM string
Source:
Returns:
Type
Boolean

(inner) isRateCachedAndNonExpired(query) → {Boolean}

Returns the combination of CurrencyConverter.isRateCached CurrencyConverter.isDateExpired functions
Parameters:
Name Type Description
query string RATETO_RATEFROM string
Source:
Returns:
Type
Boolean

(inner) setConversionInProgress(query, promise)

Sets given query and promise as a key:value pair in the private CONVERSIONS_IN_PROGRESS object
Parameters:
Name Type Description
query string Query RATETO_RATEFROM key - pair
promise Promise.<conversionObject> Conversion in progress promise
Source:

(inner) setToLocalStorage(key, value) → {undefined}

Sets given key:value pair to the local storage
Parameters:
Name Type Description
key key Key for the given value
value value Value for the given key
Source:
Returns:
Type
undefined

(inner) toQuery(fromCurrency, toCurrency) → {string}

Returns the concatenated string of the two valued passed in, with underscore (_) as a concat character
Parameters:
Name Type Description
fromCurrency string Rate we are converting from
toCurrency string Rate we are converting to
Source:
Returns:
Type
string