Skip to content

Commit

Permalink
Small percentage fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jan 29, 2024
1 parent f37ed5a commit 047990b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 0.42.2

### Patch Changes

- percentage small fix

## 0.42.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "0.42.1",
"version": "0.42.2",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
43 changes: 34 additions & 9 deletions src/formatters/formatNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,41 @@ describe("formatNumber", () => {
expect(formattedValue).toEqual("123,456.123");
});

test("Percentage no digits", () => {
const formattedValue = formatNumber(0.123456, { percentage: true });
expect(formattedValue).toEqual("12%");
});
describe("Percentage", () => {
test("no digits", () => {
const formattedValue = formatNumber(0.123456, { percentage: true });
expect(formattedValue).toEqual("12%");
});

test("digits", () => {
const formattedValue = formatNumber(0.123456, {
percentage: true,
maxDigits: 2,
});
expect(formattedValue).toEqual("12.35%");
});

test(" digits roundung", () => {
const formattedValue = formatNumber(0.9999, {
percentage: true,
});
expect(formattedValue).toEqual("100%");
});

test("undefined stuff", () => {
expect(formatNumber(0 / 0, { percentage: true })).toBe(`0%`);
expect(formatNumber(0 / 10, { percentage: true })).toBe(`0%`);
expect(formatNumber(10 / 0, { percentage: true })).toBe(`0%`);
});

test("Percentage digits", () => {
const formattedValue = formatNumber(0.123456, {
percentage: true,
maxDigits: 2,
test("defined stuff", () => {
expect(formatNumber(1 / 10, { percentage: true, maxDigits: 2 })).toBe(
`10.00%`
);
expect(formatNumber(2 / 11, { percentage: true, maxDigits: 1 })).toBe(
`18.2%`
);
expect(formatNumber(2 / 12, { percentage: true })).toBe(`17%`);
});
expect(formattedValue).toEqual("12.35%");
});
});
4 changes: 2 additions & 2 deletions src/formatters/formatNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isNumber } from "../validators/isNumber";
* @example formatNumber(1000, { compact: true }) // 1K
* @example formatNumber(1111, { maxDigits: 2 }) // 1,100
* @example formatNumber(111111.123123123) // 111,111.123
* @example formatNumber(0.12345, { percentage: true, maxDigits: 2 }) // '12.34%'
* @example formatNumber(0.12345, { percentage: true, maxDigits: 2 }) // '12.35%' (rounding)
*/
export const formatNumber = (
value: number,
Expand All @@ -21,7 +21,7 @@ export const formatNumber = (
): string => {
if (percentage) {
const useValue = isNumber(value) ? value : 0;
const number = Number((useValue * 100).toFixed(maxDigits || 0));
const number = (useValue * 100).toFixed(maxDigits || 0);
return `${number}%`;
}

Expand Down

0 comments on commit 047990b

Please sign in to comment.