Browse Source

Fix handling of values rounding to zero

pull/7201/head
Thomas Kaul 2 weeks ago
parent
commit
8280d67638
  1. 12
      apps/api/src/services/benchmark/benchmark.service.spec.ts
  2. 8
      apps/api/src/services/benchmark/benchmark.service.ts
  3. 10
      libs/ui/src/lib/benchmark/benchmark.component.html
  4. 3
      libs/ui/src/lib/benchmark/benchmark.component.ts
  5. 4
      libs/ui/src/lib/value/value.component.ts

12
apps/api/src/services/benchmark/benchmark.service.spec.ts

@ -12,4 +12,16 @@ describe('BenchmarkService', () => {
expect(benchmarkService.calculateChangeInPercentage(2, 2)).toEqual(0); expect(benchmarkService.calculateChangeInPercentage(2, 2)).toEqual(0);
expect(benchmarkService.calculateChangeInPercentage(2, 1)).toEqual(-0.5); expect(benchmarkService.calculateChangeInPercentage(2, 1)).toEqual(-0.5);
}); });
it('getMarketCondition', async () => {
expect(benchmarkService.getMarketCondition(0)).toEqual('ALL_TIME_HIGH');
expect(benchmarkService.getMarketCondition(-5.90736454893e-9)).toEqual(
'ALL_TIME_HIGH'
);
expect(benchmarkService.getMarketCondition(-0.1)).toEqual('NEUTRAL_MARKET');
expect(benchmarkService.getMarketCondition(-0.19996)).toEqual(
'BEAR_MARKET'
);
expect(benchmarkService.getMarketCondition(-0.2)).toEqual('BEAR_MARKET');
});
}); });

8
apps/api/src/services/benchmark/benchmark.service.ts

@ -24,7 +24,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { SymbolProfile } from '@prisma/client'; import { SymbolProfile } from '@prisma/client';
import { Big } from 'big.js'; import { Big } from 'big.js';
import { addHours, isAfter, subDays } from 'date-fns'; import { addHours, isAfter, subDays } from 'date-fns';
import { uniqBy } from 'lodash'; import { round, uniqBy } from 'lodash';
import ms from 'ms'; import ms from 'ms';
import { BenchmarkValue } from './interfaces/benchmark-value.interface'; import { BenchmarkValue } from './interfaces/benchmark-value.interface';
@ -220,9 +220,11 @@ export class BenchmarkService {
public getMarketCondition( public getMarketCondition(
aPerformanceInPercent: number aPerformanceInPercent: number
): Benchmark['marketCondition'] { ): Benchmark['marketCondition'] {
if (aPerformanceInPercent >= 0) { const performanceInPercent = round(aPerformanceInPercent, 4);
if (performanceInPercent >= 0) {
return 'ALL_TIME_HIGH'; return 'ALL_TIME_HIGH';
} else if (aPerformanceInPercent <= -0.2) { } else if (performanceInPercent <= -0.2) {
return 'BEAR_MARKET'; return 'BEAR_MARKET';
} else { } else {
return 'NEUTRAL_MARKET'; return 'NEUTRAL_MARKET';

10
libs/ui/src/lib/benchmark/benchmark.component.html

@ -135,9 +135,15 @@
class="d-inline-block justify-content-end" class="d-inline-block justify-content-end"
[class]="{ [class]="{
'text-danger': 'text-danger':
element?.performances?.allTimeHigh?.performancePercent < 0, round(
element?.performances?.allTimeHigh?.performancePercent,
4
) < 0,
'text-success': 'text-success':
element?.performances?.allTimeHigh?.performancePercent === 0 round(
element?.performances?.allTimeHigh?.performancePercent,
4
) === 0
}" }"
[isPercent]="true" [isPercent]="true"
[locale]="locale()" [locale]="locale()"

3
libs/ui/src/lib/benchmark/benchmark.component.ts

@ -33,7 +33,7 @@ import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { IonIcon } from '@ionic/angular/standalone'; import { IonIcon } from '@ionic/angular/standalone';
import { addIcons } from 'ionicons'; import { addIcons } from 'ionicons';
import { ellipsisHorizontal, trashOutline } from 'ionicons/icons'; import { ellipsisHorizontal, trashOutline } from 'ionicons/icons';
import { isNumber } from 'lodash'; import { isNumber, round } from 'lodash';
import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader'; import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
import { GfEntityLogoComponent } from '../entity-logo/entity-logo.component'; import { GfEntityLogoComponent } from '../entity-logo/entity-logo.component';
@ -92,6 +92,7 @@ export class GfBenchmarkComponent {
protected isLoading = true; protected isLoading = true;
protected readonly isNumber = isNumber; protected readonly isNumber = isNumber;
protected readonly resolveMarketCondition = resolveMarketCondition; protected readonly resolveMarketCondition = resolveMarketCondition;
protected readonly round = round;
protected readonly translate = translate; protected readonly translate = translate;
private readonly destroyRef = inject(DestroyRef); private readonly destroyRef = inject(DestroyRef);

4
libs/ui/src/lib/value/value.component.ts

@ -168,7 +168,9 @@ export class GfValueComponent implements AfterViewInit, OnChanges {
} }
} }
if (this.formattedValue === '0.00') { if (/^-?0([.,]0*)?$/.test(this.formattedValue)) {
// Remove algebraic sign of values rounding to zero
this.formattedValue = this.formattedValue.replace(/^-/, '');
this.useAbsoluteValue = true; this.useAbsoluteValue = true;
} }
} }

Loading…
Cancel
Save