-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format amounts with sup fraction digits
Makes the fractional digits display like exponents. The idea to de-emphasize the fractional part without completely hiding it as it could be looking incorrect. And to make it look like prices are often being displayed in supermarkets and alike.
- Loading branch information
Showing
6 changed files
with
190 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import Amount from "./amount"; | ||
|
||
it("renders 0 correctly", () => { | ||
const { asFragment } = render(<Amount>{0}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
const fraction = asFragment() | ||
.querySelector(".amount-fraction") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("0"); | ||
expect(fraction).toBe("00"); | ||
}); | ||
|
||
it("renders positive two-digit integer correctly", () => { | ||
const { asFragment } = render(<Amount>{15}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
const fraction = asFragment() | ||
.querySelector(".amount-fraction") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("15"); | ||
expect(fraction).toBe("00"); | ||
}); | ||
|
||
it("renders negative integer correctly", () => { | ||
const { asFragment } = render(<Amount>{-8}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
const fraction = asFragment() | ||
.querySelector(".amount-fraction") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("-8"); | ||
expect(fraction).toBe("00"); | ||
}); | ||
|
||
it("renders number with 1 fraction digit correctly", () => { | ||
const { asFragment } = render(<Amount>{28.4}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
const fraction = asFragment() | ||
.querySelector(".amount-fraction") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("28"); | ||
expect(fraction).toBe("40"); | ||
}); | ||
|
||
it("renders number with 2 fraction digits correctly", () => { | ||
const { asFragment } = render(<Amount>{231.87}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
const fraction = asFragment() | ||
.querySelector(".amount-fraction") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("231"); | ||
expect(fraction).toBe("87"); | ||
}); | ||
|
||
it("renders number with 3 fraction digits rounded up correctly", () => { | ||
const { asFragment } = render(<Amount>{0.336}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
const fraction = asFragment() | ||
.querySelector(".amount-fraction") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("0"); | ||
expect(fraction).toBe("34"); | ||
}); | ||
|
||
it("renders number with 3 fraction digits rounded down correctly", () => { | ||
const { asFragment } = render(<Amount>{-72.168}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
const fraction = asFragment() | ||
.querySelector(".amount-fraction") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("-72"); | ||
expect(fraction).toBe("17"); | ||
}); | ||
|
||
it("hides fraction if it's zero and hide zero flag is set", () => { | ||
const { asFragment } = render(<Amount hideZeroFraction>{-28}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("-28"); | ||
expect(asFragment().querySelector(".amount-fraction")).toBeNull(); | ||
}); | ||
|
||
it("shows fraction if hide zero flag is set but it's not zero", () => { | ||
const { asFragment } = render(<Amount hideZeroFraction>{-12.8}</Amount>); | ||
const integer = asFragment() | ||
.querySelector(".amount-integer") | ||
?.innerHTML.trim(); | ||
const fraction = asFragment() | ||
.querySelector(".amount-fraction") | ||
?.innerHTML.trim(); | ||
expect(integer).toBe("-12"); | ||
expect(fraction).toBe("80"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React, { memo, FunctionComponent } from "react"; | ||
|
||
interface Props { | ||
children: number; | ||
hideZeroFraction?: boolean; | ||
} | ||
|
||
const Amount: FunctionComponent<Props> = ({ | ||
children: value, | ||
hideZeroFraction, | ||
}) => { | ||
const integer = Math.trunc(value); | ||
const fraction = Math.round(Math.abs(value % 1) * 100); | ||
const paddedFraction = fraction < 10 ? `0${fraction}` : fraction; | ||
|
||
const showFraction = !hideZeroFraction || paddedFraction !== "00"; | ||
|
||
return ( | ||
<> | ||
<span className="amount-integer">{integer}</span> | ||
{showFraction && ( | ||
<small className="amount-fraction">{paddedFraction}</small> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(Amount); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters