From 992196c91f7600578c079eefeaa77eead08e2a51 Mon Sep 17 00:00:00 2001 From: softsimon Date: Sat, 15 Jul 2023 15:09:41 +0900 Subject: [PATCH] Calculator validation improvements --- .../calculator/calculator.component.ts | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/frontend/src/app/components/calculator/calculator.component.ts b/frontend/src/app/components/calculator/calculator.component.ts index 838afbbd4..d99302f40 100644 --- a/frontend/src/app/components/calculator/calculator.component.ts +++ b/frontend/src/app/components/calculator/calculator.component.ts @@ -54,6 +54,9 @@ export class CalculatorComponent implements OnInit { ]).subscribe(([price, value]) => { const rate = (value / price).toFixed(8); const satsRate = Math.round(value / price * 100_000_000); + if (isNaN(value)) { + return; + } this.form.get('bitcoin').setValue(rate, { emitEvent: false }); this.form.get('satoshis').setValue(satsRate, { emitEvent: false } ); }); @@ -63,6 +66,9 @@ export class CalculatorComponent implements OnInit { this.form.get('bitcoin').valueChanges ]).subscribe(([price, value]) => { const rate = parseFloat((value * price).toFixed(8)); + if (isNaN(value)) { + return; + } this.form.get('fiat').setValue(rate, { emitEvent: false } ); this.form.get('satoshis').setValue(Math.round(value * 100_000_000), { emitEvent: false } ); }); @@ -73,6 +79,9 @@ export class CalculatorComponent implements OnInit { ]).subscribe(([price, value]) => { const rate = parseFloat((value / 100_000_000 * price).toFixed(8)); const bitcoinRate = (value / 100_000_000).toFixed(8); + if (isNaN(value)) { + return; + } this.form.get('fiat').setValue(rate, { emitEvent: false } ); this.form.get('bitcoin').setValue(bitcoinRate, { emitEvent: false }); }); @@ -88,7 +97,16 @@ export class CalculatorComponent implements OnInit { if (value === '.') { value = '0'; } - const sanitizedValue = this.removeExtraDots(value); + let sanitizedValue = this.removeExtraDots(value); + if (name === 'bitcoin' && this.countDecimals(sanitizedValue) > 8) { + sanitizedValue = this.toFixedWithoutRounding(sanitizedValue, 8); + } + if (sanitizedValue === '') { + sanitizedValue = '0'; + } + if (name === 'satoshis') { + sanitizedValue = parseFloat(sanitizedValue).toFixed(0); + } formControl.setValue(sanitizedValue, {emitEvent: true}); } @@ -100,4 +118,16 @@ export class CalculatorComponent implements OnInit { const afterDotReplaced = afterDot.replace(/\./g, ''); return `${beforeDot}.${afterDotReplaced}`; } + + countDecimals(numberString: string): number { + const decimalPos = numberString.indexOf('.'); + if (decimalPos === -1) return 0; + return numberString.length - decimalPos - 1; + } + + toFixedWithoutRounding(numStr: string, fixed: number): string { + const re = new RegExp(`^-?\\d+(?:.\\d{0,${(fixed || -1)}})?`); + const result = numStr.match(re); + return result ? result[0] : numStr; + } }