Skip to content

Commit

Permalink
add separateOneDigit
Browse files Browse the repository at this point in the history
  • Loading branch information
shrpne committed Nov 30, 2020
1 parent 5d952e0 commit c8371b1
Show file tree
Hide file tree
Showing 7 changed files with 2,843 additions and 1,997 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.5.0 - 2020-12-01
- Add `separateOneDigit` option

## 0.4.0 - 2020-03-04
- Add `decimalSeparator` options

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ prettyNum('00123456789.12300e-2', {precision: 3, thousandsSeparator: ' '}); // =

### `thousandsSeparator`
Defines the thousand grouping separator character
```js
prettyNum(12345678.12345, {thousandsSeparator: ' '});
// => '12 345 678.12345'
prettyNum(12345678.12345, {thousandsSeparator: ','});
// => '12,345,678.12345'
```

### `separateOneDigit`
Should number less than 10000 (e.g. 9999) to be separated, `true` by default
```js
prettyNum(1234, {thousandsSeparator: ' ', separateOneDigit: true});
// => '1 234'
prettyNum(1234, {thousandsSeparator: ' ', separateOneDigit: false});
// => '1234'
```

### `precision`
Number of decimal digits to keep when rounding. Pass falsey value to not change precision.
Expand Down
4,785 changes: 2,803 additions & 1,982 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pretty-num",
"version": "0.4.0",
"version": "0.5.0",
"description": "Lightweight module for formatting numbers to a human readable string",
"main": "dist/index.js",
"module": "src/index.js",
Expand Down Expand Up @@ -55,19 +55,19 @@
"thousands": "^1.0.1"
},
"devDependencies": {
"@babel/core": "^7.8.6",
"@babel/preset-env": "^7.8.6",
"babel-jest": "^25.1.0",
"coveralls": "^3.0.9",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.20.1",
"jest": "^25.1.0",
"@babel/core": "^7.12.9",
"@babel/preset-env": "^7.12.7",
"babel-jest": "^26.6.3",
"coveralls": "^3.1.0",
"eslint": "^7.14.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"jest": "^26.6.3",
"pre-commit": "^1.2.2",
"rollup": "^1.32.0",
"rollup": "^2.34.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.2.0"
"rollup-plugin-terser": "^7.0.2"
}
}
7 changes: 4 additions & 3 deletions src/pretty-num.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ import fromExponential from 'from-exponential';
import thousands from 'thousands';
import toPrecision from './to-precision';


/**
* @param {number|string} num
* @param {number} [precision]
* @param {PRECISION_SETTING} [precisionSetting]
* @param {ROUNDING_MODE} [roundingMode]
* @param {string} [thousandsSeparator]
* @param {string} [decimalSeparator]
* @param {boolean} [separateOneDigit = true]
* @return {string}
*/
export default function prettyNum(num, {precision, precisionSetting, roundingMode, thousandsSeparator, decimalSeparator} = {}) {
export default function prettyNum(num, {precision, precisionSetting, roundingMode, thousandsSeparator, decimalSeparator, separateOneDigit = true} = {}) {
// remove exponential notation
num = fromExponential(num);

// reduce precision
num = toPrecision(num, precision, {precisionSetting, roundingMode});

if (thousandsSeparator) {
// skip separation if `!separateOneDigit && num < 10000`
if (thousandsSeparator && (separateOneDigit || num >= 10000)) {
num = thousands(num, thousandsSeparator);
}

Expand Down
1 change: 0 additions & 1 deletion src/to-precision.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export function _reducePrecision(numString, precision, {precisionSetting = PRECI
return (negation ? '-' : '') + numString;
}


/**
*
* @param {string} part
Expand Down
7 changes: 7 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ describe('prettyNum()', () => {
expect(prettyNum('0000012345678.12345000000', {thousandsSeparator: ' '})).toEqual('12 345 678.12345');
});

test('separateOneDigit', () => {
expect(prettyNum(1234.12345, {thousandsSeparator: ' '})).toEqual('1 234.12345');
expect(prettyNum(1234.12345, {thousandsSeparator: ' ', separateOneDigit: true})).toEqual('1 234.12345');
expect(prettyNum(12345.12345, {thousandsSeparator: ' ', separateOneDigit: true})).toEqual('12 345.12345');
expect(prettyNum(1234.12345, {thousandsSeparator: ' ', separateOneDigit: false})).toEqual('1234.12345');
});

test('zero', () => {
expect(prettyNum(0, {precision: 3, precisionSetting: PRECISION_SETTING.FIXED})).toEqual('0.000');
expect(prettyNum('0.0000000000000', {precision: 3, precisionSetting: PRECISION_SETTING.FIXED})).toEqual('0.000');
Expand Down

0 comments on commit c8371b1

Please sign in to comment.