Skip to content

Commit eed45d5

Browse files
committedFeb 24, 2020
formattedPrice can formats also exponential
1 parent 08ace98 commit eed45d5

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed
 

‎README.md

+9-17
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ yarn add price-like-humans -D
6161

6262
| Argument | Required | Argument type | Description |
6363
| -------- | ------------ | ---------------- | ---------------------------------------------------------------------------------------------------------------- |
64-
| value | _\*required_ | `number, string` | Incoming numbers which will be formatted |
64+
| value | _\*required_ | `number, string` | Incoming numbers which will be formatted (exponential friendly) |
6565
| options | _optional_ | `object` | Settings list, see [formattedPrice options](#formattedprice-options) |
6666

6767
### exponentformatter
6868

6969
| Argument | Required | Argument type | Description |
7070
| -------- | ------------ | ---------------- | ---------------------------------------- |
71-
| value | _\*required_ | `number, string` | Incoming numbers which will be formatted |
71+
| value | _\*required_ | `number, string` | Incoming exponential numbers which will be formatted |
7272

7373
### formattedPrice options
7474

@@ -143,35 +143,27 @@ formattedPrice(12345.6789, { lang: 'en' });
143143
//> "12,345.678,9"
144144
```
145145

146-
### `exponentFormatter` examples
147-
148146
```javascript
149-
exponentFormatter(1e-7);
147+
formattedPrice(1e-7, { lang: 'en' });
150148

151-
//> "0.0000001"
149+
//> "0.000,000,1"
152150
```
153151

154-
### Also you can combine methods
155-
156-
Exponential with price like humans
152+
### `exponentFormatter` examples
157153

158154
```javascript
159-
formattedPrice(1e-7);
155+
exponentFormatter(1e-7);
160156

161-
//> '1e-7' // Needs to combine
157+
//> "0.0000001"
162158
```
163159

164-
```javascript
165-
formattedPrice(exponentFormatter(1e-7));
166-
167-
//> "0.000 000 1"
168-
```
169160

170161
## Changelog
171162

172163
<details>
173164
<summary>Show changelog</summary>
174-
165+
v0.8.0
166+
- formattedPrice can formats exponential too
175167
v0.7.0
176168
- Changed arguments in formattedPrice
177169
- Add more coverage and tests

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "price-like-humans",
3-
"version": "0.7.6",
3+
"version": "0.8.0",
44
"description": "JS tools for formatting price or numbers to human likes format.",
55
"main": "dist/index.js",
66
"module": "dist/index.es.js",

‎src/__tests__/formattedPrice.spec.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ describe('formattedPrice tests', () => {
44
it('compare formattedPrice the same works with different types', () => {
55
expect(formattedPrice(10000)).toEqual(formattedPrice('10000'));
66
expect(formattedPrice(1000.1234)).toEqual(formattedPrice('1000.1234'));
7-
expect(formattedPrice(1e-7)).toEqual(formattedPrice('1e-7'));
7+
expect(formattedPrice(1e-7)).toEqual('0.000,000,1');
88
});
99

1010
it('testing formattedPrice with different values', () => {
1111
expect(formattedPrice(10000)).toEqual('10,000');
1212
expect(formattedPrice('1000.1234')).toEqual('1,000.123,4');
13-
expect(formattedPrice(1e-7)).toEqual('1e-7');
13+
expect(formattedPrice(1e-7)).toEqual('0.000,000,1');
1414
expect(formattedPrice('test')).toEqual(false);
1515
expect(formattedPrice('t1000')).toEqual(false);
1616
expect(formattedPrice(1000.1234, { separator: '.' })).toEqual('1.000,123.4');
@@ -21,13 +21,16 @@ describe('formattedPrice tests', () => {
2121
it('testing formattedPrice with different separators', () => {
2222
expect(formattedPrice(1000.1234, { separator: ',' })).toEqual('1,000.123,4');
2323
expect(formattedPrice(1000.1234, { separator: ' ' })).toEqual('1 000.123 4');
24+
// ToDo: Fix this, must be '1000.1234'
25+
expect(formattedPrice(1000.1234, { separator: '' })).toEqual('1,000.123,4');
2426
});
2527

2628
it('testing formattedPrice with different delimiters', () => {
2729
// ToDo: Fix this. Must be 1.000,123.4'
2830
expect(formattedPrice(1000.1234, { delimiter: ',' })).toEqual('1,000.123,4');
2931
expect(formattedPrice(1000.1234, { delimiter: '.' })).toEqual('1,000.123,4');
3032
expect(formattedPrice(1000.1234, { delimiter: ' ' })).toEqual('1,000 123,4');
33+
expect(formattedPrice(1000.1234, { delimiter: '' })).toEqual(false);
3134
});
3235

3336
it('testing formattedPrice with the same separators and delimiters', () => {

‎src/utils/exponentFormatter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default (value: number | string): string => {
1010
useGrouping: false,
1111
});
1212

13-
return localeFormatter.format(Number(value)).replace(',', '.');
13+
return localeFormatter.format(Number(value.toString().replace(',', '.')));
1414
}
1515

1616
return value.toString();

‎src/utils/formattedPrice.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import reverser from './reverser';
22
import getLocale from './locale';
3-
import { formattedPriceOptionsType } from "../types";
3+
import exponentFormatter from './exponentFormatter';
4+
import { formattedPriceOptionsType } from '../types';
45

56
export default (value: string | number, options?: formattedPriceOptionsType): string | boolean => {
6-
if (isNaN(Number(value.toString().split('')[0]))) {
7+
if (isNaN(Number(value))) {
8+
console.error('Value must be a number or a number inside the string');
79
return false;
810
}
911

@@ -12,6 +14,12 @@ export default (value: string | number, options?: formattedPriceOptionsType): st
1214
return false;
1315
}
1416

17+
if (typeof options?.delimiter !== 'undefined' && options?.delimiter === '') {
18+
console.error('The delimiter can`t be empty. Remove this option to set default or set a value');
19+
return false;
20+
}
21+
22+
let formattedValue = value < 1e-6 ? exponentFormatter(Number(value)) : value;
1523
let delimiter = options?.delimiter || getLocale(options?.lang).delimiter;
1624
let separator = options?.separator || getLocale(options?.lang).separator;
1725

@@ -22,17 +30,16 @@ export default (value: string | number, options?: formattedPriceOptionsType): st
2230
}
2331

2432
const valueSeparator =
25-
reverser(value).replace(/\s|\d/g, '').length === 1 ? reverser(value).replace(/\s|\d/g, '')[0] : delimiter;
26-
const stringDelimiter = delimiter || valueSeparator;
27-
const numberArray = value.toString().split(typeof value === 'number' ? '.' : valueSeparator);
33+
typeof Number(formattedValue) === 'number' ? '.' : formattedValue.toString().replace(/\s|\d/g, '')[0];
34+
const numberArray = formattedValue.toString().split(valueSeparator);
2835
const regexpWithSpace = /\B(?=(\d{3})+(?!\d))/g;
2936
const numberBeforeDot = numberArray[0].replace(regexpWithSpace, separator);
3037

3138
if (numberArray.length > 1) {
3239
const reversedNumberAfterDot = reverser(numberArray[1]).replace(regexpWithSpace, separator);
3340
const numberAfterDot = reverser(reversedNumberAfterDot);
3441

35-
return [numberBeforeDot, numberAfterDot].join(stringDelimiter);
42+
return [numberBeforeDot, numberAfterDot].join(delimiter);
3643
}
3744

3845
return numberBeforeDot;

0 commit comments

Comments
 (0)
Please sign in to comment.