Client - init distance converter

This commit is contained in:
Sam 2021-11-14 10:55:39 +01:00
parent 197d30b2e2
commit 61738ebf85
3 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,13 @@
export type TUnitSystem = 'imperial' | 'metric'
export type TUnit = 'ft' | 'mi' | 'm' | 'km'
export type TFactor = {
[k in string]: Record<string, number>
}
export interface IUnit {
unit: TUnit
system: TUnitSystem
multiplier: number
}

View File

@ -0,0 +1,52 @@
import { IUnit, TFactor, TUnit } from '@/types/units'
export const units: Record<string, IUnit> = {
ft: {
unit: 'ft',
system: 'imperial',
multiplier: 1,
},
mi: {
unit: 'mi',
system: 'imperial',
multiplier: 5280,
},
m: {
unit: 'm',
system: 'metric',
multiplier: 1,
},
km: {
unit: 'm',
system: 'metric',
multiplier: 1000,
},
}
const factors: TFactor = {
metric: {
imperial: 3.280839895,
metric: 1,
},
imperial: {
metric: 1 / 3.280839895,
imperial: 1,
},
}
export const convertDistance = (
distance: number,
from: TUnit,
to: TUnit,
digits: number | null = 3
): number => {
const unitFrom = units[from]
const unitTo = units[to]
const convertedDistance =
(distance * unitFrom.multiplier * factors[unitFrom.system][unitTo.system]) /
unitTo.multiplier
if (digits !== null) {
return parseFloat(convertedDistance.toFixed(digits))
}
return convertedDistance
}

View File

@ -0,0 +1,58 @@
import { assert } from 'chai'
import { TUnit } from '@/types/units'
import { convertDistance } from '@/utils/units'
describe('convertDistance', () => {
const testsParams: [number, TUnit, TUnit, number][] = [
[0, 'm', 'ft', 0],
[5, 'm', 'ft', 16.404],
[5, 'm', 'mi', 0.003],
[5, 'm', 'm', 5.0],
[5, 'm', 'km', 0.005],
[5, 'km', 'ft', 16404.199],
[5, 'km', 'mi', 3.107],
[5, 'km', 'm', 5000.0],
[5, 'km', 'km', 5.0],
[5, 'ft', 'ft', 5.0],
[5, 'ft', 'mi', 0.001],
[5, 'ft', 'm', 1.524],
[5, 'ft', 'km', 0.002],
[5, 'mi', 'ft', 26400.0],
[5, 'mi', 'mi', 5.0],
[5, 'mi', 'm', 8046.72],
[5, 'mi', 'km', 8.047],
]
testsParams.map((testParams) => {
it(`convert ${testParams[0]}${testParams[1]} in ${testParams[2]}}`, () => {
assert.equal(
convertDistance(testParams[0], testParams[1], testParams[2]),
testParams[3]
)
})
})
})
describe('convertDistance w/ digits', () => {
const testsParams: [number, TUnit, TUnit, number | null, number][] = [
[5, 'km', 'mi', null, 3.106855961174243],
[5, 'km', 'mi', 0, 3],
[5, 'km', 'mi', 1, 3.1],
[5, 'km', 'mi', 2, 3.11],
]
testsParams.map((testParams) => {
it(`convert ${testParams[0]}${testParams[1]} in ${testParams[2]}}`, () => {
assert.equal(
convertDistance(
testParams[0],
testParams[1],
testParams[2],
testParams[3]
),
testParams[4]
)
})
})
})