-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunits.js
214 lines (188 loc) · 5.17 KB
/
units.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict";
let config = require("./config");
let systems = config.systems;
let math = require("mathjs");
const METRIC = "metric";
const IMPERIAL = "imperial";
const SYSTEMS = [METRIC, IMPERIAL];
function coolRound(number, to) {
return math.round(number / to, 1) * to;
}
function getUnitSettings(u) {
var ts = systems[METRIC].filter(unit => unit.code === u || unit.long.singular === u || unit.long.plural === u);
if (ts.length == 1) {
return ts[0];
} else {
ts = systems[IMPERIAL].filter(unit => unit.code === u || unit.long.singular === u || unit.long.plural === u);
if (ts.length > 0) {
return ts[0];
}
}
return null;
}
function getUnitSystem(u) {
// TODO so dirty
if (systems[METRIC].filter(unit => unit.code === u || unit.long.singular === u || unit.long.plural === u).length > 0) {
return METRIC;
}
if (systems[IMPERIAL].filter(unit => unit.code === u || unit.long.singular === u || unit.long.plural === u).length > 0) {
return IMPERIAL;
}
return null;
}
function getMajorityUnitSystem(units) {
var metric = 0,
imperial = 0;
units.forEach(unit => {
var system = getUnitSystem(unit);
if (system === METRIC) {
metric ++;
} else if (system === IMPERIAL) {
imperial ++;
}
});
if (metric > imperial) {
return METRIC;
} else if (imperial > metric) {
return IMPERIAL;
}
}
function getConversions(fromUnit) {
//var math2 = require("mathjs");
var ret = [];
const replacePass = config.replacePass;
for (var from in replacePass) {
if (replacePass.hasOwnProperty(from)) {
var to = replacePass[from];
fromUnit = fromUnit.replace(new RegExp(from, 'g'), to);
}
}
try {
var parse = math.parse(fromUnit);
if (parse.fn === "to") {
ret.push(math.eval(fromUnit).format());
return ret;
}
} catch(e) {
return ret;
}
var intermediate = false;
try {
math.unit(fromUnit);
} catch(e) {
intermediate = true;
}
try {
fromUnit = math.eval(fromUnit);
} catch (e) {
return ret;
}
// result is scalar without unit
if (!fromUnit.units) {
ret.push(fromUnit);
return ret;
}
var fromSystem = getMajorityUnitSystem(fromUnit.units.map(unit => unit.unit.name));
if (!fromSystem) {
ret.push(fromUnit);
return ret;
}
var toSystem;
if (intermediate) {
ret = ret.concat(getFittingConversions(fromUnit, fromSystem));
}
if (fromSystem === METRIC) {
toSystem = IMPERIAL;
} else if (fromSystem === IMPERIAL) {
toSystem = METRIC;
} else {
return [];
}
ret = ret.concat(getFittingConversions(fromUnit, toSystem));
return ret;
}
function getFittingConversions(fromUnit, toSystem) {
if (!toSystem) {
return fromUnit;
}
var ret = [];
systems[toSystem].forEach(unit => {
try {
if (unit.hidden) {
return;
}
var value = fromUnit.toNumber(unit.code);
if (unit.range) {
var lower = unit.range.from || null;
var upper = unit.range.to || null;
let cmp = math.abs(value);
if (lower && lower > cmp) {
return;
}
if (upper && upper < cmp) {
return;
}
}
//ret.push(value + " " + unit.name);
ret.push(format(value, {name: unit.code}, unit));
} catch(e) {
}
});
return ret;
}
function formatNumber(number, maxDigits) {
var str = number + '';
var dotIndex = str.indexOf('.');
if (str.length > (dotIndex + 1 + maxDigits)) {
str = str.substr(0, dotIndex + 1 + maxDigits);
str = str.replace(/0+$/, '');
return str;
} else {
return str;
}
}
function format(scalar, unit, unitFormat) {
if (!unitFormat && unit) {
unitFormat = getUnitSettings(unit.name);
}
if (unitFormat) {
if (unitFormat.round) {
scalar = formatNumber(coolRound(scalar, unitFormat.round), 5);
}
switch (unitFormat.printAs) {
case "code":
return scalar + " " + unitFormat.code;
case "long":
if (scalar === 1) {
return scalar + " " + unitFormat.long.singular;
} else {
return scalar + " " + unitFormat.long.plural;
}
default:
return scalar + " " + unitFormat.printAs;
}
} else {
return formatNumber(math.round(scalar, 3), 3);
}
}
//var test = [
// "2 cm",
// "2cm * 4cm",
// "1 + 1",
// "(1 m + 2 m) * 5 m",
// "1 inch * 2 inch",
// "2 ° C",
// "10 °F"
//];
//
//test.forEach(expr => {
// const conversions = getConversions(expr);
// if (conversions.length > 0) {
// console.log(expr + ' = ' + conversions.join(" = "));
// }
//});
//
//process.exit();
module.exports = {
getConversions: getConversions
};