-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (54 loc) · 990 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module.exports = meters
exports.footinch = footinch
function meters (str) {
if ('number' == typeof str) return str
str = str
.replace(/[,\s]/g, '')
.toLowerCase()
str = footinch(str)
var unit = str.replace(/[0-9\.]+/,'')
var m = parseFloat(str)
switch (unit) {
case 'mi':
case 'mile':
case 'miles':
return m * 1609.34
case 'nm':
case 'nmi':
return m * 1852
case '\'':
case 'ft':
case 'foot':
case 'feet':
return m * 0.3048
case '"':
case 'in':
case 'inch':
case 'inches':
return m * 0.0254
case 'yd':
case 'yds':
case 'yard':
case 'yards':
return m * 0.9144
case 'k':
case 'km':
return m * 1000
case 'cm':
return m / 100
case 'mm':
return m / 1000
case 'm':
case '':
return m
default:
return null
}
}
function footinch (str) {
var arr = str.match(/^([0-9]+)['′][-–]?([0-9]+)["″]?$/)
if (arr && arr.length)
return +arr[1]+arr[2]/12+'ft'
else
return str
}