_pxto.js.flow 908 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // @flow
  2. import endsWith from './_endsWith'
  3. import stripUnit from '../helpers/stripUnit'
  4. import PolishedError from './_errors'
  5. /**
  6. * Factory function that creates pixel-to-x converters
  7. * @private
  8. */
  9. const pxtoFactory = (to: string) => (
  10. pxval: string | number,
  11. base?: string | number = '16px',
  12. ): string => {
  13. let newPxval = pxval
  14. let newBase = base
  15. if (typeof pxval === 'string') {
  16. if (!endsWith(pxval, 'px')) {
  17. throw new PolishedError(69, to, pxval)
  18. }
  19. newPxval = stripUnit(pxval)
  20. }
  21. if (typeof base === 'string') {
  22. if (!endsWith(base, 'px')) {
  23. throw new PolishedError(70, to, base)
  24. }
  25. newBase = stripUnit(base)
  26. }
  27. if (typeof newPxval === 'string') {
  28. throw new PolishedError(71, pxval, to)
  29. }
  30. if (typeof newBase === 'string') {
  31. throw new PolishedError(72, base, to)
  32. }
  33. return `${newPxval / newBase}${to}`
  34. }
  35. export default pxtoFactory