Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

Commit

Permalink
feat: add inches to metric conversion (#226)
Browse files Browse the repository at this point in the history
Closes #225
  • Loading branch information
baruchvlz authored and Kent C. Dodds committed Jan 25, 2019
1 parent 3282f0d commit c8dea4b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/inches-to-metric.js
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]
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import timeSince from './timeSince'
import first from './first'
import mode from './mode-array'
import rollDice from './rollDice'
import inchesToMetric from './inches-to-metric'

export {
reverseArrayInPlace,
Expand Down Expand Up @@ -172,4 +173,5 @@ export {
first,
mode,
rollDice,
inchesToMetric,
}
30 changes: 30 additions & 0 deletions test/inches-to-metric.js
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)
})

0 comments on commit c8dea4b

Please sign in to comment.