forked from desktop/desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ app/node_modules/ | |
.idea/ | ||
.vs/ | ||
.eslintcache | ||
*.iml | ||
*.iml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,27 @@ | ||
/** | ||
* Manipulate bytes for displaying purposes. | ||
* Number sign display mode | ||
*/ | ||
export class Bytes { | ||
public readonly bytes: number | ||
|
||
public constructor(bytes: number) { | ||
this.bytes = bytes | ||
} | ||
|
||
/** | ||
* Display bytes in human readable format like: | ||
* - 23gb | ||
* - 43bytes | ||
* @param {boolean} forceSign Show `+123kb` instead of `123kb` | ||
* @return {string} formatted string | ||
*/ | ||
public format(forceSign: boolean = false) { | ||
if (!Number.isFinite(this.bytes)) { | ||
return 'Unknown' | ||
} | ||
const sizes = ['bytes', 'kb', 'mb', 'gb', 'tb'] | ||
const i = Math.floor(Math.log(Math.abs(this.bytes)) / Math.log(1024)) | ||
const sign = forceSign && this.bytes > 0 ? '+' : '' | ||
const value = Math.round(this.bytes / Math.pow(1024, i)) | ||
return `${sign}${value}${sizes[i]}` | ||
} | ||
|
||
/** | ||
* Creates new Bytes object by adding or subtracting the bytes from | ||
* current instance. | ||
* | ||
* @param {number} bytes Amount of bytes to add/remove | ||
* @return {Bytes} New Bytes object. | ||
*/ | ||
public diff(bytes: number) { | ||
return new Bytes(bytes - this.bytes) | ||
} | ||
const enum Sign { | ||
Normal, | ||
Forced | ||
} | ||
|
||
/** | ||
* Display difference percentage in comparison to provided number | ||
* of bytes. | ||
* @param {number} bytes Number of bytes. | ||
* @return {string} Percentage difference like 45% | ||
*/ | ||
public percentDiff(bytes: number) { | ||
const diff = Math.round(100 * (bytes - this.bytes) / this.bytes) | ||
return `${diff}%` | ||
/** | ||
* Display bytes in human readable format like: | ||
* 23GB | ||
* -43B | ||
* It's also possible to force sign in order to get the | ||
* plus sign in case of positive numbers like: | ||
* +23GB | ||
* -43B | ||
*/ | ||
export const formatBytes = (bytes: number, signType: Sign = Sign.Normal) => { | ||
if (!Number.isFinite(bytes)) { | ||
return 'Unknown' | ||
} | ||
} | ||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] | ||
const sizeIndex = Math.floor(Math.log(Math.abs(bytes)) / Math.log(1024)) | ||
const sign = signType == Sign.Forced && bytes > 0 ? '+' : '' | ||
const value = Math.round(bytes / Math.pow(1024, sizeIndex)) | ||
return `${sign}${value}${sizes[sizeIndex]}` | ||
} |