Skip to content

Commit 92f66ff

Browse files
Merge pull request #2 from Tracktor/feat/add-option-price-adapter
feat(price): add hide decimal option in priceAdapter.ts
2 parents 7f07488 + 437d443 commit 92f66ff

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# [Versions](https://github.com/Tracktor/react-utils/releases)
22

3-
## v1.19.2
4-
- **[fix]** : `isArray` type
3+
## v1.20.0
4+
- **[feat]** : dynamic display of decimal from priceAdapter

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tracktor/react-utils",
33
"description": "React data table and react data grid",
4-
"version": "1.19.2",
4+
"version": "1.20.0",
55
"private": false,
66
"license": "ISC",
77
"type": "module",
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
interface Options {
22
local?: string;
33
currency?: string;
4-
maximumSignificantDigits?: number;
54
style?: string;
65
}
76

87
const defaultOptions = {
98
currency: "EUR",
109
local: "fr-FR",
11-
maximumSignificantDigits: 1,
1210
style: "currency",
1311
};
1412

1513
/**
1614
* Adapt price to local display format
17-
* exemple: priceAdapter(1000) -> 1 000,00 €
15+
* exemple: priceAdapter(1000) -> 1 000 €
16+
* exemple: priceAdapter(1000.5) -> 1 000,50 €
1817
* @param value
19-
* @param options @default { local: "fr-FR", currency: "EUR", maximumSignificantDigits: 1, style: "currency" }
18+
* @param options @default { local: "fr-FR", currency: "EUR", style: "currency" }
2019
*/
2120
export const priceAdapter = (value?: number | null, options?: Options) => {
22-
const { currency, local, maximumSignificantDigits, style } = { ...defaultOptions, ...options };
21+
const { currency, local, style } = {
22+
...defaultOptions,
23+
...options,
24+
};
2325

2426
if (!value) {
2527
return new Intl.NumberFormat(local, {
2628
currency,
27-
maximumSignificantDigits,
29+
maximumFractionDigits: 0,
30+
minimumFractionDigits: 0,
2831
style,
2932
}).format(0);
3033
}
3134

32-
return new Intl.NumberFormat(local, {
35+
// Check if the value is an integer
36+
const isInteger = Number.isInteger(value);
37+
38+
const formatOptions = {
3339
currency,
40+
maximumFractionDigits: isInteger ? 0 : 2,
41+
minimumFractionDigits: isInteger ? 0 : 2,
3442
style,
35-
}).format(value);
43+
};
44+
45+
return new Intl.NumberFormat(local, formatOptions).format(value);
3646
};
3747

3848
export default priceAdapter;

src/utils/adapter/priceAdapter/test/priceAdapter.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@ import priceAdapter from "@/utils/adapter/priceAdapter";
44
describe("priceAdapter", () => {
55
test("default local price", () => {
66
const price = priceAdapter(1000);
7-
expect(price).toBe("1 000,00 €");
7+
expect(price).toBe("1 000 €");
88
});
99

1010
test("negative price", () => {
1111
const price = priceAdapter(-1);
12-
expect(price).toBe("-1,00 €");
12+
expect(price).toBe("-1 €");
1313
});
1414

1515
test("other local price", () => {
1616
const price = priceAdapter(500, { local: "us-US" });
17-
expect(price).toBe("€500.00");
17+
expect(price).toBe("€500");
1818
});
1919

2020
test("with no value", () => {
2121
const price = priceAdapter();
2222
expect(price).toBe("0 €");
2323
});
24+
25+
test("with decimal value", () => {
26+
const price = priceAdapter(1000.5);
27+
expect(price).toBe("1 000,50 €");
28+
});
29+
30+
test("with zero decimal value", () => {
31+
const price = priceAdapter(1000.0);
32+
expect(price).toBe("1 000 €");
33+
});
2434
});

0 commit comments

Comments
 (0)