Skip to content

Commit

Permalink
fix: format numbers using the local users format
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed May 29, 2024
1 parent 43fb25c commit 101a29c
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 43 deletions.
3 changes: 2 additions & 1 deletion src/chart/toChartData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type GainReadings,
} from '#model/PCA20035-solar/HistoryContext.js'
import { xAxisForType } from '#chart/xAxisForType.js'
import { formatFloat } from '#utils/format.js'

export const toChartData = ({
battery,
Expand Down Expand Up @@ -35,7 +36,7 @@ export const toChartData = ({
subMilliseconds(base, base.getTime() - ts),
]),
color: 'var(--color-nordic-sun)',
format: (v) => `${v.toFixed(1)} mA`,
format: (v) => `${formatFloat(v)} mA`,
helperLines: [
{
label: '1m',
Expand Down
5 changes: 3 additions & 2 deletions src/components/BME680.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LoadingIndicator } from '#components/ValueLoading.js'
import { useDevice } from '#context/Device.js'
import { isEnvironment, toEnvironment } from '#proto/lwm2m.js'
import { formatFloat } from '#utils/format.js'
import {
AngryIcon,
AnnoyedIcon,
Expand Down Expand Up @@ -57,12 +58,12 @@ export const EnvironmentReadings = () => {
<span>
{c !== undefined && (
<span class="me-2">
<ThermometerIcon /> {c.toFixed(1)} °C
<ThermometerIcon /> {formatFloat(c)} °C
</span>
)}
{p !== undefined && (
<span class="me-2">
<DropletsIcon /> {p.toFixed(1)} %
<DropletsIcon /> {formatFloat(p)} %
</span>
)}
{mbar !== undefined && (
Expand Down
45 changes: 25 additions & 20 deletions src/components/DeviceHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
} from 'lucide-preact'
import { useState } from 'preact/hooks'
import './DeviceHeader.css'
import { CountryFlag } from './CountryFlag.js'
import { formatFloat } from '#utils/format.js'

export const DeviceHeader = ({ device }: { device: Device }) => {
const type = device.model
Expand Down Expand Up @@ -156,7 +158,7 @@ const EnvironmentInfo = () => {
{c === undefined && <LoadingIndicator width={150} />}
{c !== undefined && (
<span class="me-2">
<ThermometerIcon /> {c.toFixed(1)} °C
<ThermometerIcon /> {formatFloat(c)} °C
</span>
)}
{iaq === undefined && <LoadingIndicator width={150} class="mt-1" />}
Expand All @@ -179,15 +181,15 @@ const EnvironmentInfo = () => {

const NetworkModeInfo = () => {
const { reported } = useDevice()
const { networkMode, currentBand, ts } =
const { networkMode, currentBand, mccmnc, ts } =
Object.values(reported)
.filter(isConnectionInformation)
.map(toConnectionInformation)[0] ?? {}

return (
<span class="d-flex flex-column">
<small class="text-muted">
<strong>Network mode</strong>
<strong>Network</strong>
</small>
{(networkMode === undefined || currentBand === undefined) && (
<>
Expand All @@ -197,23 +199,26 @@ const NetworkModeInfo = () => {
)}

{networkMode !== undefined && currentBand !== undefined && (
<abbr title={`Band ${currentBand}`} class="me-2">
{networkMode?.includes('LTE-M') ?? false ? (
<LTEm
style={{
height: '25px',
width: 'auto',
}}
/>
) : (
<NBIot
style={{
height: '25px',
width: 'auto',
}}
/>
)}
</abbr>
<span>
<abbr title={`Band ${currentBand}`} class="me-2">
{networkMode?.includes('LTE-M') ?? false ? (
<LTEm
style={{
height: '25px',
width: 'auto',
}}
/>
) : (
<NBIot
style={{
height: '25px',
width: 'auto',
}}
/>
)}
</abbr>
{mccmnc !== undefined && <CountryFlag mccmnc={mccmnc} />}
</span>
)}
{ts !== undefined && (
<small class="text-muted">
Expand Down
4 changes: 3 additions & 1 deletion src/components/DeviceModeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Ban, HistoryIcon, Satellite, Settings2, X } from 'lucide-preact'
import { useState } from 'preact/hooks'
import { Problem } from './Problem.js'
import { isEqual } from 'lodash-es'
import { formatFloat } from '#utils/format.js'

export const DeviceModeSelector = ({
device,
Expand Down Expand Up @@ -209,7 +210,8 @@ const DataUsageInfo = ({
>
<SIMIcon class="me-2 flex-shrink-0" size={18} />
<small>
This mode uses around {dataUsagePerDayMB.toFixed(2)} MB of data per day.
This mode uses around {formatFloat(dataUsagePerDayMB, 2)} MB of data per
day.
</small>
</span>
)
Expand Down
4 changes: 2 additions & 2 deletions src/map/LocationSourceLabels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export enum LocationSource {
// Uses nrfcloud.com wording
export const LocationSourceLabels = new Map<string, string>([
[LocationSource.WIFI, 'Wi-Fi'],
[LocationSource.MCELL, 'multi-cell'],
[LocationSource.SCELL, 'single-cell'],
[LocationSource.MCELL, 'Multi-cell'],
[LocationSource.SCELL, 'Single-cell'],
[LocationSource.GNSS, 'GNSS'],
])

Expand Down
6 changes: 3 additions & 3 deletions src/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
type GeoLocation,
} from '#proto/lwm2m.js'
import { useDeviceLocation, type Locations } from '#context/DeviceLocation.js'
import { formatFloat, formatInt } from '#utils/format.js'

const trailColor = '#e169a5'
const defaultColor = '#C7C7C7'
Expand Down Expand Up @@ -530,11 +531,10 @@ export const Located = ({ location }: { location: GeoLocation }) => (
target="_blank"
class="text-light"
>
{location.lat.toFixed(5).replace(/0+$/, '')},{' '}
{location.lng.toFixed(5).replace(/0+$/, '')}
{formatFloat(location.lat, 5)}, {formatFloat(location.lng, 5)}
</a>{' '}
{location.acc !== undefined ? (
<>with an accuracy of {Math.round(location.acc)} m</>
<>with an accuracy of {formatInt(location.acc)} m</>
) : (
<>with an unspecified accuary</>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/model/PCA20035-solar/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HistoryChart } from '#chart/HistoryChart.js'
import { Ago } from '#components/Ago.js'
import { LoadingIndicator } from '#components/ValueLoading.js'
import { formatFloat } from '#utils/formatFloat.js'
import { formatFloat } from '#utils/format.js'
import { BatteryCharging, Sun } from 'lucide-preact'
import { toChartData } from '#chart/toChartData.js'
import { DateRangeButton } from '#chart/DateRangeButton.js'
Expand Down
20 changes: 20 additions & 0 deletions src/utils/format.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { format } from './format.js'

const { formatFloat, formatInt } = format('en-US')

void describe('formatInt()', () => {
void it('should format an integer', () => {
assert.equal(formatInt(1234), '1,234')
})
})

void describe('formatFloat()', () => {
void it('should format a float', () => {
assert.equal(formatFloat(1.234), '1.2')
})
void it('should format a float with custom number of digits', () => {
assert.equal(formatFloat(1.234, 2), '1.23')
})
})
39 changes: 39 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const format = (
locales?: Intl.LocalesArgument,
): {
formatInt: (value: number) => string
formatFloat: (value: number, maximumFractionDigits?: number) => string
} => {
const intFormatter = new Intl.NumberFormat(locales, {
maximumFractionDigits: 0,
})
const formatInt = (value: number) => intFormatter.format(value)

const formatters = new Map<number, Intl.NumberFormat>([
[
1,
new Intl.NumberFormat(locales, {
maximumFractionDigits: 1,
}),
],
])

const formatFloat = (value: number, maximumFractionDigits = 1) => {
if (!formatters.has(maximumFractionDigits)) {
const f = new Intl.NumberFormat(undefined, {
maximumFractionDigits,
})
formatters.set(maximumFractionDigits, f)
}
return formatters.get(maximumFractionDigits)!.format(value)
}

return {
formatInt,
formatFloat,
}
}

const { formatFloat, formatInt } = format()

export { formatFloat, formatInt }
11 changes: 0 additions & 11 deletions src/utils/formatFloat.spec.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/utils/formatFloat.ts

This file was deleted.

0 comments on commit 101a29c

Please sign in to comment.