-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (39 loc) · 1.06 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
/**
* Module dependencies.
*/
var visit = require('rework-visit');
module.exports = function() {
function substitute(decl) {
// grab math(...) value
var math = decl.value.split('math(')[1];
// find the closing bracket
math = math.slice(0, math.lastIndexOf(')'));
// get the trailing unit or just add nothing
var unit = (math.match(/%|in|cm|mm|em|ex|pt|pc|px/g, '')) ? math.match(/%|in|cm|mm|em|ex|pt|pc|px/g, '')[0] : '';
math = math.replace(/%|in|cm|mm|em|ex|pt|pc|px/g, '');
// do some ev[i|a]l
var sum = eval(math);
if (sum) {
return sum + unit;
} else {
// log the error
var error = {
"declaration": decl,
"preEval": math,
"postEval": sum,
"unit": unit
};
throw new Error('error');
}
}
return function math(style) {
visit(style, function(declarations, node) {
declarations.forEach(function(decl) {
if (!decl.value || !decl.value.match(/\bmath\(/)) {
return;
}
decl.value = substitute(decl);
});
});
};
};