Skip to content

Commit

Permalink
Adding fullforms for overriding full form
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jan 25, 2017
1 parent d957df3 commit 727db07
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ _*(number)*_ Specifies the SI suffix via exponent, e.g. `2` is `MB` for bytes, d
### fullform
_*(boolean)*_ Enables full form of unit of measure, default is `false`

### fullforms
_*(array)*_ Array of full form overrides, default is `[]`

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

Expand Down Expand Up @@ -57,6 +60,7 @@ filesize(1024, {exponent: 0}); // "1024 B"
filesize(1024, {output: "exponent"}); // 1
filesize(265318, {standard: "iec"}); // "259.1 KiB"
filesize(265318, {standard: "iec", fullform: true}); // "259.1 kibibytes"
filesize(0, {fullform: true, fullforms: ["байт"]}); // "0 байт"
```

## Partial Application
Expand Down
7 changes: 4 additions & 3 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.5.3
* @version 3.5.4
*/
(function (global) {
const b = /^(b|B)$/,
Expand Down Expand Up @@ -33,7 +33,7 @@
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, full, neg, num, output, round, unix, spacer, standard, symbols;
e, base, bits, ceil, full, fullforms, neg, num, output, round, unix, spacer, standard, symbols;

if (isNaN(arg)) {
throw new Error("Invalid arguments");
Expand All @@ -48,6 +48,7 @@
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = descriptor.output || "string";
full = descriptor.fullform === true;
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
num = Number(arg);
neg = num < 0;
Expand Down Expand Up @@ -124,7 +125,7 @@
}

if (full) {
result[1] = fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
}

return result.join(spacer);
Expand Down
6 changes: 4 additions & 2 deletions 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.5.3
* @version 3.5.4
*/
(function (global) {
var b = /^(b|B)$/,
Expand Down Expand Up @@ -42,6 +42,7 @@
bits = void 0,
ceil = void 0,
full = void 0,
fullforms = void 0,
neg = void 0,
num = void 0,
output = void 0,
Expand All @@ -64,6 +65,7 @@
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = descriptor.output || "string";
full = descriptor.fullform === true;
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
num = Number(arg);
neg = num < 0;
Expand Down Expand Up @@ -140,7 +142,7 @@
}

if (full) {
result[1] = fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
result[1] = fullforms[e] ? fullforms[e] : fullform[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 727db07

Please sign in to comment.