Use correct url for blocks-extras API - Fix amountShortner pipe

This commit is contained in:
nymkappa 2022-03-13 11:37:56 +01:00
parent 33897b029f
commit ab486bfe6e
No known key found for this signature in database
GPG key ID: E155910B16E8BD04
4 changed files with 34 additions and 17 deletions

View file

@ -17,7 +17,7 @@
</td> </td>
<td class="text-right">{{ diffChange.difficultyShorten }}</td> <td class="text-right">{{ diffChange.difficultyShorten }}</td>
<td class="text-right" [style]="diffChange.change >= 0 ? 'color: #42B747' : 'color: #B74242'"> <td class="text-right" [style]="diffChange.change >= 0 ? 'color: #42B747' : 'color: #B74242'">
{{ diffChange.change >= 0 ? '+' : '' }}{{ formatNumber(diffChange.change, locale, '1.2-2') }}% {{ diffChange.change >= 0 ? '+' : '' }}{{ diffChange.change | amountShortener }}%
</td> </td>
</tr> </tr>
</tbody> </tbody>

View file

@ -43,7 +43,7 @@ export class DifficultyAdjustmentsTable implements OnInit {
const change = (data.difficulty[i].difficulty / data.difficulty[i - 1].difficulty - 1) * 100; const change = (data.difficulty[i].difficulty / data.difficulty[i - 1].difficulty - 1) * 100;
tableData.push(Object.assign(data.difficulty[i], { tableData.push(Object.assign(data.difficulty[i], {
change: change, change: Math.round(change * 100) / 100,
difficultyShorten: formatNumber( difficultyShorten: formatNumber(
data.difficulty[i].difficulty / selectedPowerOfTen.divider, data.difficulty[i].difficulty / selectedPowerOfTen.divider,
this.locale, '1.2-2') + selectedPowerOfTen.unit this.locale, '1.2-2') + selectedPowerOfTen.unit

View file

@ -153,7 +153,7 @@ export class ApiService {
getBlocks$(from: number): Observable<BlockExtended[]> { getBlocks$(from: number): Observable<BlockExtended[]> {
return this.httpClient.get<BlockExtended[]>( return this.httpClient.get<BlockExtended[]>(
this.apiBasePath + this.apiBasePath + `/api/v1/blocks-extras` + this.apiBaseUrl + this.apiBasePath + `/api/v1/blocks-extras` +
(from !== undefined ? `/${from}` : ``) (from !== undefined ? `/${from}` : ``)
); );
} }

View file

@ -1,22 +1,39 @@
import { Pipe, PipeTransform } from '@angular/core'; import { Pipe, PipeTransform } from '@angular/core';
// https://medium.com/@thunderroid/angular-short-number-suffix-pipe-1k-2m-3b-dded4af82fb4
@Pipe({ @Pipe({
name: 'amountShortener' name: 'amountShortener'
}) })
export class AmountShortenerPipe implements PipeTransform { export class AmountShortenerPipe implements PipeTransform {
transform(num: number, ...args: number[]): unknown { transform(number: number, args?: any): any {
const digits = args[0] || 1; if (isNaN(number)) return null; // will only work value is a number
const lookup = [ if (number === null) return null;
{ value: 1, symbol: '' }, if (number === 0) return null;
{ value: 1e3, symbol: 'k' }, let abs = Math.abs(number);
{ value: 1e6, symbol: 'M' }, const rounder = Math.pow(10, 1);
{ value: 1e9, symbol: 'G' }, const isNegative = number < 0; // will also work for Negetive numbers
{ value: 1e12, symbol: 'T' }, let key = '';
{ value: 1e15, symbol: 'P' },
{ value: 1e18, symbol: 'E' } const powers = [
{ key: 'E', value: 10e18 },
{ key: 'P', value: 10e15 },
{ key: 'T', value: 10e12 },
{ key: 'B', value: 10e9 },
{ key: 'M', value: 10e6 },
{ key: 'K', value: 1000 }
]; ];
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
var item = lookup.slice().reverse().find((item) => num >= item.value); for (let i = 0; i < powers.length; i++) {
return item ? (num / item.value).toFixed(digits).replace(rx, '$1') + item.symbol : '0'; let reduced = abs / powers[i].value;
reduced = Math.round(reduced * rounder) / rounder;
if (reduced >= 1) {
abs = reduced;
key = powers[i].key;
break;
}
}
return (isNegative ? '-' : '') + abs + key;
} }
} }