import { Pipe, PipeTransform } from '@angular/core';

/**
 * convert string tu data format with units
 *
 * Usage:
 *   value | data
 * Example:
 *   {{ '102' | data }}
 *   return 102 Kb
 *
 *   {{ '1024' | data }}
 *   return 1 Mb
 *
 *   {{ '10241047' | data }}
 *   return 1 Gb
 */
@Pipe({ name: 'data' })
export class DataPipe implements PipeTransform {
    transform(value: string): string {
        const amount = +value;

        if (amount >= 1048576) {
            return `${Math.round(amount / 1048576 * 1000) / 1000} Gb`;
        } else if (amount >= 1024) {
            return `${Math.round(amount / 1024 * 1000) / 1000} Mb`;
        } else {
            return `${amount} Kb`;
        }
    }
}
