Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit cd1dd63

Browse files
committed
Bytes pipe 2.0
1 parent d910baa commit cd1dd63

File tree

5 files changed

+84
-29
lines changed

5 files changed

+84
-29
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 6.5.0
2+
3+
## New feature
4+
5+
* Byte pipes 2.0 [`bytes`](./docs/math.md#bytes)
6+
+ The maximum unit is now the terabyte
7+
+ You can now specify a base unit for the conversion (B, KB, MB, GB, TB)
8+
19
# 6.4.0
210

311
## New pipes

docs/math.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
2828
{{ 150 | bytes }} <!-- 150 B -->
2929
{{ 1024 | bytes }} <!-- 1 KB -->
3030
{{ 1048576 | bytes }} <!-- 1 MB -->
31+
{{ 1024 | bytes: 0 : 'KB' }} <!-- 1 MB -->
3132
{{ 1073741824 | bytes }} <!-- 1 GB -->
33+
{{ 1099511627776 | bytes }} <!-- 1 TB -->
3234
```
3335

3436
##### Todo

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-pipes",
3-
"version": "6.4.0",
3+
"version": "6.5.0",
44
"description": "Angular pipes library",
55
"main": "src/index.js",
66
"jsnext:main": "esm/index.js",

src/math/bytes.pipe.spec.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ describe('BytesPipe', () => {
3636
const result = pipe.transform(1024, 0);
3737
expect(result).toEqual('1 KB');
3838
});
39+
40+
it('Should return 1 KB #2', () => {
41+
42+
const result = pipe.transform(1, 0, 'KB');
43+
expect(result).toEqual('1 KB');
44+
});
3945

4046
it('Should return 890 KB', () => {
4147

4248
const kb = 1024 * 890;
4349
const result = pipe.transform(kb, 0);
4450
expect(result).toEqual('890 KB');
4551
});
52+
53+
it('Should return 890 KB #2', () => {
54+
55+
const result = pipe.transform(890, 0, 'KB');
56+
expect(result).toEqual('890 KB');
57+
});
4658

4759

4860
it('Should return 1023 KB', () => {
@@ -58,6 +70,13 @@ describe('BytesPipe', () => {
5870
const result = pipe.transform(mb, 0);
5971
expect(result).toEqual('241 MB');
6072
});
73+
74+
it('Should return 241 MB', () => {
75+
76+
const mb = 240.5691 / 1024;
77+
const result = pipe.transform(mb, 0, 'GB');
78+
expect(result).toEqual('241 MB');
79+
});
6180

6281
it('Should return 240.54 MB', () => {
6382

@@ -72,14 +91,29 @@ describe('BytesPipe', () => {
7291
const result = pipe.transform(mb, 2);
7392
expect(result).toEqual('1023 MB');
7493
});
94+
95+
it('Should return 1023 MB #2', () => {
96+
97+
const kb = 1024 * 1023;
98+
const result = pipe.transform(kb, 2, 'KB');
99+
expect(result).toEqual('1023 MB');
100+
});
75101

76-
it('Should return 1059 GB', () => {
102+
it('Should return 1023 GB', () => {
103+
104+
const gb = 1024 * 1024 * 1024 * 1023;
105+
const result = pipe.transform(gb, 2);
106+
expect(result).toEqual('1023 GB');
107+
});
108+
109+
it('Should return 1.03 TB', () => {
77110

78111
const gb = 1024 * 1024 * 1024 * 1059;
79112
const result = pipe.transform(gb, 2);
80-
expect(result).toEqual('1059 GB');
113+
expect(result).toEqual('1.03 TB');
81114
});
82115

116+
83117
it('Should return the input', () => {
84118
expect(pipe.transform('a')).toEqual('a');
85119
});

src/math/bytes.pipe.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,53 @@
11
import { Pipe, PipeTransform } from '@angular/core';
22
import { isNumberFinite, isPositive, isInteger, toDecimal } from '../utils/utils';
33

4+
export type ByteUnit = 'B' | 'KB' | 'MB' | 'GB' | 'TB';
5+
6+
7+
48

59
@Pipe({
610
name: 'bytes'
711
})
812
export class BytesPipe implements PipeTransform {
13+
14+
static formats: { [key: string]: { max: number, prev?: ByteUnit }} = {
15+
'B': { max: 1024 },
16+
'KB': { max: Math.pow(1024, 2), prev: 'B' },
17+
'MB': { max: Math.pow(1024, 3), prev: 'KB' },
18+
'GB': { max: Math.pow(1024, 4), prev: 'MB' },
19+
'TB': { max: Number.MAX_SAFE_INTEGER, prev: 'GB' }
20+
};
21+
922

10-
transform (input: any, decimal: number = 0): any {
11-
12-
const formats = [
13-
{ name: 'B', max: 1024 },
14-
{ name: 'KB', max: 1048576 },
15-
{ name: 'MB', max: 1073741824 },
16-
{ name: 'GB', max: Number.MAX_VALUE }
17-
];
23+
transform (input: any, decimal: number = 0, from: ByteUnit = 'B'): any {
1824

19-
if (isNumberFinite(input) &&
25+
if (!(isNumberFinite(input) &&
2026
isNumberFinite(decimal) &&
2127
isInteger(decimal) &&
22-
isPositive(decimal)) {
23-
24-
for (let i = 0; i < formats.length; ++i) {
25-
const format = formats[i];
26-
if (input < format.max) {
27-
28-
const bytes = formats[i - 1] ?
29-
toDecimal(input / formats[i - 1].max, decimal) :
30-
toDecimal(input, decimal);
31-
32-
return `${bytes} ${format.name}`;
33-
}
34-
35-
}
36-
28+
isPositive(decimal))) {
29+
return input;
3730
}
38-
else {
39-
return input;
31+
32+
let bytes = input;
33+
let unit = from;
34+
while (unit != 'B') {
35+
bytes *= 1024;
36+
unit = BytesPipe.formats[unit].prev;
37+
}
38+
39+
for (const key in BytesPipe.formats) {
40+
const format = BytesPipe.formats[key];
41+
if (bytes < format.max) {
42+
43+
const prev = BytesPipe.formats[format.prev];
44+
45+
const result = prev ?
46+
toDecimal(bytes / prev.max, decimal) :
47+
toDecimal(bytes, decimal);
48+
49+
return `${result} ${key}`;
50+
}
4051
}
4152
}
4253
}

0 commit comments

Comments
 (0)