commit f8c51f5938c1d7ab6ba739898cd0d0440616e880
parent 08eab081d381784d88e61692e7263d6b5a98bf12
Author: ashermorgan <59518073+ashermorgan@users.noreply.github.com>
Date: Fri, 30 Jul 2021 17:19:08 -0700
Implement unit utilities
Diffstat:
| A | src/utils/units.js | | | 184 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | tests/unit/units.spec.js | | | 111 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 295 insertions(+), 0 deletions(-)
diff --git a/src/utils/units.js b/src/utils/units.js
@@ -0,0 +1,184 @@
+/**
+ * The time units
+ */
+const TIME_UNITS = {
+ second: 'second',
+ minute: 'minute',
+ hour: 'hour',
+};
+
+
+
+/**
+ * The value of each time unit in seconds
+ */
+const TIME_UNIT_VALUES = {
+ second: 1,
+ minute: 1 * 60,
+ hour: 1 * 60 * 60,
+};
+
+
+
+/**
+ * The distance units
+ */
+const DISTANCE_UNITS = {
+ meter: 'meter',
+ kilometer: 'kilometer',
+ yard: 'yard',
+ mile: 'mile',
+ marathon: 'marathon',
+};
+
+
+
+/**
+ * The value of each distance unit in meters
+ */
+const DISTANCE_UNIT_VALUES = {
+ meter: 1,
+ kilometer: 1000,
+ yard: 0.9144,
+ mile: 1609.3499,
+ marathon: 42195,
+};
+
+
+
+/**
+ * The speed units
+ */
+const SPEED_UNITS = {
+ meters_per_second: 'meters_per_second',
+ kilometers_per_hour: 'kilometers_per_hour',
+ miles_per_hour: 'miles_per_hour',
+};
+
+
+
+/**
+ * The value of each speed unit in meters per second
+ */
+const SPEED_UNIT_VALUES = {
+ meters_per_second: 1,
+ kilometers_per_hour: DISTANCE_UNIT_VALUES.kilometer / TIME_UNIT_VALUES.hour,
+ miles_per_hour: DISTANCE_UNIT_VALUES.mile / TIME_UNIT_VALUES.hour,
+};
+
+
+
+/**
+ * The pace units
+ */
+const PACE_UNITS = {
+ seconds_per_meter: 'seconds_per_meter',
+ minutes_per_kilometer: 'minutes_per_kilometer',
+ minutes_per_mile: 'minutes_per_mile',
+};
+
+
+
+/**
+ * The value of each pace unit in seconds per meter
+ */
+const PACE_UNIT_VALUES = {
+ seconds_per_meter: 1,
+ minutes_per_kilometer: TIME_UNIT_VALUES.minute / DISTANCE_UNIT_VALUES.kilometer,
+ minutes_per_mile: TIME_UNIT_VALUES.minute / DISTANCE_UNIT_VALUES.mile,
+};
+
+
+
+/**
+ * Convert between time units
+ * @param {Number} inputValue The input value
+ * @param {String} inputUnit The unit of the input
+ * @param {String} outputUnit The unit of the output
+ * @returns {Number} The output
+ */
+function convertTime(inputValue, inputUnits, outputUnits) {
+ return inputValue * TIME_UNIT_VALUES[inputUnits] / TIME_UNIT_VALUES[outputUnits];
+}
+
+
+
+/**
+ * Convert between distance units
+ * @param {Number} inputValue The input value
+ * @param {String} inputUnit The unit of the input
+ * @param {String} outputUnit The unit of the output
+ * @returns {Number} The output
+ */
+function convertDistance(inputValue, inputUnits, outputUnits) {
+ return inputValue * DISTANCE_UNIT_VALUES[inputUnits] / DISTANCE_UNIT_VALUES[outputUnits];
+}
+
+
+
+/**
+ * Convert between speed units
+ * @param {Number} inputValue The input value
+ * @param {String} inputUnit The unit of the input
+ * @param {String} outputUnit The unit of the output
+ * @returns {Number} The output
+ */
+function convertSpeed(inputValue, inputUnits, outputUnits) {
+ return inputValue * SPEED_UNIT_VALUES[inputUnits] / SPEED_UNIT_VALUES[outputUnits];
+}
+
+
+
+/**
+ * Convert between pace units
+ * @param {Number} inputValue The input value
+ * @param {String} inputUnit The unit of the input
+ * @param {String} outputUnit The unit of the output
+ * @returns {Number} The output
+ */
+function convertPace(inputValue, inputUnits, outputUnits) {
+ return inputValue * PACE_UNIT_VALUES[inputUnits] / PACE_UNIT_VALUES[outputUnits];
+}
+
+
+
+/**
+ * Convert between speed and/or pace units
+ * @param {Number} inputValue The input value
+ * @param {String} inputUnit The unit of the input
+ * @param {String} outputUnit The unit of the output
+ * @returns {Number} The output
+ */
+function convertSpeedPace(inputValue, inputUnits, outputUnits) {
+ // Calculate input speed
+ let speed;
+ if (PACE_UNIT_VALUES.hasOwnProperty(inputUnits)) {
+ speed = 1 / (inputValue * PACE_UNIT_VALUES[inputUnits]);
+ }
+ else {
+ speed = inputValue * SPEED_UNIT_VALUES[inputUnits];
+ }
+
+ // Calculate output
+ if (PACE_UNIT_VALUES.hasOwnProperty(outputUnits)) {
+ return (1 / speed) / PACE_UNIT_VALUES[outputUnits];
+ }
+ else {
+ return speed / SPEED_UNIT_VALUES[outputUnits];
+ }
+}
+
+
+
+export default {
+ TIME_UNITS,
+ DISTANCE_UNITS,
+ SPEED_UNITS,
+ PACE_UNITS,
+
+ convertTime,
+ convertDistance,
+ convertSpeed,
+ convertPace,
+ convertSpeedPace,
+}
diff --git a/tests/unit/units.spec.js b/tests/unit/units.spec.js
@@ -0,0 +1,111 @@
+import { expect } from 'chai';
+import { shallowMount } from '@vue/test-utils';
+import units from '@/utils/units.js';
+
+
+
+describe('utils/units.js', () => {
+ describe('convertTime method', () => {
+ it('90 seconds should equal 1.5 minutes', () => {
+ let result = units.convertTime(
+ 90,
+ units.TIME_UNITS.second,
+ units.TIME_UNITS.minute
+ );
+ expect(result).to.equal(1.5);
+ });
+
+ it('1.5 minutes should equal 95 seconds', () => {
+ let result = units.convertTime(
+ 1.5,
+ units.TIME_UNITS.minute,
+ units.TIME_UNITS.second
+ );
+ expect(result).to.equal(90);
+ });
+ });
+
+
+ describe('convertDistance method', () => {
+ it('100 meters should equal 0.1 kilometers', () => {
+ let result = units.convertDistance(
+ 100,
+ units.DISTANCE_UNITS.meter,
+ units.DISTANCE_UNITS.kilometer
+ );
+ expect(result).to.equal(0.1);
+ });
+
+ it('0.1 kilometers should equal 100 meters', () => {
+ let result = units.convertDistance(
+ 0.1,
+ units.DISTANCE_UNITS.kilometer,
+ units.DISTANCE_UNITS.meter
+ );
+ expect(result).to.equal(100);
+ });
+ });
+
+
+ describe('convertSpeed method', () => {
+ it('1000 meters per seconds should equal 3600 kilometers per hour', () => {
+ let result = units.convertSpeed(
+ 1000,
+ units.SPEED_UNITS.meters_per_second,
+ units.SPEED_UNITS.kilometers_per_hour
+ );
+ expect(result).to.equal(3600);
+ });
+
+ it('3600 kilometers per hour should equal 1000 meters per second', () => {
+ let result = units.convertSpeed(
+ 3600,
+ units.SPEED_UNITS.kilometers_per_hour,
+ units.SPEED_UNITS.meters_per_second
+ );
+ expect(result).to.equal(1000);
+ });
+ });
+
+
+ describe('convertPace method', () => {
+ it('60 seconds per meter should equal 1000 minutes per kilometer', () => {
+ let result = units.convertPace(
+ 60,
+ units.PACE_UNITS.seconds_per_meter,
+ units.PACE_UNITS.minutes_per_kilometer
+ );
+ expect(result).to.equal(1000);
+ });
+
+ it('1000 minutes per kilometer should equal 60 seconds per meter', () => {
+ let result = units.convertPace(
+ 1000,
+ units.PACE_UNITS.minutes_per_kilometer,
+ units.PACE_UNITS.seconds_per_meter
+ );
+ expect(result).to.equal(60);
+ });
+ });
+
+
+ describe('convertSpeedPace method', () => {
+ it('60 kilometers per hour should equal 1 minute per kilometer', () => {
+ let result = units.convertSpeedPace(
+ 60,
+ units.SPEED_UNITS.kilometers_per_hour,
+ units.PACE_UNITS.minutes_per_kilometer
+ );
+ expect(result).to.equal(1);
+ });
+
+ it('1 minute per kilometer should equal 60 kilometers per hour', () => {
+ let result = units.convertSpeedPace(
+ 1,
+ units.PACE_UNITS.minutes_per_kilometer,
+ units.SPEED_UNITS.kilometers_per_hour
+ );
+ expect(result).to.equal(60);
+ });
+ });
+});