RED-6830: Refactor size format pipe

This commit is contained in:
Adina Țeudan 2023-06-29 16:13:02 +03:00
parent aa4516286e
commit 2d6ee6655c
2 changed files with 14 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import { Pipe, PipeTransform } from '@angular/core';
import { size } from '../utils';
@Pipe({
name: 'size',
@ -6,9 +7,6 @@ import { Pipe, PipeTransform } from '@angular/core';
})
export class SizePipe implements PipeTransform {
transform(value: number): string {
if (value >= 1000 ** 3) {
return `${(value / 1000 ** 3).toFixed(2)} GB`;
}
return `${(value / 1000 ** 2).toFixed(2)} MB`;
return size(value);
}
}

View File

@ -25,6 +25,18 @@ export function humanizeCamelCase(value: string): string {
return value.replace(/([a-z])([A-Z])/g, '$1 $2').toLowerCase();
}
export function size(value: number): string {
if (value >= 1000 ** 3) {
return `${(value / 1000 ** 3).toFixed(2)} GB`;
}
if (value >= 1000 ** 2) {
return `${(value / 1000 ** 2).toFixed(2)} MB`;
}
return `${(value / 1000).toFixed(2)} KB`;
}
export function escapeHtml<T extends unknown | string>(unsafe: T, options?: { ignoreTags: string[] }) {
if (typeof unsafe !== 'string') {
return unsafe;