-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*BREAKING CHANGE* feat(common): add a base 1000 bytes pipe named "dec…
…imalBytes". (#1095) * Add a base 1000 bytes pipe named "decimalBytes". Updated suffix of existing bytes pipe to X+"iB" * chore(): Moving export to public-api.ts
- Loading branch information
1 parent
71b9d22
commit 992c296
Showing
7 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/platform/core/common/pipes/decimal-bytes/decimal-bytes.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { TdDecimalBytesPipe } from './decimal-bytes.pipe'; | ||
|
||
describe('TdDecimalBytesPipe', () => { | ||
let pipe: TdDecimalBytesPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new TdDecimalBytesPipe(); | ||
}); | ||
|
||
it('should return with an empty or invalid input', () => { | ||
expect(pipe.transform(NaN, undefined)).toEqual('Invalid Number'); | ||
expect(pipe.transform(undefined, undefined)).toEqual('Invalid Number'); | ||
expect(pipe.transform(0.3, undefined)).toEqual('Invalid Number'); | ||
expect(pipe.transform(0.76, undefined)).toEqual('Invalid Number'); | ||
}); | ||
|
||
it('should return a formatted bytes to larger units', () => { | ||
expect(pipe.transform(0, undefined)).toEqual('0 B'); | ||
expect(pipe.transform(535, undefined)).toEqual('535 B'); | ||
expect(pipe.transform(1000, undefined)).toEqual('1 KB'); | ||
expect(pipe.transform(1024, undefined)).toEqual('1.02 KB'); | ||
expect(pipe.transform(1024, 1)).toEqual('1 KB'); | ||
expect(pipe.transform(138540, undefined)).toEqual('138.54 KB'); | ||
expect(pipe.transform(138540, 1)).toEqual('138.5 KB'); | ||
expect(pipe.transform(1571800, undefined)).toEqual('1.57 MB'); | ||
expect(pipe.transform(1571800, 4)).toEqual('1.5718 MB'); | ||
expect(pipe.transform(10000000, undefined)).toEqual('10 MB'); | ||
expect(pipe.transform(10485760, undefined)).toEqual('10.49 MB'); | ||
expect(pipe.transform(10201000, 3)).toEqual('10.201 MB'); | ||
expect(pipe.transform(3.81861e+10, undefined)).toEqual('38.19 GB'); | ||
expect(pipe.transform(1.890381861e+14, undefined)).toEqual('189.04 TB'); | ||
expect(pipe.transform(5.35765e+16, undefined)).toEqual('53.58 PB'); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
src/platform/core/common/pipes/decimal-bytes/decimal-bytes.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'decimalBytes', | ||
}) | ||
|
||
export class TdDecimalBytesPipe implements PipeTransform { | ||
/* `bytes` needs to be `any` or TypeScript complains | ||
Tried both `number` and `number | string` */ | ||
transform(bytes: any, precision: number = 2): string { | ||
if (bytes === 0) { | ||
return '0 B'; | ||
} else if (isNaN(parseInt(bytes, 10))) { | ||
/* If not a valid number, return 'Invalid Number' */ | ||
return 'Invalid Number'; | ||
} | ||
let k: number = 1000; | ||
let sizes: string[] = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
let i: number = Math.floor(Math.log(bytes) / Math.log(k)); | ||
// if less than 1 | ||
if (i < 0) { | ||
return 'Invalid Number'; | ||
} | ||
return parseFloat((bytes / Math.pow(k, i)).toFixed(precision)) + ' ' + sizes[i]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters