mirror of
https://github.com/mempool/mempool.git
synced 2026-08-13 12:33:11 +02:00
Address copilot feedback
This commit is contained in:
parent
a1f51387f3
commit
f4dbf56ef9
2 changed files with 21 additions and 5 deletions
|
|
@ -1,13 +1,17 @@
|
|||
import { formatCurrency, getCurrencySymbol } from '@angular/common';
|
||||
import { Inject, LOCALE_ID, Pipe, PipeTransform } from '@angular/core';
|
||||
import { Inject, LOCALE_ID, OnDestroy, Pipe, PipeTransform } from '@angular/core';
|
||||
import { Subscription } from 'rxjs';
|
||||
import { StateService } from '@app/services/state.service';
|
||||
|
||||
// Below this threshold, show 2 decimal places even when digitsInfo requests 0,
|
||||
// so small fiat amounts (e.g. $3.50) remain readable instead of rounding to $4.
|
||||
const SMALL_FIAT_THRESHOLD = 1000;
|
||||
|
||||
@Pipe({
|
||||
name: 'fiatCurrency',
|
||||
standalone: false,
|
||||
})
|
||||
export class FiatCurrencyPipe implements PipeTransform {
|
||||
export class FiatCurrencyPipe implements PipeTransform, OnDestroy {
|
||||
fiatSubscription: Subscription;
|
||||
currency: string;
|
||||
private currencyMaxFracCache: Record<string, number> = {};
|
||||
|
|
@ -21,6 +25,10 @@ export class FiatCurrencyPipe implements PipeTransform {
|
|||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.fiatSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
private getCurrencyMaxFrac(currency: string): number {
|
||||
if (!(currency in this.currencyMaxFracCache)) {
|
||||
this.currencyMaxFracCache[currency] =
|
||||
|
|
@ -39,10 +47,14 @@ export class FiatCurrencyPipe implements PipeTransform {
|
|||
if (digitsInfo) {
|
||||
const match = digitsInfo.match(/^(\d+)\.(\d+)-(\d+)$/);
|
||||
if (match) {
|
||||
const minInt = parseInt(match[1], 10);
|
||||
const minFrac = parseInt(match[2], 10);
|
||||
const maxFrac = parseInt(match[3], 10);
|
||||
const currencyMaxFrac = this.getCurrencyMaxFrac(currency);
|
||||
if (maxFrac === 0 && Math.abs(num) < 1000) {
|
||||
if (minInt > 1) {
|
||||
options.minimumIntegerDigits = minInt;
|
||||
}
|
||||
if (maxFrac === 0 && Math.abs(num) < SMALL_FIAT_THRESHOLD) {
|
||||
options.minimumFractionDigits = Math.min(2, currencyMaxFrac);
|
||||
options.maximumFractionDigits = Math.min(2, currencyMaxFrac);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { formatCurrency, getCurrencySymbol } from '@angular/common';
|
||||
import { Inject, LOCALE_ID, Pipe, PipeTransform } from '@angular/core';
|
||||
import { Inject, LOCALE_ID, OnDestroy, Pipe, PipeTransform } from '@angular/core';
|
||||
import { Subscription } from 'rxjs';
|
||||
import { StateService } from '@app/services/state.service';
|
||||
|
||||
|
|
@ -7,7 +7,7 @@ import { StateService } from '@app/services/state.service';
|
|||
name: 'fiatShortener',
|
||||
standalone: false,
|
||||
})
|
||||
export class FiatShortenerPipe implements PipeTransform {
|
||||
export class FiatShortenerPipe implements PipeTransform, OnDestroy {
|
||||
fiatSubscription: Subscription;
|
||||
currency: string;
|
||||
|
||||
|
|
@ -20,6 +20,10 @@ export class FiatShortenerPipe implements PipeTransform {
|
|||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.fiatSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
transform(num: number, ...args: any[]): unknown {
|
||||
const digits = args[0] || 1;
|
||||
const currency = args[1] || this.currency || 'USD';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue