From 2d6ee6655c26debca43f3bf248f8521e793117f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adina=20=C8=9Aeudan?= Date: Thu, 29 Jun 2023 16:13:02 +0300 Subject: [PATCH] RED-6830: Refactor size format pipe --- src/lib/pipes/size.pipe.ts | 6 ++---- src/lib/utils/functions.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib/pipes/size.pipe.ts b/src/lib/pipes/size.pipe.ts index a2900eb..2bece27 100644 --- a/src/lib/pipes/size.pipe.ts +++ b/src/lib/pipes/size.pipe.ts @@ -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); } } diff --git a/src/lib/utils/functions.ts b/src/lib/utils/functions.ts index 21c2afe..6f99ad6 100644 --- a/src/lib/utils/functions.ts +++ b/src/lib/utils/functions.ts @@ -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(unsafe: T, options?: { ignoreTags: string[] }) { if (typeof unsafe !== 'string') { return unsafe;