Skip to content

Commit b482fc7

Browse files
committed
v0.7.0 -changed formattedPrice arguments
1 parent 4319c17 commit b482fc7

13 files changed

+213
-200
lines changed

README.md

+72-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Price like Humans
2+
23
[![Build Status](https://travis-ci.org/irodger/price-like-humans.svg?branch=master)](https://travis-ci.org/irodger/price-like-humans)
3-
[![Codacy Badge Code Grade](https://api.codacy.com/project/badge/Grade/d28c98d454e4433e8cd643a824be8848)](https://www.codacy.com/manual/irodger/price-like-humans?utm_source=github.com&utm_medium=referral&utm_content=irodger/price-like-humans&utm_campaign=Badge_Grade)
4+
[![Codacy Badge Code Grade](https://api.codacy.com/project/badge/Grade/d28c98d454e4433e8cd643a824be8848)](https://www.codacy.com/manual/irodger/price-like-humans?utm_source=github.com&utm_medium=referral&utm_content=irodger/price-like-humans&utm_campaign=Badge_Grade)
45
[![Codacy Badge Coverage](https://api.codacy.com/project/badge/Coverage/d28c98d454e4433e8cd643a824be8848)](https://www.codacy.com/manual/irodger/price-like-humans?utm_source=github.com&utm_medium=referral&utm_content=irodger/price-like-humans&utm_campaign=Badge_Coverage)
56
[![Dist size](https://img.shields.io/github/size/irodger/price-like-humans/dist/index.js?label=dist%20size)](https://github.com/irodger/price-like-humans)
67
[![NPM version](https://badge.fury.io/js/price-like-humans.svg)](http://badge.fury.io/js/price-like-humans)
@@ -12,12 +13,14 @@
1213
JS kit for formatting price or numbers to human likes format. Also kit will be useful for crypto-currency with 7+ numbers after a delimiter
1314

1415
Breaking changes from 0.5.0 to 0.6.0 see the changelog (spoiler: excessZero is deprecated)
15-
16+
1617
## Features:
18+
1719
- [Price formatter](#formattedprice)
1820
- [Exponential formatter](#exponentformatter)
1921

2022
## Table of contents
23+
2124
- [Features](#features)
2225
- [Install](#install)
2326
- [Methods](#methods)
@@ -31,97 +34,140 @@ Breaking changes from 0.5.0 to 0.6.0 see the changelog (spoiler: excessZero is d
3134
- [Changelog](#changelog)
3235
- [License](#license)
3336

34-
3537
## Install
38+
3639
### NPM users:
40+
3741
```
3842
npm i --save-dev price-like-humans
3943
```
4044

4145
### Yarn users:
46+
4247
```
4348
yarn add price-like-humans -D
4449
```
4550

4651
## Methods
47-
| Methods | Returns | Description |
48-
| --- | --- | --- |
49-
| `formattedPrice(value)` | `string` | Formatting incoming numbers to humans like price with user locale delimiter |
50-
| `exponentFormatter(value)` | `string` | Formatting exponential numbers to human likes numbers. Exponent free |
52+
53+
| Methods | Returns | Description |
54+
| -------------------------------- | -------- | --------------------------------------------------------------------------- |
55+
| `formattedPrice(value, options)` | `string` | Formatting incoming numbers to humans like price with user locale delimiter |
56+
| `exponentFormatter(value)` | `string` | Formatting exponential numbers to human likes numbers. Exponent free |
5157

5258
## formattedPrice options
53-
| Argument | Argument type | Description |
54-
| --- | --- | --- |
55-
| value * | `number, string, object` | Incoming numbers which will be formatted |
56-
| delimiter | `string` | Delimiter symbol. Number which split decimal. Can be replaced |
57-
| separator | `string` | Symbol which separates grouped number. Can be replaced |
58-
| lang | `string` | You can set locale option. Using user locale by default |
59+
60+
| Argument | Required | Argument type | Description |
61+
| -------- | -------------- | ---------------- | ------------------------------------------------------------- |
62+
| value | **\*required** | `number, string` | Incoming numbers which will be formatted |
63+
| options | **optional** | `string` | Delimiter symbol. Number which split decimal. Can be replaced |
64+
65+
### formattedPrice options
66+
67+
| Argument | Argument type | Description |
68+
| --------- | ------------- | ------------------------------------------------------------- |
69+
| delimiter | `string` | Delimiter symbol. Number which split decimal. Can be replaced |
70+
| separator | `string` | Symbol which separates grouped number. Can be replaced |
71+
| lang | `string` | You can set locale option. Using user locale by default |
5972

6073
⚠️ Warning: When works in Nodejs environment, intl.NumberFormat contains 'en-US' locale only, so use the `separator` with `delimiter` when the code needs to run on a server-side.
6174

6275
## Usage
76+
6377
### NodeJS
78+
6479
```javascript
6580
const priceLikeHumans = require('price-like-humans');
6681
```
6782

6883
### ES6
84+
6985
```javascript
7086
import priceLikeHumans from 'price-like-humans';
7187
// or methods only
7288
import { formattedPrice, exponentFormatter } from 'price-like-humans';
7389
```
7490

7591
## Examples
92+
7693
### `formattedPrice`
94+
7795
Without separator arguments (putted your local separator)
96+
7897
```javascript
79-
formattedPrice(12345.6789)
98+
formattedPrice(12345.6789);
8099

81-
//> "12,345.678,9" // EU Locale
82-
//> "12 345.678 9" // RU Locale
100+
//> "12,345.678,9" // EU Locale
101+
//> "12 345.678 9" // RU Locale
83102
```
84103

85104
#### Using with options
105+
86106
```javascript
87-
formattedPrice( {value: 12345.6789, delimiter:','} )
107+
formattedPrice(12345.6789, { delimiter: ',' });
88108

89-
//> "12.345,678.9" // EN Locale
90-
//> "12 345,678 9" // RU Locale
109+
//> "12.345,678.9" // EN Locale
110+
//> "12 345,678 9" // RU Locale
111+
```
91112

92-
formattedPrice( {value: 12345.6789, lang: 'ru'} )
113+
```javascript
114+
formattedPrice(12345.6789, { separator: '.' });
115+
116+
//> "12.345,678.9" // EN Locale
117+
//> "12.345,678.9" // RU Locale
118+
```
119+
120+
```javascript
121+
formattedPrice(12345.6789, { delimiter: '.', separator: ',' });
122+
123+
//> "12,345.678,9"
124+
```
125+
126+
```javascript
127+
formattedPrice(12345.6789, { lang: 'ru' });
93128

94129
//> "12 345,678 9"
130+
```
95131

96-
formattedPrice( {value: 12345.6789, lang: 'en'} )
132+
```javascript
133+
formattedPrice(12345.6789, { lang: 'en' });
97134

98135
//> "12,345.678,9"
99136
```
100137

101138
### `exponentFormatter`
139+
102140
```javascript
103-
exponentFormatter(1e-7)
141+
exponentFormatter(1e-7);
104142

105143
//> "0.0000001"
106144
```
107145

108146
### Also you can combine methods
147+
109148
Exponential with price like humans
149+
110150
```javascript
111-
formattedPrice(1e-7)
151+
formattedPrice(1e-7);
112152

113153
//> '1e-7' // Needs to combine
154+
```
114155

115-
116-
formattedPrice( exponentFormatter(1e-7) )
156+
```javascript
157+
formattedPrice(exponentFormatter(1e-7));
117158

118159
//> "0.000 000 1"
119160
```
120161

121162
## Changelog
163+
122164
<details>
123165
<summary>Show changelog</summary>
124166

167+
v0.7.0
168+
- Changed arguments in formattedPrice
169+
- Add more coverage and tests
170+
- Remove debian lang detector
125171
v0.6.7
126172
- Added prettier
127173
- Added dev unit tests
@@ -146,6 +192,6 @@ formattedPrice( exponentFormatter(1e-7) )
146192
- Refactored formattedPrice: Add default values
147193
</details>
148194

149-
150195
## License
196+
151197
Price Like Humans is [MIT licensed](https://github.com/irodger/price-like-humans/LICENSE).

__tests__/tests.js

+51-58
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,56 @@
11
const plh = require('./../dist/index.js');
22

3-
const cases = {
4-
string: '10000',
5-
stringReversed: '00001',
6-
stringOnly: '10000',
7-
int: 12345,
8-
intReversed: 54321,
9-
arr: [],
10-
number: 10000,
11-
float: 1234.5678,
12-
floatDotString: '1234.5678',
13-
floatWithCustomDelimiter: '1 234,567 8',
14-
floatString: '1234,5678',
15-
floatFormatted: '1 234.567 8',
16-
floatEnFormatted: '1,234.567,8',
17-
floatStringFormatted: '1 234,567 8',
18-
floatStringRuFormatted: '1 234,567 8',
19-
exponential: 1e-7,
20-
exponentialJustString: '1e-7',
21-
stringFormatted: '10 000',
22-
stringRuFormatted: '10 000',
23-
stringEnFormatted: '10,000',
24-
exponentialString: '0.0000001',
25-
exponentialEnFormatted: '0.000,000,1',
26-
exponentialFormatted: '0.000 000 1',
27-
trashString: 'test'
28-
};
29-
30-
describe('dist-test', () => {
31-
it('test formattedPrice with one argument', () => {
3+
describe('plh.formattedPrice tests', () => {
4+
it('compare plh.formattedPrice the same works with different types', () => {
5+
expect(plh.formattedPrice(10000)).toEqual(plh.formattedPrice('10000'));
6+
expect(plh.formattedPrice(1000.1234)).toEqual(plh.formattedPrice('1000.1234'));
7+
expect(plh.formattedPrice(1e-7)).toEqual(plh.formattedPrice('1e-7'));
8+
});
9+
10+
it('testing plh.formattedPrice with different values', () => {
3211
expect(plh.formattedPrice(10000)).toEqual('10,000');
33-
expect(plh.formattedPrice(cases.exponential)).toEqual(cases.exponentialJustString);
34-
expect(plh.formattedPrice(cases.trashString)).toEqual(cases.trashString);
35-
});
36-
37-
it('test formattedPrice with several arguments', () => {
38-
expect(plh.formattedPrice({ value: 1000.1234, delimiter:',',separator:'.' })).toEqual('1.000,123.4');
39-
expect(plh.formattedPrice({ value:'1000.1234', delimiter:',',separator:'.' })).toEqual('1.000,123.4');
40-
expect(plh.formattedPrice({ value:'1000.1234', delimiter:'.',separator:'.' })).toEqual(false);
41-
expect(plh.formattedPrice({ value:'1000.1234', delimiter:',',separator:',' })).toEqual(false);
42-
expect(plh.formattedPrice({ value:'1000.1234', delimiter:' ',separator:' ' })).toEqual(false);
43-
expect(plh.formattedPrice({ value:'1000.1234', delimiter:'.' })).toEqual('1,000.123,4');
44-
expect(plh.formattedPrice({ value: 1000.1234, lang: 'en' })).toEqual('1,000.123,4');
45-
expect(plh.formattedPrice({ value:'1000.1234', separator:'.' })).toEqual('1.000,123.4');
46-
expect(plh.formattedPrice({ value:'1000.1234', separator:' ' })).toEqual('1 000.123 4');
47-
expect(plh.formattedPrice({ value:'1000.1234', delimiter:',', separator:' ' })).toEqual('1 000,123 4');
48-
expect(plh.formattedPrice({ value: cases.floatString, delimiter:',', separator: ' ' })).toEqual(cases.floatWithCustomDelimiter);
49-
});
50-
51-
it('test combine formattedPrice with exponentFormatter', () => {
52-
expect(plh.formattedPrice(plh.exponentFormatter(cases.exponential))).toEqual(cases.exponentialEnFormatted);
53-
});
54-
55-
it('exponentFormatter', () => {
56-
expect(plh.exponentFormatter(cases.number)).toEqual(cases.string);
57-
expect(plh.exponentFormatter(cases.string)).toEqual(cases.string);
58-
expect(plh.exponentFormatter(cases.float)).toEqual(cases.floatDotString);
59-
expect(plh.exponentFormatter(cases.exponential)).toEqual(cases.exponentialString);
60-
expect(plh.exponentFormatter(cases.trashString)).toEqual(cases.trashString);
61-
expect(plh.exponentFormatter(cases.floatString)).toEqual(cases.floatString);
12+
expect(plh.formattedPrice('1000.1234')).toEqual('1,000.123,4');
13+
expect(plh.formattedPrice(1e-7)).toEqual('1e-7');
14+
expect(plh.formattedPrice('test')).toEqual(false);
15+
expect(plh.formattedPrice('t1000')).toEqual(false);
16+
expect(plh.formattedPrice(1000.1234, { separator: '.' })).toEqual('1.000,123.4');
17+
expect(plh.formattedPrice('1000.1234', { separator: '.' })).toEqual('1.000,123.4');
18+
expect(plh.formattedPrice('0.0000001', { separator: '.' })).toEqual('0,000.000.1');
19+
});
20+
21+
it('testing plh.formattedPrice with different separators', () => {
22+
expect(plh.formattedPrice(1000.1234, { separator: ',' })).toEqual('1,000.123,4');
23+
expect(plh.formattedPrice(1000.1234, { separator: ' ' })).toEqual('1 000.123 4');
24+
});
25+
26+
it('testing plh.formattedPrice with different delimiters', () => {
27+
// ToDo: Fix this. Must be 1.000,123.4'
28+
expect(plh.formattedPrice(1000.1234, { delimiter: ',' })).toEqual('1,000.123,4');
29+
expect(plh.formattedPrice(1000.1234, { delimiter: '.' })).toEqual('1,000.123,4');
30+
expect(plh.formattedPrice(1000.1234, { delimiter: ' ' })).toEqual('1,000 123,4');
31+
});
32+
33+
it('testing plh.formattedPrice with the same separators and delimiters', () => {
34+
expect(plh.formattedPrice(1000.1234, { separator: '.', delimiter: '.' })).toEqual(false);
35+
expect(plh.formattedPrice(1000.1234, { separator: ',', delimiter: ',' })).toEqual(false);
36+
expect(plh.formattedPrice(1000.1234, { separator: ' ', delimiter: ' ' })).toEqual(false);
37+
});
38+
39+
it('test plh.formattedPrice with several options', () => {
40+
expect(plh.formattedPrice(1000.1234, { separator: '.', delimiter: ',' })).toEqual('1.000,123.4');
41+
expect(plh.formattedPrice(1000.1234, { separator: ',', delimiter: '.' })).toEqual('1,000.123,4');
42+
expect(plh.formattedPrice(1000.1234, { separator: ' ', delimiter: ',' })).toEqual('1 000,123 4');
43+
expect(plh.formattedPrice(1000.1234, { separator: ' ', delimiter: '.' })).toEqual('1 000.123 4');
44+
expect(plh.formattedPrice(1000.1234, { separator: ',', delimiter: ' ' })).toEqual('1,000 123,4');
45+
});
46+
47+
it('test plh.formattedPrice with lang', () => {
48+
expect(plh.formattedPrice(1000.1234, { lang: 'en' })).toEqual('1,000.123,4');
49+
});
50+
51+
it('plh.exponentFormatter', () => {
52+
expect(plh.exponentFormatter(1e-8)).toBe('0.00000001');
53+
expect(plh.exponentFormatter(1e-20)).toBe('0.00000000000000000001');
54+
expect(plh.exponentFormatter(1000)).toBe('1000');
6255
});
6356
});

dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "price-like-humans",
3-
"version": "0.6.9",
3+
"version": "0.7.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",
@@ -12,6 +12,7 @@
1212
"test": "npm run test:build",
1313
"test-with-coverage": "jest --coverage && cat ./coverage/lcov.info | codacy-coverage -t 67007f5f6f8945ef95839115f2e2fb58 -u irodger -n price-like-humans",
1414
"test:src": "jest -o",
15+
"test:src:full": "jest",
1516
"test:coverage": "jest --coverage",
1617
"test:watch": "jest --watch",
1718
"test:build": "npm run build && jest -o --config=jest-build.config.js"

0 commit comments

Comments
 (0)