index.js 1.3 KB

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