UNPKG

em-media-query

Version:

Transform min/max-width/height media queries to ems.

56 lines (45 loc) 1.25 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var parse = _interopDefault(require('postcss-value-parser')); var round = _interopDefault(require('lodash.round')); /** * @param {string} string * @param {object} options * * @returns {string} */ var index = (string, options) => { options = { precision: 5, ...options }; const tree = parse(string); tree.walk((node) => { if (node.type === 'function') { const values = node.nodes; const minMax = values.some((item) => { return /(?:min|max)-(?:width|height)/.test(item.value); }); // If we are working with min/max-width/height query if (minMax) { values // Work only with pixel values .filter((item) => { const value = parse.unit(item.value); return ( item.type === 'word' && value && value.unit === 'px' ); }) // Convert to ems .map((item) => { const value = parse.unit(item.value); item.value = [ round(Number(value.number) / 16, options.precision), 'em' ].join(''); return item; }); } } }); return tree.toString(); }; module.exports = index; //# sourceMappingURL=index.cjs.js.map