-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (52 loc) · 1.54 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
"use strict";
module.exports = function (tasks) {
tasks.addTask({
filter: {
type: 'Function',
name: 'calc'
},
fn: function (fn) {
resolve(fn);
}
});
tasks.addTask({
filter: 'Expression',
fn: function (expression) {
resolve(expression);
}
});
function resolve (element) {
var mainUnit, units = element.getAll('Unit');
//Check the unit used in this function
units.forEach(function (unit) {
if (mainUnit === false) {
return;
}
if (mainUnit === undefined) {
mainUnit = unit.name;
} else if (mainUnit !== unit.name) {
mainUnit = false;
}
});
//There are more than one unit
if (mainUnit === false) {
return;
}
//Replace all units by its numbers
units.forEach(function (unit) {
var number = unit.get('Number');
if (unit.name === '%') {
number.name *= 0.01;
}
unit.replaceWith(unit.get('Number'));
});
var number = eval(element.join(' '));
if (mainUnit === undefined) {
element.replaceWithCode(number.toFixed(2), 'Number');
} else if (mainUnit === '%') {
element.replaceWithCode((number *= 100).toFixed(2) + '%', 'Unit');
} else {
element.replaceWithCode(number.toFixed(2) + mainUnit, 'Unit');
}
}
};