Client - refacto util functions for temperature and wind speed
This commit is contained in:
parent
940f0a8416
commit
546315e218
@ -1,8 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="wind">
|
<div class="wind">
|
||||||
{{ useImperialUnits ?
|
{{ getWindSpeed(weather.wind, useImperialUnits) }}
|
||||||
convert_mps_to_mph(Number(weather.wind)).toFixed(1) + ' mph' :
|
|
||||||
Number(weather.wind).toFixed(1) + " m/s" }}
|
|
||||||
<div class="wind-bearing">
|
<div class="wind-bearing">
|
||||||
<i
|
<i
|
||||||
v-if="weather.windBearing"
|
v-if="weather.windBearing"
|
||||||
@ -22,6 +20,7 @@
|
|||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
|
|
||||||
import { IWeather } from '@/types/workouts'
|
import { IWeather } from '@/types/workouts'
|
||||||
|
import { getWindSpeed } from '@/utils/units'
|
||||||
import { convertDegreeToDirection } from '@/utils/weather'
|
import { convertDegreeToDirection } from '@/utils/weather'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@ -40,10 +39,6 @@
|
|||||||
)}`
|
)}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function convert_mps_to_mph(windSpeed: number): number {
|
|
||||||
return windSpeed * 2.2369363
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
@ -58,14 +58,20 @@
|
|||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{ useImperialUnits ?
|
{{
|
||||||
convertCelsiusToFahrenheit(Number(workoutObject.weatherStart.temperature)).toFixed(1) + " °F" :
|
getTemperature(
|
||||||
Number(workoutObject.weatherStart.temperature).toFixed(1) + " °C"}}
|
workoutObject.weatherStart.temperature,
|
||||||
|
useImperialUnits
|
||||||
|
)
|
||||||
|
}}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{ useImperialUnits ?
|
{{
|
||||||
convertCelsiusToFahrenheit(Number(workoutObject.weatherEnd.temperature)).toFixed(1) + " °F" :
|
getTemperature(
|
||||||
Number(workoutObject.weatherEnd.temperature).toFixed(1) + " °C"}}
|
workoutObject.weatherEnd.temperature,
|
||||||
|
useImperialUnits
|
||||||
|
)
|
||||||
|
}}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -116,6 +122,7 @@
|
|||||||
|
|
||||||
import WeatherWind from '@/components/Workout/WorkoutDetail/WeatherWind.vue'
|
import WeatherWind from '@/components/Workout/WorkoutDetail/WeatherWind.vue'
|
||||||
import { IWorkoutObject } from '@/types/workouts'
|
import { IWorkoutObject } from '@/types/workouts'
|
||||||
|
import { getTemperature } from '@/utils/units'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
workoutObject: IWorkoutObject
|
workoutObject: IWorkoutObject
|
||||||
@ -124,10 +131,6 @@
|
|||||||
const props = defineProps<Props>()
|
const props = defineProps<Props>()
|
||||||
|
|
||||||
const { useImperialUnits, workoutObject } = toRefs(props)
|
const { useImperialUnits, workoutObject } = toRefs(props)
|
||||||
|
|
||||||
function convertCelsiusToFahrenheit(celsius_temp: number): number {
|
|
||||||
return celsius_temp * 1.8 + 32
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
@ -63,3 +63,23 @@ export const convertStatsDistance = (
|
|||||||
const unitTo = useImperialUnits ? units[unitFrom].defaultTarget : unitFrom
|
const unitTo = useImperialUnits ? units[unitFrom].defaultTarget : unitFrom
|
||||||
return useImperialUnits ? convertDistance(value, unitFrom, unitTo, 2) : value
|
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}`
|
||||||
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { assert } from 'chai'
|
import { assert } from 'chai'
|
||||||
|
|
||||||
import { TUnit } from '@/types/units'
|
import { TUnit } from '@/types/units'
|
||||||
import { convertDistance } from '@/utils/units'
|
import { convertDistance, getTemperature, getWindSpeed } from '@/utils/units'
|
||||||
|
|
||||||
describe('convertDistance', () => {
|
describe('convertDistance', () => {
|
||||||
const testsParams: [number, TUnit, TUnit, number][] = [
|
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])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user