1 | const
|
2 | maths = require('mathjs');
|
3 |
|
4 | class MathJs {
|
5 |
|
6 | static resolve(argString) {
|
7 |
|
8 | let unit = '';
|
9 | const
|
10 | expr = /(-?(0\.|[1-9\.])+[0-9\.]*(px|em|ex|ch|rem|pt|vh|vw|vmin|vmax)?( |\+|-|\*|\/)*)+/g,
|
11 | matches = argString.match(expr);
|
12 |
|
13 | if (matches !== null) {
|
14 | matches.map((match) => {
|
15 |
|
16 | const
|
17 | number = match.replace(/([a-zA-Z]+)/g, ''),
|
18 | [unit] = match.match(/([a-zA-Z])+/) || [''];
|
19 |
|
20 | try {
|
21 | const resolved = maths.eval(number);
|
22 | argString = argString.replace(match, resolved+unit);
|
23 | }
|
24 | catch(err) {
|
25 | console.log(err);
|
26 | }
|
27 | });
|
28 | }
|
29 |
|
30 | return argString;
|
31 | }
|
32 | }
|
33 |
|
34 | module.exports = MathJs; |
\ | No newline at end of file |