-
Notifications
You must be signed in to change notification settings - Fork 2
/
currency-conversion.pipe.ts
45 lines (40 loc) · 1.44 KB
/
currency-conversion.pipe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { formatCurrency } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'currencyConversion',
pure: false
})
export class CurrencyConversionPipe implements PipeTransform {
/**
*
* Formats price as currency based on user selected country
*
* @param amount - price data 1:1 with usd
*
* @param country - checks location on initial load OR sets default location to US dollar, updates currency when USD CAD,JPY,EUR,GBP options are selected
*
* @param rates - current international exchange rates from Currencybeacon API
*
* @returns updated currency amount based on selected country
*
*/
transform(amount: number, country: string, rates: any): any {
switch (country) {
// Canada
case 'store-ca':
return formatCurrency(amount * rates['CAD'].exchangeRate, 'en-CA', '$', 'Canadian Dollar', '1.0-0');
// European Union
case 'store-eu':
return formatCurrency(amount * rates['EUR'].exchangeRate, 'en-EU', '€', 'Euro', '1.0-0');
// United Kingdom
case 'store-uk':
return formatCurrency(amount * rates['GBP'].exchangeRate, 'en-us', '£', 'GBP', '1.0-0');
// Japan
case 'store-jp':
return formatCurrency(amount * rates['JPY'].exchangeRate, 'en-JP', '¥', 'JPY', '1.0-0');
// USA or Just browsing
default:
return formatCurrency(amount, 'en-US', '$', 'USD', '1.0-0');
}
}
}