Add file size to file metadata
Signed-off-by: trivernis <trivernis@protonmail.com>pull/14/head
parent
fd1d3e128c
commit
6e150a3701
@ -0,0 +1,8 @@
|
||||
import {FormatBytesPipe} from "./format-bytes.pipe";
|
||||
|
||||
describe("FormatBytesPipe", () => {
|
||||
it("create an instance", () => {
|
||||
const pipe = new FormatBytesPipe();
|
||||
expect(pipe).toBeTruthy();
|
||||
});
|
||||
});
|
@ -0,0 +1,24 @@
|
||||
import {Pipe, PipeTransform} from "@angular/core";
|
||||
|
||||
@Pipe({
|
||||
name: "formatBytes"
|
||||
})
|
||||
export class FormatBytesPipe implements PipeTransform {
|
||||
|
||||
static round(number: number, decimals: number) {
|
||||
return Math.round(number * (10 ** decimals)) / (10 ** decimals);
|
||||
}
|
||||
|
||||
transform(value: number): string {
|
||||
const units = ["B", "KiB", "MiB", "GiB"];
|
||||
let formattedValue = value;
|
||||
|
||||
for (const unit of units) {
|
||||
if (formattedValue < 1000) {
|
||||
return `${formattedValue} ${unit}`;
|
||||
}
|
||||
formattedValue = FormatBytesPipe.round(formattedValue / 1024, 2);
|
||||
}
|
||||
return formattedValue + " GiB";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue