css-math
Version:
Simple library for performing math operations on CSS properties
33 lines (27 loc) • 866 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _parser = require('./parser');
var _parser2 = _interopRequireDefault(_parser);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Calculate the fraction of the value
*
* fraction('1/2', '100%') = 50%
* fraction('2/3', '600px') = 400px
* fraction('25%', '800px') = 200px
*
* @param fraction
* @param value
* @return string
*/
exports.default = function (fraction, value) {
if (String(fraction).search('%') !== -1) {
// Parse fractions that are percentages
var found = (0, _parser.findAndReplaceUnits)(fraction);
return (0, _parser2.default)(value + ' * (' + found.value / 100 + ')');
}
// parse normal string fractions like 1/5 etc
return (0, _parser2.default)(value + ' * (' + fraction + ')');
};