Skip to content

Commit 70da89b

Browse files
fix(formatUnit): allow to change base on formatUnit (#1405)
* fix(formatUnit): allow to change base on formatUnit * fix: adds comments for default value
1 parent a1c8773 commit 70da89b

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

.changeset/young-shirts-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@scaleway/use-i18n': minor
3+
---
4+
5+
Allow to change base on formatUnit calculations

packages/use-i18n/src/__tests__/__snapshots__/formatUnit.ts.snap

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,120 @@ exports[`formatUnit should work with { unit: 'zettabyte' } 1`] = `"12,56 Zo"`;
116116

117117
exports[`formatUnit should work with { unit: 'zettabyte-humanized' } 1`] = `"0 Zo"`;
118118

119+
exports[`formatUnit should work with base { base: 2, unit: 'bit' } 1`] = `"4,294,967,296 b"`;
120+
121+
exports[`formatUnit should work with base { base: 2, unit: 'bit-humanized' } 1`] = `"4,294,967,296 b"`;
122+
123+
exports[`formatUnit should work with base { base: 2, unit: 'bit-per-second' } 1`] = `"4,294,967,296 bps"`;
124+
125+
exports[`formatUnit should work with base { base: 2, unit: 'bit-per-second-humanized' } 1`] = `"4,294,967,296 bps"`;
126+
127+
exports[`formatUnit should work with base { base: 2, unit: 'bits-humanized' } 1`] = `"4 Gb"`;
128+
129+
exports[`formatUnit should work with base { base: 2, unit: 'bits-per-second-humanized' } 1`] = `"4 Gbps"`;
130+
131+
exports[`formatUnit should work with base { base: 2, unit: 'byte' } 1`] = `"4,294,967,296 B"`;
132+
133+
exports[`formatUnit should work with base { base: 2, unit: 'byte-humanized' } 1`] = `"4,294,967,296 B"`;
134+
135+
exports[`formatUnit should work with base { base: 2, unit: 'bytes-humanized' } 1`] = `"4 GB"`;
136+
137+
exports[`formatUnit should work with base { base: 2, unit: 'exabit' } 1`] = `"4,294,967,296 Eb"`;
138+
139+
exports[`formatUnit should work with base { base: 2, unit: 'exabit-humanized' } 1`] = `"0 Eb"`;
140+
141+
exports[`formatUnit should work with base { base: 2, unit: 'exabit-per-second' } 1`] = `"4,294,967,296 Ebps"`;
142+
143+
exports[`formatUnit should work with base { base: 2, unit: 'exabit-per-second-humanized' } 1`] = `"0 Ebps"`;
144+
145+
exports[`formatUnit should work with base { base: 2, unit: 'exabyte' } 1`] = `"4,294,967,296 EB"`;
146+
147+
exports[`formatUnit should work with base { base: 2, unit: 'exabyte-humanized' } 1`] = `"0 EB"`;
148+
149+
exports[`formatUnit should work with base { base: 2, unit: 'gigabit' } 1`] = `"4,294,967,296 Gb"`;
150+
151+
exports[`formatUnit should work with base { base: 2, unit: 'gigabit-humanized' } 1`] = `"4 Gb"`;
152+
153+
exports[`formatUnit should work with base { base: 2, unit: 'gigabit-per-second' } 1`] = `"4,294,967,296 Gbps"`;
154+
155+
exports[`formatUnit should work with base { base: 2, unit: 'gigabit-per-second-humanized' } 1`] = `"4 Gbps"`;
156+
157+
exports[`formatUnit should work with base { base: 2, unit: 'gigabyte' } 1`] = `"4,294,967,296 GB"`;
158+
159+
exports[`formatUnit should work with base { base: 2, unit: 'gigabyte-humanized' } 1`] = `"4 GB"`;
160+
161+
exports[`formatUnit should work with base { base: 2, unit: 'kilobit' } 1`] = `"4,294,967,296 Kb"`;
162+
163+
exports[`formatUnit should work with base { base: 2, unit: 'kilobit-humanized' } 1`] = `"4,194,304 Kb"`;
164+
165+
exports[`formatUnit should work with base { base: 2, unit: 'kilobit-per-second' } 1`] = `"4,294,967,296 Kbps"`;
166+
167+
exports[`formatUnit should work with base { base: 2, unit: 'kilobit-per-second-humanized' } 1`] = `"4,194,304 Kbps"`;
168+
169+
exports[`formatUnit should work with base { base: 2, unit: 'kilobyte' } 1`] = `"4,294,967,296 KB"`;
170+
171+
exports[`formatUnit should work with base { base: 2, unit: 'kilobyte-humanized' } 1`] = `"4,194,304 KB"`;
172+
173+
exports[`formatUnit should work with base { base: 2, unit: 'megabit' } 1`] = `"4,294,967,296 Mb"`;
174+
175+
exports[`formatUnit should work with base { base: 2, unit: 'megabit-humanized' } 1`] = `"4,096 Mb"`;
176+
177+
exports[`formatUnit should work with base { base: 2, unit: 'megabit-per-second' } 1`] = `"4,294,967,296 Mbps"`;
178+
179+
exports[`formatUnit should work with base { base: 2, unit: 'megabit-per-second-humanized' } 1`] = `"4,096 Mbps"`;
180+
181+
exports[`formatUnit should work with base { base: 2, unit: 'megabyte' } 1`] = `"4,294,967,296 MB"`;
182+
183+
exports[`formatUnit should work with base { base: 2, unit: 'megabyte-humanized' } 1`] = `"4,096 MB"`;
184+
185+
exports[`formatUnit should work with base { base: 2, unit: 'petabit' } 1`] = `"4,294,967,296 Pb"`;
186+
187+
exports[`formatUnit should work with base { base: 2, unit: 'petabit-humanized' } 1`] = `"0 Pb"`;
188+
189+
exports[`formatUnit should work with base { base: 2, unit: 'petabit-per-second' } 1`] = `"4,294,967,296 Pbps"`;
190+
191+
exports[`formatUnit should work with base { base: 2, unit: 'petabit-per-second-humanized' } 1`] = `"0 Pbps"`;
192+
193+
exports[`formatUnit should work with base { base: 2, unit: 'petabyte' } 1`] = `"4,294,967,296 PB"`;
194+
195+
exports[`formatUnit should work with base { base: 2, unit: 'petabyte-humanized' } 1`] = `"0 PB"`;
196+
197+
exports[`formatUnit should work with base { base: 2, unit: 'terabit' } 1`] = `"4,294,967,296 Tb"`;
198+
199+
exports[`formatUnit should work with base { base: 2, unit: 'terabit-humanized' } 1`] = `"0 Tb"`;
200+
201+
exports[`formatUnit should work with base { base: 2, unit: 'terabit-per-second' } 1`] = `"4,294,967,296 Tbps"`;
202+
203+
exports[`formatUnit should work with base { base: 2, unit: 'terabit-per-second-humanized' } 1`] = `"0 Tbps"`;
204+
205+
exports[`formatUnit should work with base { base: 2, unit: 'terabyte' } 1`] = `"4,294,967,296 TB"`;
206+
207+
exports[`formatUnit should work with base { base: 2, unit: 'terabyte-humanized' } 1`] = `"0 TB"`;
208+
209+
exports[`formatUnit should work with base { base: 2, unit: 'yottabit' } 1`] = `"4,294,967,296 Yb"`;
210+
211+
exports[`formatUnit should work with base { base: 2, unit: 'yottabit-humanized' } 1`] = `"0 Yb"`;
212+
213+
exports[`formatUnit should work with base { base: 2, unit: 'yottabit-per-second' } 1`] = `"4,294,967,296 Ybps"`;
214+
215+
exports[`formatUnit should work with base { base: 2, unit: 'yottabit-per-second-humanized' } 1`] = `"0 Ybps"`;
216+
217+
exports[`formatUnit should work with base { base: 2, unit: 'yottabyte' } 1`] = `"4,294,967,296 YB"`;
218+
219+
exports[`formatUnit should work with base { base: 2, unit: 'yottabyte-humanized' } 1`] = `"0 YB"`;
220+
221+
exports[`formatUnit should work with base { base: 2, unit: 'zettabit' } 1`] = `"4,294,967,296 Zb"`;
222+
223+
exports[`formatUnit should work with base { base: 2, unit: 'zettabit-humanized' } 1`] = `"0 Zb"`;
224+
225+
exports[`formatUnit should work with base { base: 2, unit: 'zettabit-per-second' } 1`] = `"4,294,967,296 Zbps"`;
226+
227+
exports[`formatUnit should work with base { base: 2, unit: 'zettabit-per-second-humanized' } 1`] = `"0 Zbps"`;
228+
229+
exports[`formatUnit should work with base { base: 2, unit: 'zettabyte' } 1`] = `"4,294,967,296 ZB"`;
230+
231+
exports[`formatUnit should work with base { base: 2, unit: 'zettabyte-humanized' } 1`] = `"0 ZB"`;
232+
119233
exports[`formatUnit should work with locale en and { unit: 'bit' } 1`] = `"12.56 b"`;
120234

121235
exports[`formatUnit should work with locale en and { unit: 'bit-humanized' } 1`] = `"13 b"`;

packages/use-i18n/src/__tests__/formatUnit.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ const tests: TestType[] = [
4444
'fr',
4545
12,
4646
]),
47+
...SUPPORTED_UNITS.map<TestType>(unit => [
48+
'should work with base',
49+
{ base: 2, unit },
50+
'en',
51+
4294967296, // 4 GB
52+
]),
4753
]
4854

4955
describe('formatUnit', () => {

packages/use-i18n/src/formatUnit.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ const format =
137137
maximumFractionDigits,
138138
minimumFractionDigits,
139139
short = true,
140+
base = 10,
140141
}: {
141142
maximumFractionDigits?: number
142143
minimumFractionDigits?: number
143144
short?: boolean
145+
base?: number
144146
},
145147
): string => {
146148
let computedExponent = exponent
@@ -149,7 +151,7 @@ const format =
149151
if (humanize) {
150152
if (computedExponent) {
151153
const value = filesize(amount, {
152-
base: 10,
154+
base,
153155
exponent: exponents.findIndex(
154156
exp => exp.name === (computedExponent as Exponent).name,
155157
),
@@ -159,7 +161,7 @@ const format =
159161
computedValue = value.value
160162
} else {
161163
const value = filesize(amount, {
162-
base: 10,
164+
base,
163165
output: 'object',
164166
round: maximumFractionDigits,
165167
}) as unknown as { value: number; symbol: string; exponent: number }
@@ -247,7 +249,16 @@ export const supportedUnits: Partial<
247249

248250
export type FormatUnitOptions = {
249251
unit: SupportedUnits
252+
/**
253+
default: true
254+
*/
250255
short?: boolean
256+
maximumFractionDigits?: number
257+
minimumFractionDigits?: number
258+
/**
259+
default: 10
260+
*/
261+
base?: number
251262
}
252263

253264
const formatUnit = (

0 commit comments

Comments
 (0)