Skip to content

Commit cb5104b

Browse files
committed
refactor(ui): wrapped formatter
Signed-off-by: Neko Ayaka <[email protected]>
1 parent 6e09bb9 commit cb5104b

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

app/components/Generate.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { EncodedBlock } from '~~/utils/lt-code'
33
import { blockToBinary, createEncoder } from '~~/utils/lt-code'
44
import { fromUint8Array } from 'js-base64'
55
import { renderSVG } from 'uqr'
6+
import { useKiloBytesNumberFormat } from '~/composables/intlNumberFormat'
67
78
const props = withDefaults(defineProps<{
89
data: Uint8Array
@@ -20,6 +21,7 @@ const block = shallowRef<EncodedBlock>()
2021
2122
const renderTime = ref(0)
2223
const framePerSecond = computed(() => 1000 / renderTime.value)
24+
const bytes = useKiloBytesNumberFormat(computed(() => ((block.value?.bytes || 0) / 1024).toFixed(2)))
2325
2426
onMounted(() => {
2527
let frame = performance.now()
@@ -47,7 +49,7 @@ onMounted(() => {
4749
<span text-neutral-500>Total</span>
4850
<span text-right md:text-left>{{ block?.k }}</span>
4951
<span text-neutral-500>Bytes</span>
50-
<span text-right md:text-left>{{ ((block?.bytes || 0) / 1024).toFixed(2) }} KB</span>
52+
<span text-right md:text-left>{{ bytes }}</span>
5153
<span text-neutral-500>Bitrate</span>
5254
<span text-right md:text-left>{{ ((block?.bytes || 0) / 1024 * framePerSecond).toFixed(2) }} Kbps</span>
5355
<span text-neutral-500>Frame Count</span>

app/components/Scan.vue

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { readFileHeaderMetaFromBuffer } from '~~/utils/lt-code/binary-meta'
44
55
import { toUint8Array } from 'js-base64'
66
import QrScanner from 'qr-scanner'
7+
import { useKiloBytesNumberFormat } from '~/composables/intlNumberFormat'
78
import { useBytesRate } from '~/composables/timeseries'
89
import { CameraSignalStatus } from '~/types'
910
@@ -173,6 +174,9 @@ const filename = ref<string | undefined>()
173174
const contentType = ref<string | undefined>()
174175
const textContent = ref<string | undefined>()
175176
177+
const bytesFormatted = useKiloBytesNumberFormat(computed(() => (bytes.value / 1024).toFixed(2)))
178+
const receivedBytesFormatted = useKiloBytesNumberFormat(computed(() => (receivedBytes.value / 1024).toFixed(2)))
179+
176180
function getStatus() {
177181
const array = Array.from({ length: k.value }, () => 0)
178182
for (let i = 0; i < k.value; i++) {
@@ -342,9 +346,9 @@ function now() {
342346
<span text-neutral-500>Received blocks</span>
343347
<span text-right md:text-left>{{ decoder.encodedCount }}</span>
344348
<span text-neutral-500>Expected bytes</span>
345-
<span text-right md:text-left>{{ (bytes / 1024).toFixed(2) }} KB</span>
349+
<span text-right md:text-left>{{ bytesFormatted }} KB</span>
346350
<span text-neutral-500>Received bytes</span>
347-
<span text-right md:text-left>{{ (receivedBytes / 1024).toFixed(2) }} KB ({{ bytes === 0 ? 0 : (receivedBytes / bytes * 100).toFixed(2) }}%)</span>
351+
<span text-right md:text-left>{{ receivedBytesFormatted }} KB ({{ bytes === 0 ? 0 : (receivedBytes / bytes * 100).toFixed(2) }}%)</span>
348352
<span text-neutral-500>Time elapsed</span>
349353
<span text-right md:text-left>{{ k === 0 ? 0 : (((endTime || now()) - startTime) / 1000).toFixed(2) }}s</span>
350354
<span text-neutral-500>Average bitrate</span>

app/composables/intlNumberFormat.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export function useNumberFormat(
2+
value: MaybeRef<number | string> | ComputedRef<number | string>,
3+
options?: {
4+
locales?: Intl.LocalesArgument
5+
} & Intl.NumberFormatOptions,
6+
) {
7+
const opts = {
8+
locales: options?.locales ?? 'en-US',
9+
...options,
10+
}
11+
12+
const formatter = new Intl.NumberFormat(opts.locales, opts)
13+
const val = toRef(value)
14+
15+
return computed(() => {
16+
const parsedNum = Number.parseFloat(val.value as string)
17+
if (Number.isNaN(parsedNum)) {
18+
return formatter.format(0)
19+
}
20+
21+
return formatter.format(parsedNum)
22+
})
23+
}
24+
25+
export function useKiloBytesNumberFormat(
26+
value: MaybeRef<number | string> | ComputedRef<number | string>,
27+
options?: {
28+
locales?: Intl.LocalesArgument
29+
} & Intl.NumberFormatOptions,
30+
) {
31+
return useNumberFormat(value, {
32+
...options,
33+
style: 'unit',
34+
unit: 'kilobyte',
35+
})
36+
}

0 commit comments

Comments
 (0)