From 4c3a28c780e91d85392e705b08980f37b799625f Mon Sep 17 00:00:00 2001 From: Darren Nelsen Date: Thu, 10 Aug 2017 14:39:35 -0400 Subject: [PATCH] added CurrencyProvider and associated spec --- app/src/providers/currency/currency.spec.ts | 78 +++++++++++++++++++++ app/src/providers/currency/currency.ts | 68 ++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 app/src/providers/currency/currency.spec.ts create mode 100644 app/src/providers/currency/currency.ts diff --git a/app/src/providers/currency/currency.spec.ts b/app/src/providers/currency/currency.spec.ts new file mode 100644 index 0000000..d1ef863 --- /dev/null +++ b/app/src/providers/currency/currency.spec.ts @@ -0,0 +1,78 @@ +/* tslint:disable:no-unused-variable */ +import { TestBed, ComponentFixture, inject } from '@angular/core/testing'; +import { HttpModule } from '@angular/http'; +import { CurrencyProvider } from './currency'; + +describe('CurrencyProvider', () => { + let currency; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + HttpModule + ], + providers: [ + CurrencyProvider + ] + }); + }); + + beforeEach(inject([CurrencyProvider], _cp => { + currency = _cp; + })); + + it('initialises', () => { + expect(currency).not.toBeNull(); + expect(currency.defaultCurrency).toBe('BTC'); + expect(currency.currencySymbol).toBe('BTC'); + expect(currency.factor).toBe(1); + }); + + it('sets currency by updating the symbol and changing the multiplication factor', () => { + currency.setCurrency('USD'); + expect(currency.currencySymbol).toBe('USD'); + expect(currency.factor).toEqual(1); + + currency.setCurrency('BTC'); + expect(currency.currencySymbol).toBe('BTC'); + expect(currency.factor).toEqual(1); + + currency.setCurrency('mBTC'); + expect(currency.currencySymbol).toBe('mBTC'); + expect(currency.factor).toEqual(1000); + + currency.setCurrency('bits'); + expect(currency.currencySymbol).toBe('bits'); + expect(currency.factor).toEqual(1000000); + }); + + it('rounds float using specified number of decimal places', () => { + let aFloat = 4.32943; + + expect(currency.roundFloat(aFloat, 2)).toBe(4.33); + expect(currency.roundFloat(aFloat, 3)).toBe(4.329); + expect(currency.roundFloat(aFloat, 4)).toBe(4.3294); + expect(currency.roundFloat(aFloat, 5)).toBe(4.32943); + + aFloat = 1234567890.09876543; + + expect(currency.roundFloat(aFloat, 2)).toBe(1234567890.10); + expect(currency.roundFloat(aFloat, 3)).toBe(1234567890.099); + expect(currency.roundFloat(aFloat, 4)).toBe(1234567890.0988); + expect(currency.roundFloat(aFloat, 5)).toBe(1234567890.09877); + expect(currency.roundFloat(aFloat, 6)).toBe(1234567890.098765); + expect(currency.roundFloat(aFloat, 7)).toBe(1234567890.0987654); + expect(currency.roundFloat(aFloat, 8)).toBe(1234567890.09876543); + }); + + it('gets proper conversion after changing currency', () => { + let aFloat = 12345.09876543; + expect(currency.getConversion(aFloat)).toBe('12345.09876543 BTC'); + + currency.setCurrency('mBTC'); + expect(currency.getConversion(aFloat)).toBe('12345098.76543 mBTC'); + + currency.setCurrency('bits'); + expect(currency.getConversion(aFloat)).toBe('12345098765.43 bits'); + }); +}); diff --git a/app/src/providers/currency/currency.ts b/app/src/providers/currency/currency.ts new file mode 100644 index 0000000..fe6f412 --- /dev/null +++ b/app/src/providers/currency/currency.ts @@ -0,0 +1,68 @@ +import { Injectable } from '@angular/core'; +import { Http } from '@angular/http'; +import 'rxjs/add/operator/map'; + +/* + Generated class for the CurrencyProvider provider. + + See https://angular.io/docs/ts/latest/guide/dependency-injection.html + for more info on providers and Angular DI. +*/ +@Injectable() +export class CurrencyProvider { + + private defaultCurrency: string; + private currencySymbol: string; + private factor: number = 1; + + constructor(public http: Http) { + this.defaultCurrency = 'BTC'; + this.currencySymbol = this.defaultCurrency; + } + + private roundFloat(aFloat: number, decimalPlaces: number): number { + return Math.round(aFloat * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces); + } + + public getConversion(value: number): string { + if (value === 0.00000000) return '0 ' + this.currencySymbol; // fix value to show + + let response: number; + + if (this.currencySymbol === 'USD') { + response = this.roundFloat((value * this.factor), 2); + } else if (this.currencySymbol === 'mBTC') { + this.factor = 1000; + response = this.roundFloat((value * this.factor), 5); + } else if (this.currencySymbol === 'bits') { + this.factor = 1000000; + response = this.roundFloat((value * this.factor), 2); + } else { // assumes currencySymbol is BTC + this.factor = 1; + response = this.roundFloat((value * this.factor), 8); + } + + return response + ' ' + this.currencySymbol; + } + + public setCurrency(currency: string): void { + this.currencySymbol = currency; + localStorage.setItem('insight-currency', currency); + + if (currency === 'USD') { + // TODO Replace this with call + /* + Currency.get({}, function(res) { + $rootScope.currency.factor = $rootScope.currency.bitstamp = res.data.bitstamp; + }); + */ + } else if (currency === 'mBTC') { + this.factor = 1000; + } else if (currency === 'bits') { + this.factor = 1000000; + } else { + this.factor = 1; + } + } + +}