style-to-object 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
1
  import StyleToObject from '../cjs/index.js';
2
2
 
3
- export default StyleToObject.default;
3
+ // ensure compatibility with rollup umd build
4
+ export default StyleToObject.default || StyleToObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "style-to-object",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Converts inline style to object.",
5
5
  "author": "Mark <mark@remarkablemark.org>",
6
6
  "main": "cjs/index.js",
@@ -52,14 +52,14 @@
52
52
  "@rollup/plugin-node-resolve": "15.2.3",
53
53
  "@rollup/plugin-terser": "0.4.4",
54
54
  "@rollup/plugin-typescript": "11.1.5",
55
- "@types/jest": "29.5.5",
56
- "@typescript-eslint/eslint-plugin": "6.7.5",
57
- "@typescript-eslint/parser": "6.7.5",
55
+ "@types/jest": "29.5.6",
56
+ "@typescript-eslint/eslint-plugin": "6.8.0",
57
+ "@typescript-eslint/parser": "6.8.0",
58
58
  "eslint": "8.51.0",
59
59
  "eslint-plugin-prettier": "5.0.1",
60
60
  "husky": "8.0.3",
61
61
  "jest": "29.7.0",
62
- "lint-staged": "15.0.1",
62
+ "lint-staged": "15.0.2",
63
63
  "npm-run-all": "4.1.5",
64
64
  "pinst": "3.0.0",
65
65
  "prettier": "3.0.3",
@@ -70,7 +70,8 @@
70
70
  "files": [
71
71
  "/cjs",
72
72
  "/dist",
73
- "/esm"
73
+ "/esm",
74
+ "/src"
74
75
  ],
75
76
  "license": "MIT"
76
77
  }
package/src/index.ts ADDED
@@ -0,0 +1,59 @@
1
+ import parse from 'inline-style-parser';
2
+ import type { Declaration } from 'inline-style-parser';
3
+
4
+ export { Declaration };
5
+
6
+ interface StyleObject {
7
+ [name: string]: string;
8
+ }
9
+
10
+ type Iterator = (
11
+ property: string,
12
+ value: string,
13
+ declaration: Declaration,
14
+ ) => void;
15
+
16
+ /**
17
+ * Parses inline style to object.
18
+ *
19
+ * @param style - Inline style.
20
+ * @param iterator - Iterator.
21
+ * @returns - Style object or null.
22
+ *
23
+ * @example Parsing inline style to object:
24
+ *
25
+ * ```js
26
+ * import parse from 'style-to-object';
27
+ * parse('line-height: 42;'); // { 'line-height': '42' }
28
+ * ```
29
+ */
30
+ export default function StyleToObject(
31
+ style: string,
32
+ iterator?: Iterator,
33
+ ): StyleObject | null {
34
+ let styleObject: StyleObject | null = null;
35
+
36
+ if (!style || typeof style !== 'string') {
37
+ return styleObject;
38
+ }
39
+
40
+ const declarations = parse(style);
41
+ const hasIterator = typeof iterator === 'function';
42
+
43
+ declarations.forEach((declaration) => {
44
+ if (declaration.type !== 'declaration') {
45
+ return;
46
+ }
47
+
48
+ const { property, value } = declaration;
49
+
50
+ if (hasIterator) {
51
+ iterator(property, value, declaration);
52
+ } else if (value) {
53
+ styleObject = styleObject || {};
54
+ styleObject[property] = value;
55
+ }
56
+ });
57
+
58
+ return styleObject;
59
+ }