-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
feat: Added speed unit conversion functionality #1450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Speed conversion | ||
* | ||
* This function converts speed units including kilometers per hour (km/h), | ||
* meters per second (m/s), miles per hour (mph), and knots (knot). | ||
* | ||
* https://en.wikipedia.org/wiki/Kilometres_per_hour | ||
* https://en.wikipedia.org/wiki/Miles_per_hour | ||
* https://en.wikipedia.org/wiki/Metre_per_second | ||
* https://en.wikipedia.org/wiki/Knot_(unit) | ||
* | ||
* chart for unit conversions. | ||
* | ||
* @constant {Object} speedChart | ||
* @property {number} speedChart['km/h'] - Conversion factor for kilometers per hour (km/h). | ||
* @property {number} speedChart['m/s'] - Conversion factor for meters per second (m/s). | ||
* @property {number} speedChart['mph'] - Conversion factor for miles per hour (mph). | ||
* @property {number} speedChart['knot'] - Conversion factor for knots (knot). | ||
*/ | ||
const speedChart = { | ||
'km/h': 1.0, | ||
'm/s': 3.6, | ||
mph: 1.609344, | ||
knot: 1.852 | ||
} | ||
|
||
/** | ||
* Inverse speed conversion chart for unit conversions. | ||
* | ||
* @constant {Object} speedChartInverse | ||
* @property {number} speedChartInverse['km/h'] - Inverse conversion factor for kilometers per hour (km/h). | ||
* @property {number} speedChartInverse['m/s'] - Inverse conversion factor for meters per second (m/s). | ||
* @property {number} speedChartInverse['mph'] - Inverse conversion factor for miles per hour (mph). | ||
* @property {number} speedChartInverse['knot'] - Inverse conversion factor for knots (knot). | ||
*/ | ||
const speedChartInverse = { | ||
'km/h': 1.0, | ||
'm/s': 0.277777778, | ||
mph: 0.621371192, | ||
knot: 0.539956803 | ||
} | ||
|
||
/** | ||
* Convert speed from one unit to another using the speedChart and speedChartInverse. | ||
* | ||
* @param {number} speed - The speed value to be converted. | ||
* @param {string} inputUnit - The source unit (e.g., 'km/h', 'm/s', 'mph', 'knot'). | ||
* @param {string} outputUnit - The target unit (e.g., 'km/h', 'm/s', 'mph', 'knot'). | ||
* @throws {Error} Throws an error if the source or target unit is not recognized. | ||
* @returns {number} The converted speed value. | ||
*/ | ||
const convertSpeed = (speed, inputUnit, outputUnit) => { | ||
if (!(outputUnit in speedChart) || !(inputUnit in speedChartInverse)) { | ||
const validUnits = Object.keys(speedChartInverse).join(', ') | ||
throw new Error( | ||
`Incorrect 'inputUnit' or 'outputUnit' value: ${inputUnit}, ${outputUnit}\n Valid values are: ${validUnits}` | ||
) | ||
} | ||
|
||
return parseFloat( | ||
(speed * speedChart[inputUnit] * speedChartInverse[outputUnit]).toFixed(3) | ||
) | ||
} | ||
|
||
export { convertSpeed } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { convertSpeed } from '../SpeedConversion' | ||
|
||
describe('Speed Unit Conversions', () => { | ||
it('100 km/h to m/s', () => { | ||
expect(convertSpeed(100, 'km/h', 'm/s')).toBe(27.778, 3) | ||
}) | ||
|
||
it('100 km/h to mph', () => { | ||
expect(convertSpeed(100, 'km/h', 'mph')).toBe(62.137, 3) | ||
}) | ||
|
||
it('100 km/h to knot', () => { | ||
expect(convertSpeed(100, 'km/h', 'knot')).toBe(53.996, 3) | ||
}) | ||
|
||
it('100 m/s to km/h', () => { | ||
expect(convertSpeed(100, 'm/s', 'km/h')).toBe(360.0, 3) | ||
}) | ||
|
||
it('100 m/s to mph', () => { | ||
expect(convertSpeed(100, 'm/s', 'mph')).toBe(223.694, 3) | ||
}) | ||
|
||
it('100 m/s to knot', () => { | ||
expect(convertSpeed(100, 'm/s', 'knot')).toBe(194.384, 3) | ||
}) | ||
|
||
it('100 mph to km/h', () => { | ||
expect(convertSpeed(100, 'mph', 'km/h')).toBe(160.934, 3) | ||
}) | ||
|
||
it('100 mph to m/s', () => { | ||
expect(convertSpeed(100, 'mph', 'm/s')).toBe(44.704, 3) | ||
}) | ||
|
||
it('100 mph to knot', () => { | ||
expect(convertSpeed(100, 'mph', 'knot')).toBe(86.898, 3) | ||
}) | ||
|
||
it('100 knot to km/h', () => { | ||
expect(convertSpeed(100, 'knot', 'km/h')).toBe(185.2, 3) | ||
}) | ||
|
||
it('100 knot to m/s', () => { | ||
expect(convertSpeed(100, 'knot', 'm/s')).toBe(51.444, 3) | ||
}) | ||
|
||
it('100 knot to mph', () => { | ||
expect(convertSpeed(100, 'knot', 'mph')).toBe(115.078, 3) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.