Client - refacto util functions for temperature and wind speed

This commit is contained in:
Sam 2022-11-17 00:20:21 +01:00
parent 940f0a8416
commit 546315e218
4 changed files with 68 additions and 18 deletions

View File

@ -1,8 +1,6 @@
<template>
<div class="wind">
{{ useImperialUnits ?
convert_mps_to_mph(Number(weather.wind)).toFixed(1) + ' mph' :
Number(weather.wind).toFixed(1) + " m/s" }}
{{ getWindSpeed(weather.wind, useImperialUnits) }}
<div class="wind-bearing">
<i
v-if="weather.windBearing"
@ -22,6 +20,7 @@
import { useI18n } from 'vue-i18n'
import { IWeather } from '@/types/workouts'
import { getWindSpeed } from '@/utils/units'
import { convertDegreeToDirection } from '@/utils/weather'
interface Props {
@ -40,10 +39,6 @@
)}`
)
}
function convert_mps_to_mph(windSpeed: number): number {
return windSpeed * 2.2369363
}
</script>
<style lang="scss" scoped>

View File

@ -58,14 +58,20 @@
/>
</td>
<td>
{{ useImperialUnits ?
convertCelsiusToFahrenheit(Number(workoutObject.weatherStart.temperature)).toFixed(1) + " °F" :
Number(workoutObject.weatherStart.temperature).toFixed(1) + " °C"}}
{{
getTemperature(
workoutObject.weatherStart.temperature,
useImperialUnits
)
}}
</td>
<td>
{{ useImperialUnits ?
convertCelsiusToFahrenheit(Number(workoutObject.weatherEnd.temperature)).toFixed(1) + " °F" :
Number(workoutObject.weatherEnd.temperature).toFixed(1) + " °C"}}
{{
getTemperature(
workoutObject.weatherEnd.temperature,
useImperialUnits
)
}}
</td>
</tr>
<tr>
@ -116,6 +122,7 @@
import WeatherWind from '@/components/Workout/WorkoutDetail/WeatherWind.vue'
import { IWorkoutObject } from '@/types/workouts'
import { getTemperature } from '@/utils/units'
interface Props {
workoutObject: IWorkoutObject
@ -124,10 +131,6 @@
const props = defineProps<Props>()
const { useImperialUnits, workoutObject } = toRefs(props)
function convertCelsiusToFahrenheit(celsius_temp: number): number {
return celsius_temp * 1.8 + 32
}
</script>
<style lang="scss" scoped>

View File

@ -63,3 +63,23 @@ export const convertStatsDistance = (
const unitTo = useImperialUnits ? units[unitFrom].defaultTarget : unitFrom
return useImperialUnits ? convertDistance(value, unitFrom, unitTo, 2) : value
}
export const getTemperature = (
temperatureInCelsius: number,
useImperialUnits: boolean
): string => {
const temperature = useImperialUnits
? temperatureInCelsius * 1.8 + 32
: temperatureInCelsius
const unit = useImperialUnits ? ' °F' : '°C'
return `${temperature === 0 ? 0 : Number(temperature).toFixed(1)}${unit}`
}
export const getWindSpeed = (
windSpeedInMS: number,
useImperialUnits: boolean
): string => {
const windSpeed = useImperialUnits ? windSpeedInMS * 2.2369363 : windSpeedInMS
const unit = useImperialUnits ? ' mph' : 'm/s'
return `${windSpeed === 0 ? 0 : Number(windSpeed).toFixed(1)}${unit}`
}

View File

@ -1,7 +1,7 @@
import { assert } from 'chai'
import { TUnit } from '@/types/units'
import { convertDistance } from '@/utils/units'
import { convertDistance, getTemperature, getWindSpeed } from '@/utils/units'
describe('convertDistance', () => {
const testsParams: [number, TUnit, TUnit, number][] = [
@ -56,3 +56,35 @@ describe('convertDistance w/ digits', () => {
})
})
})
describe('getTemperature', () => {
const testsParams: [number, boolean, string][] = [
[0, false, '0°C'],
[10.0, false, '10.0°C'],
[10.3, false, '10.3°C'],
[0, true, '32.0 °F'],
[13.0, true, '55.4 °F'],
]
testsParams.map((testParams) => {
it(`get temperature for input: ${testParams[0]} and imperialUnits: ${testParams[1]}`, () => {
assert.equal(getTemperature(testParams[0], testParams[1]), testParams[2])
})
})
})
describe('getWindSpeed', () => {
const testsParams: [number, boolean, string][] = [
[0, false, '0m/s'],
[6.0, false, '6.0m/s'],
[6.3, false, '6.3m/s'],
[0, true, '0 mph'],
[13.2, true, '29.5 mph'],
]
testsParams.map((testParams) => {
it(`get wind speed for input: ${testParams[0]} and imperialUnits: ${testParams[1]}`, () => {
assert.equal(getWindSpeed(testParams[0], testParams[1]), testParams[2])
})
})
})