boxShadow.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const VALUES_REG = /,(?![^\(]*\))/;
  2. const PARTS_REG = /\s(?![^(]*\))/;
  3. const LENGTH_REG = /^[0-9]+[a-zA-Z%]+?$/;
  4. const parseValue = str => {
  5. const parts = str.split(PARTS_REG);
  6. const inset = parts.includes('inset');
  7. const last = parts.slice(-1)[0];
  8. const color = !isLength(last) ? last : undefined;
  9. const nums = parts
  10. .filter(n => n !== 'inset')
  11. .filter(n => n !== color)
  12. .map(toNum);
  13. const [ offsetX, offsetY, blurRadius, spreadRadius ] = nums;
  14. return {
  15. inset,
  16. offsetX,
  17. offsetY,
  18. blurRadius,
  19. spreadRadius,
  20. color
  21. };
  22. };
  23. const stringifyValue = obj => {
  24. const {
  25. inset,
  26. offsetX = 0,
  27. offsetY = 0,
  28. blurRadius = 0,
  29. spreadRadius,
  30. color
  31. } = obj || {};
  32. return [
  33. (inset ? 'inset' : null),
  34. offsetX,
  35. offsetY,
  36. blurRadius,
  37. spreadRadius,
  38. color
  39. ].filter(v => v !== null && v !== undefined)
  40. .map(toPx)
  41. .map(s => ('' + s).trim())
  42. .join(' ');
  43. };
  44. const isLength = v => v === '0' || LENGTH_REG.test(v);
  45. const toNum = v => {
  46. if (!/px$/.test(v) && v !== '0') return v;
  47. const n = parseFloat(v);
  48. return !isNaN(n) ? n : v;
  49. };
  50. const toPx = n => typeof n === 'number' && n !== 0 ? (n + 'px') : n;
  51. export const parse = str => str.split(VALUES_REG).map(s => s.trim()).map(parseValue);
  52. export const stringify = arr => arr.map(stringifyValue).join(', ');