Skip to content

Commit

Permalink
Format percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jan 29, 2024
1 parent 880be46 commit f37ed5a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 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.1

### Patch Changes

- format percentage

## 0.42.0

### Minor Changes
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Contributions always welcome!
- `capitalize()` word => Word
- `cleanSpaces()` trims and turns double spaces into single space
- `clamp()` clamp number in a range
- `enumKeys()` enum FRUIT { APPLE, PEAR } => ["APPLE", "PEAR"]
- `enumValues()` enum FRUIT { APPLE = 1, PEAR = 3 } => [1, 3]
- `first()` get the first element of an array
- `firstKey()`
- `firstValue()`
Expand Down Expand Up @@ -96,7 +98,7 @@ Contributions always welcome!

### Formatters

- `formatNumber()` 1000 => "1,000" or "1K"
- `formatNumber()` 1000 => "1,000" or "1K" or 0.112 => "11.2%"
- `stringToUnicode()` "hello" => "\u0068\u0065\u006c\u006c\u006f"
- `stringToCSSUnicode()` "hello" => "\000068\000065\00006c\00006c\00006f" use this for CSS

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.0",
"version": "0.42.1",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
13 changes: 13 additions & 0 deletions src/formatters/formatNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ describe("formatNumber", () => {
const formattedValue = formatNumber(123456.123456);
expect(formattedValue).toEqual("123,456.123");
});

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

test("Percentage digits", () => {
const formattedValue = formatNumber(0.123456, {
percentage: true,
maxDigits: 2,
});
expect(formattedValue).toEqual("12.35%");
});
});
12 changes: 12 additions & 0 deletions src/formatters/formatNumber.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
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%'
*/
export const formatNumber = (
value: number,
{
compact,
maxDigits,
percentage,
}: {
compact?: boolean;
maxDigits?: number;
percentage?: boolean;
} = {}
): string => {
if (percentage) {
const useValue = isNumber(value) ? value : 0;
const number = Number((useValue * 100).toFixed(maxDigits || 0));
return `${number}%`;
}

const formatter = Intl.NumberFormat("en", {
notation: compact ? "compact" : "standard",
maximumSignificantDigits: maxDigits,
});

return formatter.format(value);
};
2 changes: 2 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export * from "./capitalize";
export * from "./clamp";
export * from "./cleanSpaces";
export * from "./dir";
export * from "./enumKeys";
export * from "./enumValues";
export * from "./first";
export * from "./firstKey";
export * from "./firstValue";
Expand Down

0 comments on commit f37ed5a

Please sign in to comment.