commit db2f7a77fc6fc5cfb2a888bceccb0d1709df9130
parent 6b9c27168d431654d0c8484167f5d97be2ae6d96
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date: Sun, 9 Jun 2024 16:18:36 -0700
Refactor pace util methods
Diffstat:
3 files changed, 28 insertions(+), 50 deletions(-)
diff --git a/src/utils/calculators.js b/src/utils/calculators.js
@@ -49,8 +49,7 @@ function calculatePaceResults(input, target, defaultUnitSystem) {
result: target.type === 'distance' ? 'time' : 'distance',
};
- const pace = paceUtils.getPace(unitUtils.convertDistance(input.distanceValue, input.distanceUnit,
- 'meters'), input.time);
+ const d1 = unitUtils.convertDistance(input.distanceValue, input.distanceUnit, 'meters');
// Add missing value to result
if (target.type === 'distance') {
@@ -58,21 +57,15 @@ function calculatePaceResults(input, target, defaultUnitSystem) {
const d2 = unitUtils.convertDistance(target.distanceValue, target.distanceUnit, 'meters');
// Calculate time to travel distance at input pace
- const time = paceUtils.getTime(pace, d2);
-
- // Update result
- result.time = time;
+ result.time = paceUtils.calculateTime(d1, input.time, d2);
} else {
// Calculate distance traveled in time at input pace
- let distance = paceUtils.getDistance(pace, target.time);
+ const d2 = paceUtils.calculateDistance(input.time, d1, target.time);
// Convert output distance into default distance unit
- distance = unitUtils.convertDistance(distance, 'meters',
- unitUtils.getDefaultDistanceUnit(defaultUnitSystem));
-
- // Update result
- result.distanceValue = distance;
- result.distanceUnit = unitUtils.getDefaultDistanceUnit(defaultUnitSystem);
+ const units = unitUtils.getDefaultDistanceUnit(defaultUnitSystem);
+ result.distanceValue = unitUtils.convertDistance(d2, 'meters', units);
+ result.distanceUnit = units;
}
// Return result
diff --git a/src/utils/paces.js b/src/utils/paces.js
@@ -1,35 +1,26 @@
/**
- * Calculate pace from distance and time
- * @param {Number} distance The distance (in meters)
- * @param {Number} time The time (in seconds)
- * @returns {Number} The pace (in seconds per meter)
+ * Calculate time from a distance and input pace
+ * @param {Number} d1 The input pace distance (in any unit)
+ * @param {Number} t1 The input pace time (in seconds)
+ * @param {Number} d2 The output distance (in the same unit as d1)
+ * @returns {Number} The output time (in seconds)
*/
-function getPace(distance, time) {
- return time / distance;
+function calculateTime(d1, t1, d2) {
+ return (t1 / d1) * d2
}
/**
- * Calculate time from pace and distance
- * @param {Number} pace The pace (in seconds per meter)
- * @param {Number} distance The distance (in meters)
- * @returns {Number} The time (in seconds)
+ * Calculate distance from a time and input pace
+ * @param {Number} t1 The input pace time (in seconds)
+ * @param {Number} d1 The input pace distance (in any unit)
+ * @param {Number} t2 The output time (in seconds)
+ * @returns {Number} The output distance (in the same unit as d1)
*/
-function getTime(pace, distance) {
- return pace * distance;
-}
-
-/**
- * Calculate distance from pace and time
- * @param {Number} pace The pace (in seconds per meter)
- * @param {Number} time The time (in seconds)
- * @return {Number} The distance (in meters)
- */
-function getDistance(pace, time) {
- return time / pace;
+function calculateDistance(t1, d1, t2) {
+ return (d1 / t1) * t2
}
export default {
- getPace,
- getTime,
- getDistance,
+ calculateTime,
+ calculateDistance,
};
diff --git a/tests/unit/utils/paces.spec.js b/tests/unit/utils/paces.spec.js
@@ -1,20 +1,14 @@
import { describe, test, expect } from 'vitest';
import paces from '@/utils/paces';
-describe('getPace method', () => {
- test('2 meters in 6 seconds should equal 3 seconds per meter', () => {
- expect(paces.getPace(2, 6)).to.equal(3);
+describe('calculateTime method', () => {
+ test('1 meters in 3 seconds should equal 2 meters in 6 seconds', () => {
+ expect(paces.calculateTime(1, 3, 2)).to.equal(6);
});
});
-describe('getTime method', () => {
- test('2 meters at 3 seconds per meter should equal 6 seconds', () => {
- expect(paces.getTime(3, 2)).to.equal(6);
- });
-});
-
-describe('getDistance method', () => {
- test('6 seconds at 3 seconds per meter should equal 2 meters', () => {
- expect(paces.getDistance(3, 6)).to.equal(2);
+describe('calculateDistance method', () => {
+ test('1 meter in 3 seconds should equal 2 meters in 6 seconds', () => {
+ expect(paces.calculateDistance(3, 1, 6)).to.equal(2);
});
});