Skip to content

Commit 47d3006

Browse files
committed
remove aveta dependency
1 parent d7f0fd8 commit 47d3006

12 files changed

Lines changed: 69 additions & 74 deletions
-25.7 KB
Binary file not shown.
-12.2 KB
Binary file not shown.
-3.44 KB
Binary file not shown.
-5.54 KB
Binary file not shown.
-9.58 KB
Binary file not shown.
-85.9 KB
Binary file not shown.
-31.8 KB
Binary file not shown.

plugins/google-search-console/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"check-eslint": "run g:check-eslint",
1111
"pack": "run g:pack",
1212
"preview": "run g:preview",
13-
"check-typescript": "run g:check-typescript"
13+
"check-typescript": "run g:check-typescript",
14+
"check-vitest": "run g:check-vitest"
1415
},
1516
"dependencies": {
1617
"@ataverascrespo/react18-ts-textfit": "^1.0.0",
17-
"aveta": "^1.5.2",
1818
"cheerio": "^1.1.2",
1919
"framer-plugin": "3.11.0-alpha.12",
2020
"react": "^18.3.1",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, it } from "vitest"
2+
import { formatStat } from "./formatStat"
3+
4+
describe("formatStat", () => {
5+
it("keeps values below one thousand unchanged", () => {
6+
expect(formatStat(0)).toBe("0")
7+
expect(formatStat(12)).toBe("12")
8+
expect(formatStat(999)).toBe("999")
9+
})
10+
11+
it("abbreviates large values with one decimal place", () => {
12+
expect(formatStat(1000)).toBe("1K")
13+
expect(formatStat(1234)).toBe("1.2K")
14+
expect(formatStat(10_500)).toBe("10.5K")
15+
expect(formatStat(1_200_000)).toBe("1.2M")
16+
})
17+
18+
it("carries rounded values into the next unit", () => {
19+
expect(formatStat(999_950)).toBe("1M")
20+
})
21+
22+
it("preserves negative values", () => {
23+
expect(formatStat(-1234)).toBe("-1.2K")
24+
})
25+
26+
it("rejects non-finite values", () => {
27+
expect(() => formatStat(Number.POSITIVE_INFINITY)).toThrow("Value must be finite")
28+
expect(() => formatStat(Number.NaN)).toThrow("Value must be finite")
29+
})
30+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const statUnits = ["", "K", "M", "B", "T", "P", "E"] as const
2+
3+
export function formatStat(value: number) {
4+
if (!Number.isFinite(value)) {
5+
throw new Error("Value must be finite")
6+
}
7+
8+
const sign = value < 0 ? "-" : ""
9+
let scaledValue = Math.abs(value)
10+
let unitIndex = 0
11+
12+
while (scaledValue >= 1000 && unitIndex < statUnits.length - 1) {
13+
scaledValue /= 1000
14+
unitIndex += 1
15+
}
16+
17+
if (scaledValue >= 1000) {
18+
return value.toString()
19+
}
20+
21+
let roundedValue = Number.isInteger(scaledValue) ? scaledValue : Number(scaledValue.toFixed(1))
22+
23+
while (roundedValue >= 1000 && unitIndex < statUnits.length - 1) {
24+
roundedValue /= 1000
25+
unitIndex += 1
26+
}
27+
28+
const unit = statUnits[unitIndex] ?? ""
29+
30+
return `${sign}${roundedValue.toString()}${unit}`
31+
}

0 commit comments

Comments
 (0)