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

Commit ed59efa

Browse files
committed
add backward compatible way to specify KB instead of kB as a unit. Closes #70
1 parent 8297eeb commit ed59efa

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/math/bytes.pipe.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ describe('BytesPipe', () => {
4242
expect(result).toEqual('1 kB');
4343
});
4444

45+
it('Should return 1 kB #3', () => {
46+
47+
const result = pipe.transform(1, 0, 'KB');
48+
expect(result).toEqual('1 kB');
49+
});
50+
4551
it('Should return 890 kB', () => {
4652

4753
const kB = 1024 * 890;
@@ -55,6 +61,12 @@ describe('BytesPipe', () => {
5561
expect(result).toEqual('890 kB');
5662
});
5763

64+
it('Should return 890 kB #3', () => {
65+
66+
const result = pipe.transform(890, 0, 'KB');
67+
expect(result).toEqual('890 kB');
68+
});
69+
5870

5971
it('Should return 1023 kB', () => {
6072

src/math/bytes.pipe.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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';
4+
export type ByteUnit = 'B' | 'kB' | 'KB' | 'MB' | 'GB' | 'TB';
55

66

77
@Pipe({
@@ -12,6 +12,7 @@ export class BytesPipe implements PipeTransform {
1212
static formats: { [key: string]: { max: number, prev?: ByteUnit } } = {
1313
'B': {max: 1024},
1414
'kB': {max: Math.pow(1024, 2), prev: 'B'},
15+
'KB': {max: Math.pow(1024, 2), prev: 'B'}, // Backward compatible
1516
'MB': {max: Math.pow(1024, 3), prev: 'kB'},
1617
'GB': {max: Math.pow(1024, 4), prev: 'MB'},
1718
'TB': {max: Number.MAX_SAFE_INTEGER, prev: 'GB'}

0 commit comments

Comments
 (0)