Skip to content

Commit

Permalink
Add option "unitSeparator"
Browse files Browse the repository at this point in the history
closes #35
  • Loading branch information
glegrain authored and dougwilson committed Jun 1, 2016
1 parent 387bb00 commit 65197f9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Add option "unitSeparator"

2.3.0 / 2016-02-15
==================

Expand Down
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Format the given value in bytes into a string. If the value is negative, it is k
| decimalPlaces | `number`|`null` | Maximum number of decimal places to include in output. Default value to `2`. |
| fixedDecimals | `boolean`|`null` | Whether to always display the maximum number of decimal places. Default value to `false` |
| thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `.`... Default value to `' '`. |
| unitSeparator | `string`|`null` | Separator to use between number and unit. Default value to `''`. |

**Returns**

Expand All @@ -52,6 +53,10 @@ bytes(1000, {thousandsSeparator: ' '});

bytes(1024 * 1.7, {decimalPlaces: 0});
// output: '2kB'

bytes(1024, {unitSeparator: ' '});
// output: '1 kB'

```

#### bytes.parse(string value): number|null
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb)$/i;
* decimalPlaces: [number]
* fixedDecimals: [boolean]
* thousandsSeparator: [string]
* unitSeparator: [string]
* }} [options] bytes options.
*
* @returns {string|number|null}
Expand Down Expand Up @@ -75,6 +76,7 @@ function bytes(value, options) {
* @param {number} [options.decimalPlaces=2]
* @param {number} [options.fixedDecimals=false]
* @param {string} [options.thousandsSeparator=]
* @param {string} [options.unitSeparator=]
*
* @returns {string|null}
* @public
Expand All @@ -87,6 +89,7 @@ function format(value, options) {

var mag = Math.abs(value);
var thousandsSeparator = (options && options.thousandsSeparator) || '';
var unitSeparator = (options && options.unitSeparator) || '';
var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2;
var fixedDecimals = Boolean(options && options.fixedDecimals);
var unit = 'B';
Expand All @@ -112,7 +115,7 @@ function format(value, options) {
str = str.replace(formatThousandsRegExp, thousandsSeparator);
}

return str + unit;
return str + unitSeparator + unit;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/byte-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ describe('Test byte format function', function(){
assert.equal(bytes.format(1000, {thousandsSeparator: ' '}).toLowerCase(), '1 000b');
});

it('Should custom unit separator', function(){
assert.equal(bytes.format(1024), '1kB');
assert.equal(bytes.format(1024, {unitSeparator: ''}), '1kB');
assert.equal(bytes.format(1024, {unitSeparator: null}), '1kB');
assert.equal(bytes.format(1024, {unitSeparator: ' '}), '1 kB');
assert.equal(bytes.format(1024, {unitSeparator: '\t'}), '1\tkB');
});

it('Should support custom number of decimal places', function(){
assert.equal(bytes.format(kb - 1, {decimalPlaces: 0}).toLowerCase(), '1023b');
assert.equal(bytes.format(kb, {decimalPlaces: 0}).toLowerCase(), '1kb');
Expand Down

0 comments on commit 65197f9

Please sign in to comment.