Skip to content

Commit

Permalink
dataSize format displayer decimal option
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jun 21, 2019
1 parent e38e121 commit 2e20789
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,4 @@ No changes. Erroneous version bump.
## Next

* Fix: `dataSize` format displayer indicate sizes are binary (GiB not GB)
* `dataSize` format displayer `decimal` option
13 changes: 10 additions & 3 deletions lib/forms/displayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,24 @@ exports = module.exports = {
},

dataSize: function(val, options) {
var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
var sizes, divisor;
if (options.decimal) {
sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
divisor = 1000;
} else {
sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
divisor = 1024;
}

var result,
v = val;
_.forEach(sizes, function(size) {
if (v < 1024) {
if (v < divisor) {
result = v.toPrecision(3) + ' ' + size;
return false;
}

v = v / 1024;
v = v / divisor;
});

if (!result) result = v.toPrecision(3) + ' ' + sizes[sizes.length - 1];
Expand Down

0 comments on commit 2e20789

Please sign in to comment.