index.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import parse from 'inline-style-parser';
  2. /**
  3. * Parses inline style to object.
  4. *
  5. * @param style - Inline style.
  6. * @param iterator - Iterator.
  7. * @returns - Style object or null.
  8. *
  9. * @example Parsing inline style to object:
  10. *
  11. * ```js
  12. * import parse from 'style-to-object';
  13. * parse('line-height: 42;'); // { 'line-height': '42' }
  14. * ```
  15. */
  16. function StyleToObject(style, iterator) {
  17. let styleObject = null;
  18. if (!style || typeof style !== 'string') {
  19. return styleObject;
  20. }
  21. const declarations = parse(style);
  22. const hasIterator = typeof iterator === 'function';
  23. declarations.forEach((declaration) => {
  24. if (declaration.type !== 'declaration') {
  25. return;
  26. }
  27. const { property, value } = declaration;
  28. if (hasIterator) {
  29. iterator(property, value, declaration);
  30. }
  31. else if (value) {
  32. styleObject = styleObject || {};
  33. styleObject[property] = value;
  34. }
  35. });
  36. return styleObject;
  37. }
  38. export { StyleToObject as default };
  39. //# sourceMappingURL=index.mjs.map