Skip to content

Commit

Permalink
Adding longform flag, fixing markdown in README
Browse files Browse the repository at this point in the history
Fixing README
  • Loading branch information
avoidwork committed Jan 24, 2017
1 parent 7009d45 commit 7d8b650
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 19 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,37 @@ filesize.js provides a simple way to get a human readable file size string from
`filesize()` accepts an optional descriptor Object as a second argument, so you can customize the output.

### base
_***(number)***_ Number base, default is `2`
_*(number)*_ Number base, default is `2`

### bits
_***(boolean)***_ Enables `bit` sizes, default is `false`
_*(boolean)*_ Enables `bit` sizes, default is `false`

### exponent
_***(number)***_ Specifies the SI suffix via exponent, e.g. `2` is `MB` for bytes, default is `-1`
_*(number)*_ Specifies the SI suffix via exponent, e.g. `2` is `MB` for bytes, default is `-1`

### longform
_*(boolean)*_ Enables long form of unit of measure, default is `false`

### output
_***(string)***_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`
_*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`

### round
_***(number)***_ Decimal place, default is `2`
_*(number)*_ Decimal place, default is `2`

### spacer
_***(string)***_ Character between the `result` and `suffix`, default is `" "`
_*(string)*_ Character between the `result` and `suffix`, default is `" "`

### standard
_***(string)***_ Standard unit of measure, can be `iec` or `jedec`, default is `jedec`; can be overruled by `base`
_*(string)*_ Standard unit of measure, can be `iec` or `jedec`, default is `jedec`; can be overruled by `base`

### symbols
_***(object)***_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
_*(object)*_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found

### suffixes (deprecated: use 'symbols')
_***(object)***_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
_*(object)*_ Dictionary of SI/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`
_*(boolean)*_ Enables unix style human readable output, e.g `ls -lh`, default is `false`

## Examples

Expand All @@ -53,6 +56,7 @@ filesize(1024); // "1 KB"
filesize(1024, {exponent: 0}); // "1024 B"
filesize(1024, {output: "exponent"}); // 1
filesize(265318, {standard: "iec"}); // "259.1 KiB"
filesize(265318, {standard: "iec", longform: true}); // "259.1 kibibytes"
```

## Partial Application
Expand Down
13 changes: 11 additions & 2 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2017 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 3.4.3
* @version 3.5.0
*/
(function (global) {
const b = /^(b|B)$/,
Expand All @@ -16,6 +16,10 @@
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
}
},
longform = {
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
};

/**
Expand All @@ -29,7 +33,7 @@
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, neg, num, output, round, unix, spacer, standard, symbols;
e, base, bits, ceil, long, neg, num, output, round, unix, spacer, standard, symbols;

if (isNaN(arg)) {
throw new Error("Invalid arguments");
Expand All @@ -43,6 +47,7 @@
symbols = descriptor.symbols || descriptor.suffixes || {};
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = descriptor.output || "string";
long = descriptor.longform === true;
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
num = Number(arg);
neg = num < 0;
Expand Down Expand Up @@ -117,6 +122,10 @@
return {value: result[0], suffix: result[1], symbol: result[1]};
}

if (long) {
result[1] = longform[standard][e] + (bits ? "bit" : "byte") + (result[0] > 1 ? "s" : "");
}

return result.join(spacer);
}

Expand Down
12 changes: 11 additions & 1 deletion lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @copyright 2017 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 3.4.3
* @version 3.5.0
*/
(function (global) {
var b = /^(b|B)$/,
Expand All @@ -18,6 +18,10 @@
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
}
},
longform = {
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
};

/**
Expand All @@ -37,6 +41,7 @@
base = void 0,
bits = void 0,
ceil = void 0,
long = void 0,
neg = void 0,
num = void 0,
output = void 0,
Expand All @@ -58,6 +63,7 @@
symbols = descriptor.symbols || descriptor.suffixes || {};
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = descriptor.output || "string";
long = descriptor.longform === true;
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
num = Number(arg);
neg = num < 0;
Expand Down Expand Up @@ -132,6 +138,10 @@
return { value: result[0], suffix: result[1], symbol: result[1] };
}

if (long) {
result[1] = longform[standard][e] + (bits ? "bit" : "byte") + (result[0] > 1 ? "s" : "");
}

return result.join(spacer);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/filesize.min.js

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

0 comments on commit 7d8b650

Please sign in to comment.