From 5f995544788b742f0b7c13bf8b53b99f4bc7016e Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Thu, 26 Jun 2014 16:11:40 -0300 Subject: [PATCH 1/4] New Angular filter for correct the formatting issue of the currency value --- index.html | 84 ++++++++++++++++++++++++++++----------------------- js/filters.js | 26 +++++++++++++++- 2 files changed, 72 insertions(+), 38 deletions(-) diff --git a/index.html b/index.html index e7825c84c..d94c45b70 100644 --- a/index.html +++ b/index.html @@ -36,7 +36,12 @@ - {{totalBalance || 0 |number}} {{$root.unitName}} + {{totalBalance || 0 + |noFractionNumber:4}} {{$root.unitName}}
@@ -44,7 +49,11 @@ - {{availableBalance || 0|number}} {{$root.unitName}} + {{availableBalance || 0|noFractionNumber:4}} {{$root.unitName}}
@@ -167,8 +176,8 @@
- {{$root.wallet.getName()}} - {{$root.wallet.requiredCopayers}}-of-{{$root.wallet.totalCopayers}} + {{$root.wallet.getName()}} + {{$root.wallet.requiredCopayers}}-of-{{$root.wallet.totalCopayers}} [LIVENET] [TESTNET]
@@ -180,7 +189,7 @@
- {{$root.wallet.getName()}} + {{$root.wallet.getName()}}
{{$root.wallet.requiredCopayers}}-of-{{$root.wallet.totalCopayers}} wallet @@ -190,7 +199,7 @@
Copay v{{version}}
- + @@ -404,11 +413,11 @@ - {{$root.balanceByAddr[addr.address] || 0|number}} {{$root.unitName}} + {{$root.balanceByAddr[addr.address] || 0|noFractionNumber:4}} {{$root.unitName}} - {{addr.balance || 0|number}} {{$root.unitName}} + {{addr.balance || 0|noFractionNumber:4}} {{$root.unitName}} @@ -426,12 +435,12 @@ - {{balanceByAddr[selectedAddr.address] || 0 | number}} + {{balanceByAddr[selectedAddr.address] || 0 | noFractionNumber:4}} {{selectedAddr.address}}
- {{selectedAddr.balance || 0|number}} {{$root.unitName}} + {{selectedAddr.balance || 0|noFractionNumber:4}} {{$root.unitName}}

@@ -462,7 +471,8 @@
-
{{out.value | number}} {{$root.unitName}}
+
+ {{out.value | noFractionNumber:4}} {{$root.unitName}}
@@ -546,7 +556,7 @@

{{tx.missingSignatures}} signatures missing

- Fee: {{tx.fee|number}} {{$root.unitName}} + Fee: {{tx.fee|noFractionNumber:4}} {{$root.unitName}} Proposal ID: {{tx.ntxid}}
@@ -593,7 +603,7 @@
- {{vin.value| number}} {{$root.unitName}} + {{vin.value| noFractionNumber:4}} {{$root.unitName}}

@@ -604,7 +614,7 @@
- {{vout.value| number}} {{$root.unitName}} + {{vout.value| noFractionNumber:4}} {{$root.unitName}}

@@ -614,9 +624,9 @@
-
Fees: {{btx.fees | number}} {{$root.unitName}}
+
Fees: {{btx.fees | noFractionNumber:4}} {{$root.unitName}}
Confirmations: {{btx.confirmations || 0}}
-
Total: {{btx.valueOut| number}} {{$root.unitName}}
+
Total: {{btx.valueOut| noFractionNumber:4}} {{$root.unitName}}
@@ -699,16 +709,16 @@
- Total amount for this transaction: + Total amount for this transaction:
- {{amount + defaultFee |number}} {{$root.unitName}} + {{amount + defaultFee |noFractionNumber:4}} {{$root.unitName}} - {{ ((amount + defaultFee) * unitToBtc) |number}} BTC + {{ ((amount + defaultFee) * unitToBtc) |noFractionNumber:4}} BTC
- Including fee of {{defaultFee|number}} {{$root.unitName}} + Including fee of {{defaultFee|noFractionNumber:4}} {{$root.unitName}}
@@ -771,23 +781,23 @@ diff --git a/js/filters.js b/js/filters.js index 9668c71bf..14069d27d 100644 --- a/js/filters.js +++ b/js/filters.js @@ -30,4 +30,28 @@ angular.module('copayApp.filters', []) return addrs; }; - }); + }) + .filter('noFractionNumber', + [ '$filter', '$locale', + function(filter, locale) { + var numberFilter = filter('number'); + var formats = locale.NUMBER_FORMATS; + return function(amount, fraction) { + var value = numberFilter(amount, fraction); + var sep = value.indexOf(formats.DECIMAL_SEP); + var group = value.indexOf(formats.GROUP_SEP); + if(amount >= 0) { + if (group > 0) { + return value.substring(0, sep); + } + else { + value = parseFloat(value); + if(value % 1 === 0) { + value = value.toFixed(0); + } + return value; + } + } + return 0; + }; + } ]); From f94292e49d9bf7c895634c8b2275028a506ea574 Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Thu, 26 Jun 2014 16:38:09 -0300 Subject: [PATCH 2/4] Test for the new filter --- test/unit/filters/filtersSpec.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/unit/filters/filtersSpec.js b/test/unit/filters/filtersSpec.js index d9d52fe74..a942c4ff2 100644 --- a/test/unit/filters/filtersSpec.js +++ b/test/unit/filters/filtersSpec.js @@ -68,4 +68,16 @@ describe('Unit: Testing Filters', function() { expect(limitAddress(addresses, false).length).to.equal(4); })); }); + + describe('noFractionNumber', function() { + it('should format number to display correctly', inject(function($filter) { + var noFraction = $filter('noFractionNumber'); + var fraction = 4; + expect(noFraction(3100, fraction)).to.equal('3,100'); + expect(noFraction(3100200, fraction)).to.equal('3,100,200'); + expect(noFraction(3, fraction)).to.equal('3'); + expect(noFraction(0.3, fraction)).to.equal(0.3); + })); + }); + }); From 75b45d2f171300dd5976e3380fb93bad74fc776c Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Fri, 27 Jun 2014 02:46:28 -0300 Subject: [PATCH 3/4] Fixed amount values: decimal is variable, depending of unit in config. Also custom values are allowed --- index.html | 34 ++++++++--------- js/filters.js | 22 +++++++++-- js/services/controllerUtils.js | 4 +- test/unit/filters/filtersSpec.js | 65 +++++++++++++++++++++++++++++--- 4 files changed, 97 insertions(+), 28 deletions(-) diff --git a/index.html b/index.html index 8f70095d9..637a95286 100644 --- a/index.html +++ b/index.html @@ -38,10 +38,10 @@ {{totalBalance || 0 - |noFractionNumber:4}} {{$root.unitName}} + |noFractionNumber}} {{$root.unitName}}
@@ -51,9 +51,9 @@ {{availableBalance || 0|noFractionNumber:4}} {{$root.unitName}} + tooltip-placement="bottom">{{availableBalance || 0|noFractionNumber}} {{$root.unitName}}
@@ -422,11 +422,11 @@ - {{$root.balanceByAddr[addr.address] || 0|noFractionNumber:4}} {{$root.unitName}} + {{$root.balanceByAddr[addr.address] || 0|noFractionNumber}} {{$root.unitName}} - {{addr.balance || 0|noFractionNumber:4}} {{$root.unitName}} + {{addr.balance || 0|noFractionNumber}} {{$root.unitName}} @@ -444,12 +444,12 @@ - {{balanceByAddr[selectedAddr.address] || 0 | noFractionNumber:4}} + {{balanceByAddr[selectedAddr.address] || 0 | noFractionNumber}} {{selectedAddr.address}}
- {{selectedAddr.balance || 0|noFractionNumber:4}} {{$root.unitName}} + {{selectedAddr.balance || 0|noFractionNumber}} {{$root.unitName}}

@@ -481,7 +481,7 @@
- {{out.value | noFractionNumber:4}} {{$root.unitName}}
+ {{out.value | noFractionNumber}} {{$root.unitName}}
@@ -565,7 +565,7 @@

{{tx.missingSignatures}} signatures missing

- Fee: {{tx.fee|noFractionNumber:4}} {{$root.unitName}} + Fee: {{tx.fee|noFractionNumber}} {{$root.unitName}} Proposal ID: {{tx.ntxid}}
@@ -612,7 +612,7 @@
- {{vin.value| noFractionNumber:4}} {{$root.unitName}} + {{vin.value| noFractionNumber}} {{$root.unitName}}

@@ -623,7 +623,7 @@
- {{vout.value| noFractionNumber:4}} {{$root.unitName}} + {{vout.value| noFractionNumber}} {{$root.unitName}}

@@ -633,9 +633,9 @@
-
Fees: {{btx.fees | noFractionNumber:4}} {{$root.unitName}}
+
Fees: {{btx.fees | noFractionNumber}} {{$root.unitName}}
Confirmations: {{btx.confirmations || 0}}
-
Total: {{btx.valueOut| noFractionNumber:4}} {{$root.unitName}}
+
Total: {{btx.valueOut| noFractionNumber}} {{$root.unitName}}
@@ -725,13 +725,13 @@ Total amount for this transaction:
- {{amount + defaultFee |noFractionNumber:4}} {{$root.unitName}} + {{amount + defaultFee |noFractionNumber}} {{$root.unitName}} - {{ ((amount + defaultFee) * unitToBtc) |noFractionNumber:4}} BTC + {{ ((amount + defaultFee) * unitToBtc)|noFractionNumber:8}} BTC
- Including fee of {{defaultFee|noFractionNumber:4}} {{$root.unitName}} + Including fee of {{defaultFee|noFractionNumber}} {{$root.unitName}}
diff --git a/js/filters.js b/js/filters.js index 14069d27d..46d56469d 100644 --- a/js/filters.js +++ b/js/filters.js @@ -36,13 +36,29 @@ angular.module('copayApp.filters', []) function(filter, locale) { var numberFilter = filter('number'); var formats = locale.NUMBER_FORMATS; - return function(amount, fraction) { - var value = numberFilter(amount, fraction); + return function(amount, n) { + var fractionSize = (typeof(n) != 'undefined') ? n : config.unitToSatoshi.toString().length - 1; + var value = numberFilter(amount, fractionSize); var sep = value.indexOf(formats.DECIMAL_SEP); var group = value.indexOf(formats.GROUP_SEP); if(amount >= 0) { if (group > 0) { - return value.substring(0, sep); + if (sep < 0) { + return value; + } + var intValue = value.substring(0, sep); + var floatValue = parseFloat(value.substring(sep)); + if (floatValue === 0) { + floatValue = ''; + } + else { + if(floatValue % 1 === 0) { + floatValue = floatValue.toFixed(0); + } + floatValue = floatValue.toString().substring(1); + } + var finalValue = intValue + floatValue; + return finalValue; } else { value = parseFloat(value); diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index f8e87d5e9..cbc293a1f 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -193,9 +193,9 @@ angular.module('copayApp.services') var COIN = bitcore.util.COIN; $rootScope.totalBalance = balanceSat * satToUnit; - $rootScope.totalBalanceBTC = (balanceSat / COIN).toFixed(4); + $rootScope.totalBalanceBTC = (balanceSat / COIN); $rootScope.availableBalance = safeBalanceSat * satToUnit; - $rootScope.availableBalanceBTC = (safeBalanceSat / COIN).toFixed(4); + $rootScope.availableBalanceBTC = (safeBalanceSat / COIN); var balanceByAddr = {}; for (var ii in balanceByAddrSat) { balanceByAddr[ii] = balanceByAddrSat[ii] * satToUnit; diff --git a/test/unit/filters/filtersSpec.js b/test/unit/filters/filtersSpec.js index a942c4ff2..fe180659f 100644 --- a/test/unit/filters/filtersSpec.js +++ b/test/unit/filters/filtersSpec.js @@ -69,14 +69,67 @@ describe('Unit: Testing Filters', function() { })); }); - describe('noFractionNumber', function() { + describe('noFractionNumber bits', function() { + beforeEach(function() { + config.unitToSatoshi = 100; + config.unitName = 'bits'; + }); it('should format number to display correctly', inject(function($filter) { var noFraction = $filter('noFractionNumber'); - var fraction = 4; - expect(noFraction(3100, fraction)).to.equal('3,100'); - expect(noFraction(3100200, fraction)).to.equal('3,100,200'); - expect(noFraction(3, fraction)).to.equal('3'); - expect(noFraction(0.3, fraction)).to.equal(0.3); + expect(noFraction(3100)).to.equal('3,100'); + expect(noFraction(3100200)).to.equal('3,100,200'); + expect(noFraction(3)).to.equal('3'); + expect(noFraction(0.3)).to.equal(0.3); + expect(noFraction(0.30000000)).to.equal(0.3); + expect(noFraction(3200.01)).to.equal('3,200.01'); + expect(noFraction(3200890.010000)).to.equal('3,200,890.01'); + })); + }); + + describe('noFractionNumber BTC', function() { + beforeEach(function() { + config.unitToSatoshi = 100000000; + config.unitName = 'BTC'; + }); + it('should format number to display correctly', inject(function($filter) { + var noFraction = $filter('noFractionNumber'); + expect(noFraction(0.30000000)).to.equal(0.3); + expect(noFraction(0.00302000)).to.equal(0.00302); + expect(noFraction(1.00000001)).to.equal(1.00000001); + expect(noFraction(3.10000012)).to.equal(3.10000012); + expect(noFraction(0.00100000)).to.equal(0.001); + expect(noFraction(0.00100009)).to.equal(0.00100009); + expect(noFraction(2000.00312011)).to.equal('2,000.00312011'); + expect(noFraction(2000998.00312011)).to.equal('2,000,998.00312011'); + })); + }); + + describe('noFractionNumber mBTC', function() { + beforeEach(function() { + config.unitToSatoshi = 100000; + config.unitName = 'mBTC'; + }); + it('should format number to display correctly', inject(function($filter) { + var noFraction = $filter('noFractionNumber'); + expect(noFraction(0.30000)).to.equal(0.3); + expect(noFraction(0.00302)).to.equal(0.00302); + expect(noFraction(1.00001)).to.equal(1.00001); + expect(noFraction(3.10002)).to.equal(3.10002); + expect(noFraction(0.00100000)).to.equal(0.001); + expect(noFraction(0.00100009)).to.equal(0.001); + expect(noFraction(2000.00312)).to.equal('2,000.00312'); + expect(noFraction(2000998.00312)).to.equal('2,000,998.00312'); + })); + }); + + describe('noFractionNumber:custom fractionSize', function() { + it('should format number to display correctly', inject(function($filter) { + var noFraction = $filter('noFractionNumber'); + expect(noFraction(0.30000, 0)).to.equal('0'); + expect(noFraction(1.00001, 0)).to.equal('1'); + expect(noFraction(3.10002, 0)).to.equal('3'); + expect(noFraction(2000.00312, 0)).to.equal('2,000'); + expect(noFraction(2000998.00312, 0)).to.equal('2,000,998'); })); }); From f669f383e295cdc4ef121f4bf1ed9e578e7ebb38 Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Fri, 27 Jun 2014 09:53:18 -0300 Subject: [PATCH 4/4] Fixed karma test (rounded values) --- test/unit/services/servicesSpec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/services/servicesSpec.js b/test/unit/services/servicesSpec.js index 2c29c78ea..badab5154 100644 --- a/test/unit/services/servicesSpec.js +++ b/test/unit/services/servicesSpec.js @@ -91,8 +91,8 @@ describe("Unit: controllerUtils", function() { //retuns values in DEFAULT UNIT(bits) controllerUtils.updateBalance(function() { - expect($rootScope.totalBalanceBTC).to.be.equal('1.0000'); - expect($rootScope.availableBalanceBTC).to.be.equal('0.9000'); + expect($rootScope.totalBalanceBTC).to.be.equal(1.00000001); + expect($rootScope.availableBalanceBTC).to.be.equal(0.90000002); expect($rootScope.totalBalance).to.be.equal(1000000.01); expect($rootScope.availableBalance).to.be.equal(900000.02); expect($rootScope.addrInfos).not.to.equal(null);