Skip to content

Commit 6731e95

Browse files
feat: priceAdapter allow "-"
1 parent 7dde836 commit 6731e95

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
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.22.1
4-
- **[fix]** : useInView ref type
3+
## v1.22.3
4+
- **[fix]** : priceAdapter allow "-"

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.22.1",
4+
"version": "1.22.3",
55
"private": false,
66
"license": "ISC",
77
"type": "module",

src/utils/adapter/priceAdapter/priceAdatper.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ const defaultOptions = {
1717
* @param value
1818
* @param options @default { local: "fr-FR", currency: "EUR", style: "currency" }
1919
*/
20-
export const priceAdapter = (value?: number | null, options?: Options) => {
20+
export const priceAdapter = (value?: number | "-" | null, options?: Options) => {
2121
const { currency, local, style } = {
2222
...defaultOptions,
2323
...options,
2424
};
2525

26+
if (value === "-") {
27+
const parts = new Intl.NumberFormat(local, { currency, style: "currency" }).formatToParts(0);
28+
const currencySymbol = parts.find((part) => part.type === "currency")?.value || currency;
29+
return `-${currencySymbol}`;
30+
}
31+
2632
if (!value) {
2733
return new Intl.NumberFormat(local, {
2834
currency,
@@ -32,17 +38,15 @@ export const priceAdapter = (value?: number | null, options?: Options) => {
3238
}).format(0);
3339
}
3440

35-
// Check if the value is an integer
3641
const isInteger = Number.isInteger(value);
37-
3842
const formatOptions = {
3943
currency,
4044
maximumFractionDigits: isInteger ? 0 : 2,
4145
minimumFractionDigits: isInteger ? 0 : 2,
4246
style,
4347
};
4448

45-
return new Intl.NumberFormat(local, formatOptions).format(value);
49+
return new Intl.NumberFormat(local, formatOptions).format(Number(value));
4650
};
4751

4852
export default priceAdapter;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ describe("priceAdapter", () => {
3131
const price = priceAdapter(1000.0);
3232
expect(price).toBe("1 000 €");
3333
});
34+
35+
test('with "-" value', () => {
36+
const price = priceAdapter("-");
37+
expect(price).toBe("-€");
38+
});
3439
});

0 commit comments

Comments
 (0)