Module: CurrencyConverter

JavaScript currency converter
Source:

Methods

(inner) buildUrl(queryParams) → {string}

Builds API endpoint url from SETTINGS.API.url, SETTINGS.API.queryParams, and queryParams 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, …conversions) → {Promise.<conversionObject>}

Converts given amount from given currency to given currency
Parameters:
Name Type Attributes Description
amount number First parameter is the amount of money converting
conversions string <repeatable>
All following string parameters are conversion strings
Properties:
Name Type Description
conversionObject.amount number converted amount
conversionObject.val 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(…conversions) → {Promise.<conversionObject>}

Returns conversion rate from the API
Parameters:
Name Type Attributes Description
conversions string <repeatable>
Conversion strings. Multiple conversion strings can be passed in simultaneously, the api will handle all of them in one call. Results will be resolved all at once in the conversionObject. Currency codes inside of the conversion string have to be delimited by '_' (USD_EUR, GBP_KRW, etc.).
Properties:
Name Type Description
conversionObject.val number conversion rate
Source:
Returns:
Resolves to conversion rate object
Type
Promise.<conversionObject>

(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(…conversions) → {Promise.<conversionObject>}

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 Attributes Description
conversions string <repeatable>
Conversion strings. Multiple conversion strings can be passed in simultaneously. Cached and non expired conversion rates will be used, and non-cached ones will be fetched from the server. Results will be merged and resolved through conversionObject.
Properties:
Name Type Description
conversionObject.val number conversion rate
conversionObject.expired number conversion rate expiriy flag
Source:
Returns:
Resolves to conversion rate object
Type
Promise.<conversionObject>

(inner) getRateFromCache() → {conversionObject}

Returns the conversion rate object form memory for the given query
Properties:
Name Type Description
conversionObject.val number converted amount
conversionObject.expired boolean 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