Skip to content

Commit 78eed3c

Browse files
committed
✨ add pipe for file size
1 parent 9ba27f2 commit 78eed3c

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { FileSizePipe } from './file-size.pipe';
2+
3+
describe('FileSizePipe', () => {
4+
it('create an instance', () => {
5+
const pipe = new FileSizePipe();
6+
7+
expect(pipe).toBeTruthy();
8+
});
9+
});

src/app/core/pipes/file-size.pipe.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'fileSize'
5+
})
6+
export class FileSizePipe implements PipeTransform {
7+
8+
transform(value: string | number): string {
9+
return this.formatBytes(+value);
10+
}
11+
12+
private formatBytes(bytes: number, decimals = 2): string {
13+
if (bytes === 0) {
14+
return '0 Bytes';
15+
}
16+
const k = 1024;
17+
const dm = decimals < 0 ? 0 : decimals;
18+
const sizes = ['Bytes', 'KB', 'MB'];
19+
const i = Math.floor(Math.log(bytes) / Math.log(k));
20+
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
21+
}
22+
23+
}

src/app/core/pipes/pipes.module.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { CommonModule } from '@angular/common';
2+
import { NgModule } from '@angular/core';
3+
4+
import { FileSizePipe } from './file-size.pipe';
5+
6+
@NgModule({
7+
declarations: [
8+
FileSizePipe
9+
],
10+
imports: [
11+
CommonModule
12+
],
13+
exports: [
14+
FileSizePipe
15+
]
16+
})
17+
export class PipesModule { }

src/app/shared/modals/modals.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { CommonModule } from '@angular/common';
22
import { NgModule } from '@angular/core';
33
import { FormsModule } from '@angular/forms';
44

5+
import { PipesModule } from '@pipes/pipes.module';
6+
57
import { NewFolderComponent } from './new-folder/new-folder.component';
68
import { UploadFilesComponent } from './upload-files/upload-files.component';
79

@@ -13,6 +15,7 @@ import { UploadFilesComponent } from './upload-files/upload-files.component';
1315
imports: [
1416
CommonModule,
1517
FormsModule,
18+
PipesModule
1619
],
1720
exports: [
1821
NewFolderComponent,

src/app/shared/modals/upload-files/upload-files.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<i class="bx bx-file"></i>
2121
</div>
2222
<span class="file-name">{{ file.name }}</span>
23-
<span class="file-size">{{ file.size }}</span>
23+
<span class="file-size">{{ file.size | fileSize }}</span>
2424
</div>
2525
<div class="file-actions">
2626
<i class="bx bx-x" (click)="removeFile(i)"></i>

src/app/shared/modals/upload-files/upload-files.component.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@
6363
@include fs-6;
6464
@include fw-500;
6565
color: var(--fc-primary);
66-
flex: 6;
66+
flex: 10;
6767
}
6868

6969
& .file-size {
7070
@include fs-6;
71-
flex: 1;
71+
flex: 2;
7272
color: var(--fc-secondary);
7373
}
7474
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@interceptors/*": [ "app/core/interceptors/*" ],
3333
"@guards/*": [ "app/core/guards/*" ],
3434
"@constants/*": [ "app/core/constants/*" ],
35+
"@pipes/*": [ "app/core/pipes/*" ],
3536
"@shared/*": [ "app/shared/*" ],
3637
}
3738
},

0 commit comments

Comments
 (0)