-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
169 lines (146 loc) · 5 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
function prettify (value, shouldPrettify) {
if (!shouldPrettify) return value
var rounded = +(Math.round(value + 'e+2') + 'e-2')
var parts = rounded.toString().split('.')
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.')
}
/**
* Utility for checking conversion validity
* Errors if a length measurement is trying to convert to an area measurement
* and vice versa
* @param {string} fromCategoryType the incoming category type i.e. length or area
* @param {string} toCategoryType the outbound category type i.e. length or area
*/
function isValidConversion (fromCategoryType, toCategoryType) {
if (fromCategoryType !== toCategoryType) {
var errorSnippet =
fromCategoryType === 'area'
? 'an area measurement to a length measurement'
: 'a length measurement to an area measurement'
throw new Error(
`Invalid conversion. You are trying to convert ${errorSnippet}.`
)
}
}
var modifiers = {
km: .001,
km2: .000001,
hm: .01,
hm2: .0001,
dam: .1,
dam2: .01,
m: 1,
m2: 1,
dm: 10,
dm2: 100,
cm: 100,
cm2: 10000,
mm: 1000,
mm2: 1000000
}
var types = {
km: { value: 1000, category: 'length' },
km2: { value: 1000000, category: 'area' },
hm: { value: 100, category: 'length' },
hm2: { value: 10000, category: 'area' },
dam: { value: 10, category: 'length' },
dam2: { value: 100, category: 'area' },
m: { value: 1, category: 'length' },
m2: { value: 1, category: 'area' },
dm: { value: .1, category: 'length' },
dm2: { value: .01, category: 'area' },
cm: { value: .01, category: 'length' },
cm2: { value: .0001, category: 'area' },
mm: { value: .001, category: 'length' },
mm2: { value: .000001, category: 'area' },
}
function Pretty (value) {
if (!(this instanceof Pretty)) {
return new Pretty(value)
}
this.value = value === 0 ? 0 : value || 1
this.type = types.m
}
Pretty.prototype.input = function (type) {
if (!type in types) {
throw new Error('type ' + type + ' is not valid')
}
this.type = types[type]
return this
}
Pretty.prototype.km = function () {
isValidConversion(this.type.category, 'length')
return prettify((this.value * this.type.value) * modifiers.km, this.prettify) + 'km'
}
Pretty.prototype.km2 = function () {
isValidConversion(this.type.category, 'area')
return prettify((this.value * this.type.value) * modifiers.km2, this.prettify) + 'km2'
}
Pretty.prototype.hm = function () {
isValidConversion(this.type.category, 'length')
return prettify((this.value * this.type.value) * modifiers.hm, this.prettify) + 'hm'
}
Pretty.prototype.hm2 = function () {
isValidConversion(this.type.category, 'area')
return prettify((this.value * this.type.value) * modifiers.hm2, this.prettify) + 'hm2'
}
Pretty.prototype.dam = function () {
isValidConversion(this.type.category, 'length')
return prettify((this.value * this.type.value) * modifiers.dam, this.prettify) + 'dam'
}
Pretty.prototype.dam2 = function () {
isValidConversion(this.type.category, 'area')
return prettify((this.value * this.type.value) * modifiers.dam2, this.prettify) + 'dam2'
}
Pretty.prototype.m = function () {
isValidConversion(this.type.category, 'length')
return prettify((this.value * this.type.value) * modifiers.m, this.prettify) + 'm'
}
Pretty.prototype.m2 = function () {
isValidConversion(this.type.category, 'area')
return prettify((this.value * this.type.value) * modifiers.m2, this.prettify) + 'm2'
}
Pretty.prototype.dm = function () {
isValidConversion(this.type.category, 'length')
return prettify((this.value * this.type.value) * modifiers.dm, this.prettify) + 'dm'
}
Pretty.prototype.dm2 = function () {
isValidConversion(this.type.category, 'area')
return prettify((this.value * this.type.value) * modifiers.dm2, this.prettify) + 'dm2'
}
Pretty.prototype.cm = function () {
isValidConversion(this.type.category, 'length')
return prettify((this.value * this.type.value) * modifiers.cm, this.prettify) + 'cm'
}
Pretty.prototype.cm2 = function () {
isValidConversion(this.type.category, 'area')
return prettify((this.value * this.type.value) * modifiers.cm2, this.prettify) + 'cm2'
}
Pretty.prototype.mm = function () {
isValidConversion(this.type.category, 'length')
return prettify((this.value * this.type.value) * modifiers.mm, this.prettify) + 'mm'
}
Pretty.prototype.mm2 = function () {
isValidConversion(this.type.category, 'area')
return prettify((this.value * this.type.value) * modifiers.mm2, this.prettify) + 'mm2'
}
Pretty.prototype.humanize = function () {
var value = this.type.value * this.value
var category = this.type.category
this.prettify = true
if (category === 'area') {
if (value >= 1000000) return this.km2()
if (value >= 1) return this.m2()
if (value >= .5) return this.m2()
if (value >= .1) return this.cm2()
return this.mm2()
}
if (value >= 1000) return this.km()
if (value >= 1) return this.m()
if (value >= .5) return this.m()
if (value >= .1) return this.cm()
return this.mm()
}
module.exports = Pretty