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

Commit f1dd040

Browse files
yitzhak-dashfknop
authored andcommitted
feat(bytes pipe): be able to change/set the output bytes unit (#60)
1 parent 65681cd commit f1dd040

File tree

4 files changed

+116
-76
lines changed

4 files changed

+116
-76
lines changed

docs/math.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
3131
{{ 1024 | bytes: 0 : 'KB' }} <!-- 1 MB -->
3232
{{ 1073741824 | bytes }} <!-- 1 GB -->
3333
{{ 1099511627776 | bytes }} <!-- 1 TB -->
34+
{{ 1073741824 | bytes : 0 : 'B' : 'MB' }} <!-- 1024 MB -->
3435
```
3536

3637
##### Todo

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"karma-firefox-launcher": "^1.0.0",
4949
"karma-jasmine": "^1.0.2",
5050
"karma-spec-reporter": "0.0.26",
51-
"karma-typescript": "next",
51+
"karma-typescript": "^3.0.9",
5252
"reflect-metadata": "^0.1.3",
5353
"rollup": "^0.41.4",
5454
"rollup-plugin-node-resolve": "^2.0.0",

src/math/bytes.pipe.spec.ts

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,112 @@
1-
import { BytesPipe } from './bytes.pipe';
2-
1+
import { BytesPipe, ByteUnit } from './bytes.pipe';
32

43

54
describe('BytesPipe', () => {
6-
5+
76
let pipe: BytesPipe;
8-
7+
98
beforeEach(() => {
10-
pipe = new BytesPipe();
9+
pipe = new BytesPipe();
1110
});
12-
13-
11+
12+
1413
it('Should return 150 B', () => {
15-
14+
1615
const result = pipe.transform(150, 0);
1716
expect(result).toEqual('150 B');
1817
});
19-
20-
18+
19+
2120
it('Should return 155.57 B', () => {
22-
21+
2322
const result = pipe.transform(155.56791, 2);
2423
expect(result).toEqual('155.57 B');
2524
});
26-
25+
2726
it('Should return 155.5 B', () => {
28-
27+
2928
const result = pipe.transform(155.5, 1);
3029
expect(result).toEqual('155.5 B');
3130
});
32-
33-
31+
32+
3433
it('Should return 1 KB', () => {
35-
34+
3635
const result = pipe.transform(1024, 0);
3736
expect(result).toEqual('1 KB');
3837
});
3938

4039
it('Should return 1 KB #2', () => {
41-
40+
4241
const result = pipe.transform(1, 0, 'KB');
4342
expect(result).toEqual('1 KB');
4443
});
45-
44+
4645
it('Should return 890 KB', () => {
47-
46+
4847
const kb = 1024 * 890;
4948
const result = pipe.transform(kb, 0);
5049
expect(result).toEqual('890 KB');
5150
});
5251

5352
it('Should return 890 KB #2', () => {
54-
53+
5554
const result = pipe.transform(890, 0, 'KB');
5655
expect(result).toEqual('890 KB');
5756
});
58-
59-
57+
58+
6059
it('Should return 1023 KB', () => {
61-
60+
6261
const kb = 1024 * 1023;
6362
const result = pipe.transform(kb, 0);
6463
expect(result).toEqual('1023 KB');
6564
});
66-
65+
6766
it('Should return 241 MB', () => {
68-
67+
6968
const mb = 1024 * 1024 * 240.5691;
7069
const result = pipe.transform(mb, 0);
7170
expect(result).toEqual('241 MB');
7271
});
7372

74-
it('Should return 241 MB', () => {
75-
73+
it('Should return 241 MB', () => {
74+
7675
const mb = 240.5691 / 1024;
7776
const result = pipe.transform(mb, 0, 'GB');
7877
expect(result).toEqual('241 MB');
7978
});
80-
79+
8180
it('Should return 240.54 MB', () => {
82-
81+
8382
const mb = 1024 * 1024 * 240.5411;
8483
const result = pipe.transform(mb, 2);
8584
expect(result).toEqual('240.54 MB');
8685
});
87-
86+
8887
it('Should return 1023 MB', () => {
89-
88+
9089
const mb = 1024 * 1024 * 1023;
9190
const result = pipe.transform(mb, 2);
9291
expect(result).toEqual('1023 MB');
9392
});
9493

95-
it('Should return 1023 MB #2', () => {
96-
94+
it('Should return 1023 MB #2', () => {
95+
9796
const kb = 1024 * 1023;
9897
const result = pipe.transform(kb, 2, 'KB');
9998
expect(result).toEqual('1023 MB');
10099
});
101-
100+
102101
it('Should return 1023 GB', () => {
103-
102+
104103
const gb = 1024 * 1024 * 1024 * 1023;
105104
const result = pipe.transform(gb, 2);
106105
expect(result).toEqual('1023 GB');
107106
});
108107

109-
it('Should return 1.03 TB', () => {
110-
108+
it('Should return 1.03 TB', () => {
109+
111110
const gb = 1024 * 1024 * 1024 * 1059;
112111
const result = pipe.transform(gb, 2);
113112
expect(result).toEqual('1.03 TB');
@@ -117,5 +116,34 @@ describe('BytesPipe', () => {
117116
it('Should return the input', () => {
118117
expect(pipe.transform('a')).toEqual('a');
119118
});
120-
119+
120+
it('Should return 100 TB', () => {
121+
const bytes = 100;
122+
const unit = 'TB';
123+
const result = BytesPipe.formatResult(bytes, unit);
124+
expect(result).toEqual('100 TB');
125+
});
126+
127+
it('Should return 1', () => {
128+
const format = {max: Math.pow(1024, 4), prev: 'MB' as ByteUnit};
129+
const bytes = 1024 * 1024 * 1024;
130+
const result = BytesPipe.calculateResult(format, bytes);
131+
expect(result).toEqual(1);
132+
});
133+
134+
it('Should return 1024 MB, set output unit', () => {
135+
const bytes = 1024 * 1024 * 1024;
136+
const from = 'B' as ByteUnit;
137+
const to = 'MB' as ByteUnit;
138+
const result = pipe.transform(bytes, 0, from, to);
139+
expect(result).toEqual('1024 MB')
140+
});
141+
142+
it('Should return 1024 MB, from & to units are equals', () => {
143+
const bytes = 1024;
144+
const from = 'MB' as ByteUnit;
145+
const to = 'MB' as ByteUnit;
146+
const result = pipe.transform(bytes, 0, from, to);
147+
expect(result).toEqual('1024 MB')
148+
})
121149
});

src/math/bytes.pipe.ts

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

44
export type ByteUnit = 'B' | 'KB' | 'MB' | 'GB' | 'TB';
55

66

7-
8-
97
@Pipe({
10-
name: 'bytes'
8+
name: 'bytes'
119
})
1210
export class BytesPipe implements PipeTransform {
1311

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-
22-
23-
transform (input: any, decimal: number = 0, from: ByteUnit = 'B'): any {
24-
25-
if (!(isNumberFinite(input) &&
26-
isNumberFinite(decimal) &&
27-
isInteger(decimal) &&
28-
isPositive(decimal))) {
29-
return input;
30-
}
12+
static formats: { [key: string]: { max: number, prev?: ByteUnit } } = {
13+
'B': {max: 1024},
14+
'KB': {max: Math.pow(1024, 2), prev: 'B'},
15+
'MB': {max: Math.pow(1024, 3), prev: 'KB'},
16+
'GB': {max: Math.pow(1024, 4), prev: 'MB'},
17+
'TB': {max: Number.MAX_SAFE_INTEGER, prev: 'GB'}
18+
};
19+
20+
21+
transform(input: any, decimal: number = 0, from: ByteUnit = 'B', to?: ByteUnit): any {
22+
23+
if (!(isNumberFinite(input) &&
24+
isNumberFinite(decimal) &&
25+
isInteger(decimal) &&
26+
isPositive(decimal))) {
27+
return input;
28+
}
29+
30+
let bytes = input;
31+
let unit = from;
32+
while (unit !== 'B') {
33+
bytes *= 1024;
34+
unit = BytesPipe.formats[unit].prev!;
35+
}
3136

32-
let bytes = input;
33-
let unit = from;
34-
while (unit !== 'B') {
35-
bytes *= 1024;
36-
unit = BytesPipe.formats[unit].prev!;
37+
if (to) {
38+
const format = BytesPipe.formats[to];
39+
40+
const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal);
41+
42+
return BytesPipe.formatResult(result, to);
43+
}
44+
45+
for (const key in BytesPipe.formats) {
46+
const format = BytesPipe.formats[key];
47+
if (bytes < format.max) {
48+
49+
const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal);
50+
51+
return BytesPipe.formatResult(result, key);
52+
}
53+
}
3754
}
3855

39-
for (const key in BytesPipe.formats) {
40-
const format = BytesPipe.formats[key];
41-
if (bytes < format.max) {
56+
static formatResult(result: number, unit: string): string {
57+
return `${result} ${unit}`;
58+
}
4259

60+
static calculateResult(format: { max: number, prev?: ByteUnit }, bytes: number) {
4361
const prev = format.prev ? BytesPipe.formats[format.prev] : undefined;
44-
45-
const result = prev ?
46-
toDecimal(bytes / prev.max, decimal) :
47-
toDecimal(bytes, decimal);
48-
49-
return `${result} ${key}`;
50-
}
62+
return prev ? bytes / prev.max : bytes;
5163
}
52-
}
5364
}

0 commit comments

Comments
 (0)