Skip to content

Commit

Permalink
feat: adding a useGrouping option for number formatting (#86)
Browse files Browse the repository at this point in the history
Co-authored-by: Dave Lockhart <“dlockhart@users.noreply.github.com”>
  • Loading branch information
dlockhart and Dave Lockhart authored Apr 9, 2021
1 parent fe4dc52 commit 20b4861
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const percent = formatPercent(0.333, [options]); // -> '33.3 %' in en-US
Options:
- **minimumFractionDigits**: Minimum number of fraction digits to use. Possible values range from `0` to `20`; the default is `0`.
- **maximumFractionDigits**: Maximum number of fraction digits to use. Possible values range from `0` to `20`; the default is the larger of `minimumFractionDigits` and `3`.
- **useGrouping**: Whether to use grouping separators, such as thousands separators; the default is `true`.

Formatting as an integer (rounded to 0 decimal places):

Expand Down
9 changes: 7 additions & 2 deletions lib/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import {
validateFormatValue
} from './common.js';

function formatPositiveInteger(value, descriptor) {
function formatPositiveInteger(value, descriptor, useGrouping) {
value = Math.floor(value);

if (!useGrouping) {
return value.toString();
}

const valueStr = '' + value;
let ret = '';

Expand Down Expand Up @@ -47,6 +51,7 @@ function validateFormatOptions(options) {

options = options || {};

options.useGrouping = options.useGrouping !== false;
if (options.style !== 'decimal' && options.style !== 'percent') {
options.style = 'decimal';
}
Expand Down Expand Up @@ -192,7 +197,7 @@ function formatDecimal(value, options) {
}
).format(value);

let ret = formatPositiveInteger(parseInt(strValue), descriptor);
let ret = formatPositiveInteger(parseInt(strValue), descriptor, options.useGrouping);

const decimalIndex = strValue.indexOf('.');
if (decimalIndex > -1) {
Expand Down
5 changes: 5 additions & 0 deletions test/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ describe('number', () => {
});
});

it('should not include group separators when "useGrouping" is false', () => {
const value = formatNumber(1234567.891, { useGrouping: false });
expect(value).to.equal('1234567.891');
});

[
{ symbol: '|@|', expected: '1|@|000|@|000' },
{ symbol: '\'', expected: '1\'000\'000' }
Expand Down

0 comments on commit 20b4861

Please sign in to comment.