This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add inches to metric conversion (#226)
Closes #225
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,25 @@ | ||
export default inchesToMetric | ||
|
||
const cmConversion = 2.54 | ||
|
||
const mapper = { | ||
cm: cmConversion, | ||
m: cmConversion / 100, | ||
mm: cmConversion * 10, | ||
} | ||
|
||
function inchesToMetric(value, unit = 'cm') { | ||
if (isNaN(value)) { | ||
throw new TypeError('First argument must be of type "number"') | ||
} | ||
|
||
if (value > 12 || value < 0) { | ||
throw new RangeError('Inches have to be between 0 and 12') | ||
} | ||
|
||
if (!mapper[unit]) { | ||
throw new Error('Can only convert inches to meters ("m"), centimeters ("cm"), or milimeters ("mm").') | ||
} | ||
|
||
return value * mapper[unit] | ||
} |
This file contains 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
This file contains 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,30 @@ | ||
import test from 'ava' | ||
import {inchesToMetric} from './../src' | ||
|
||
test('should throw error if a number is not passed', t => { | ||
t.throws(() => inchesToMetric('foo')) | ||
}) | ||
|
||
test('should throw error if inches is out of range value', t => { | ||
t.throws(() => inchesToMetric(22)) | ||
t.throws(() => inchesToMetric(-1)) | ||
}) | ||
|
||
test('should throw error if wrong metric value is passed', t => { | ||
t.throws(() => inchesToMetric(12, 'km')) | ||
}) | ||
|
||
test('should convert inches to centimeters', t => { | ||
t.deepEqual(inchesToMetric(1), 2.54) | ||
t.deepEqual(inchesToMetric(10), 25.4) | ||
}) | ||
|
||
test('should convert inches to meters', t => { | ||
t.deepEqual(inchesToMetric(1, 'm'), 0.0254) | ||
t.deepEqual(inchesToMetric(10, 'm'), 0.254) | ||
}) | ||
|
||
test('should convert inches to milimeters', t => { | ||
t.deepEqual(inchesToMetric(1, 'mm'), 25.4) | ||
t.deepEqual(inchesToMetric(10, 'mm'), 254) | ||
}) |