Block view.

This commit is contained in:
Simon Lindh
2019-11-12 16:39:59 +08:00
parent bd2bd478ef
commit 309e851ead
24 changed files with 455 additions and 126 deletions

View File

@@ -0,0 +1,63 @@
/* tslint:disable */
import { Pipe, PipeTransform } from '@angular/core';
import { isNumberFinite, isPositive, isInteger, toDecimal } from './utils';
export type ByteUnit = 'WU' | 'kWU' | 'MWU' | 'GWU' | 'TWU';
@Pipe({
name: 'wuBytes'
})
export class WuBytesPipe implements PipeTransform {
static formats: { [key: string]: { max: number, prev?: ByteUnit } } = {
'WU': {max: 1000},
'kWU': {max: Math.pow(1000, 2), prev: 'WU'},
'MWU': {max: Math.pow(1000, 3), prev: 'kWU'},
'GWU': {max: Math.pow(1000, 4), prev: 'MWU'},
'TWU': {max: Number.MAX_SAFE_INTEGER, prev: 'GWU'}
};
transform(input: any, decimal: number = 0, from: ByteUnit = 'WU', to?: ByteUnit): any {
if (!(isNumberFinite(input) &&
isNumberFinite(decimal) &&
isInteger(decimal) &&
isPositive(decimal))) {
return input;
}
let bytes = input;
let unit = from;
while (unit !== 'WU') {
bytes *= 1024;
unit = WuBytesPipe.formats[unit].prev!;
}
if (to) {
const format = WuBytesPipe.formats[to];
const result = toDecimal(WuBytesPipe.calculateResult(format, bytes), decimal);
return WuBytesPipe.formatResult(result, to);
}
for (const key in WuBytesPipe.formats) {
const format = WuBytesPipe.formats[key];
if (bytes < format.max) {
const result = toDecimal(WuBytesPipe.calculateResult(format, bytes), decimal);
return WuBytesPipe.formatResult(result, key);
}
}
}
static formatResult(result: number, unit: string): string {
return `${result} ${unit}`;
}
static calculateResult(format: { max: number, prev?: ByteUnit }, bytes: number) {
const prev = format.prev ? WuBytesPipe.formats[format.prev] : undefined;
return prev ? bytes / prev.max : bytes;
}
}

View File

@@ -0,0 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'shortenString' })
export class ShortenStringPipe implements PipeTransform {
transform(str: string, length: number = 12) {
const half = length / 2;
return str.substring(0, half) + '...' + str.substring(str.length - half);
}
}

View File

@@ -8,6 +8,8 @@ import { RoundPipe } from './pipes/math-round-pipe/math-round.pipe';
import { CeilPipe } from './pipes/math-ceil/math-ceil.pipe';
import { ChartistComponent } from '../statistics/chartist.component';
import { TimeSincePipe } from './pipes/time-since/time-since.pipe';
import { WuBytesPipe } from './pipes/bytes-pipe/wubytes.pipe';
import { ShortenStringPipe } from './pipes/shorten-string-pipe/shorten-string.pipe';
@NgModule({
imports: [
@@ -21,6 +23,8 @@ import { TimeSincePipe } from './pipes/time-since/time-since.pipe';
CeilPipe,
BytesPipe,
VbytesPipe,
WuBytesPipe,
ShortenStringPipe,
TimeSincePipe,
],
exports: [
@@ -28,7 +32,9 @@ import { TimeSincePipe } from './pipes/time-since/time-since.pipe';
CeilPipe,
BytesPipe,
VbytesPipe,
WuBytesPipe,
TimeSincePipe,
ShortenStringPipe,
NgbButtonsModule,
NgbModalModule,
ChartistComponent,
@@ -36,6 +42,8 @@ import { TimeSincePipe } from './pipes/time-since/time-since.pipe';
providers: [
BytesPipe,
VbytesPipe,
WuBytesPipe,
ShortenStringPipe,
]
})
export class SharedModule { }