Skip to content

Commit

Permalink
Merge pull request #149 from avoidwork/fixit
Browse files Browse the repository at this point in the history
Fixing output
  • Loading branch information
avoidwork committed May 26, 2022
2 parents 8b5fb95 + ab6474b commit 949bada
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 186 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ _*(string)*_ Standard unit of measure, can be `iec` or `jedec`, default is `iec`
### symbols
_*(object)*_ Dictionary of IEC/JEDEC symbols to replace for localization, defaults to english if no match is found

### unix
_*(boolean)*_ Enables unix style human readable output, e.g `ls -lh`, default is `false`

## Examples

```javascript
Expand Down
25 changes: 7 additions & 18 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory());
})(this, (function () { 'use strict';

const b = /^(b|B)$/,
symbol = {
const symbol = {
iec: {
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
Expand Down Expand Up @@ -42,23 +41,22 @@
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, separator, spacer, standard, symbols, roundingFunc, precision;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
}

bits = descriptor.bits === true;
unix = descriptor.unix === true;
pad = descriptor.pad === true;
base = descriptor.base || 10;
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
round = descriptor.round !== void 0 ? descriptor.round : 2;
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
localeOptions = descriptor.localeOptions || {};
separator = descriptor.separator !== void 0 ? descriptor.separator : "";
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " ";
symbols = descriptor.symbols || {};
standard = base === 2 ? descriptor.standard || "iec" : "jedec";
standard = descriptor.standard in symbol ? descriptor.standard : "iec";
output = descriptor.output || "string";
full = descriptor.fullform === true;
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
Expand Down Expand Up @@ -99,7 +97,7 @@
// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
u = result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e];
} else {
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));

Expand All @@ -120,16 +118,7 @@
e++;
}

u = result[1] = base === 10 && e === 1 ? bits ? "kbit" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];

if (unix) {
result[1] = result[1].charAt(0);

if (b.test(result[1])) {
result[0] = Math.floor(result[0]);
result[1] = "";
}
}
u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e];
}

// Decorating a 'diff'
Expand Down
2 changes: 1 addition & 1 deletion lib/filesize.es6.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/filesize.es6.min.js.map

Large diffs are not rendered by default.

25 changes: 7 additions & 18 deletions lib/filesize.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* @license BSD-3-Clause
* @version 8.0.7
*/
const b = /^(b|B)$/,
symbol = {
const symbol = {
iec: {
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
Expand Down Expand Up @@ -36,23 +35,22 @@ const b = /^(b|B)$/,
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, separator, spacer, standard, symbols, roundingFunc, precision;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
}

bits = descriptor.bits === true;
unix = descriptor.unix === true;
pad = descriptor.pad === true;
base = descriptor.base || 10;
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
round = descriptor.round !== void 0 ? descriptor.round : 2;
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
localeOptions = descriptor.localeOptions || {};
separator = descriptor.separator !== void 0 ? descriptor.separator : "";
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " ";
symbols = descriptor.symbols || {};
standard = base === 2 ? descriptor.standard || "iec" : "jedec";
standard = descriptor.standard in symbol ? descriptor.standard : "iec";
output = descriptor.output || "string";
full = descriptor.fullform === true;
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
Expand Down Expand Up @@ -93,7 +91,7 @@ function filesize (arg, descriptor = {}) {
// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
u = result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e];
} else {
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));

Expand All @@ -114,16 +112,7 @@ function filesize (arg, descriptor = {}) {
e++;
}

u = result[1] = base === 10 && e === 1 ? bits ? "kbit" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];

if (unix) {
result[1] = result[1].charAt(0);

if (b.test(result[1])) {
result[0] = Math.floor(result[0]);
result[1] = "";
}
}
u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e];
}

// Decorating a 'diff'
Expand Down
2 changes: 1 addition & 1 deletion lib/filesize.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 949bada

Please sign in to comment.