From 65197f9e0fe3b2cae9ab1a6c25af0b2ecc47df29 Mon Sep 17 00:00:00 2001 From: Guillaume Legrain Date: Wed, 25 May 2016 22:15:30 +0200 Subject: [PATCH] Add option "unitSeparator" closes #35 --- History.md | 5 +++++ Readme.md | 5 +++++ index.js | 5 ++++- test/byte-format.js | 8 ++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index 559e063..818aae3 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +unreleased +========== + + * Add option "unitSeparator" + 2.3.0 / 2016-02-15 ================== diff --git a/Readme.md b/Readme.md index dd19ff2..7465fde 100644 --- a/Readme.md +++ b/Readme.md @@ -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** @@ -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 diff --git a/index.js b/index.js index f71cca7..aa24231 100644 --- a/index.js +++ b/index.js @@ -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} @@ -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 @@ -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'; @@ -112,7 +115,7 @@ function format(value, options) { str = str.replace(formatThousandsRegExp, thousandsSeparator); } - return str + unit; + return str + unitSeparator + unit; } /** diff --git a/test/byte-format.js b/test/byte-format.js index cdfd0d9..0914a01 100644 --- a/test/byte-format.js +++ b/test/byte-format.js @@ -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');