atomatic
Version:
An easy to use build and development tool for Atomic Design Systems, that works with rollup.js, Browserify, webpack and many more...
34 lines (26 loc) • 696 B
JavaScript
const
maths = require('mathjs');
class MathJs {
static resolve(argString) {
let unit = '';
const
expr = /(-?(0\.|[1-9\.])+[0-9\.]*(px|em|ex|ch|rem|pt|vh|vw|vmin|vmax)?( |\+|-|\*|\/)*)+/g,
matches = argString.match(expr);
if (matches !== null) {
matches.map((match) => {
const
number = match.replace(/([a-zA-Z]+)/g, ''),
[unit] = match.match(/([a-zA-Z])+/) || [''];
try {
const resolved = maths.eval(number);
argString = argString.replace(match, resolved+unit);
}
catch(err) {
console.log(err);
}
});
}
return argString;
}
}
module.exports = MathJs;