UNPKG

5.03 kBMarkdownView Raw
1# @ellentorg/ad-dolor-atque
2
3[![NPM](https://nodei.co/npm/@ellentorg/ad-dolor-atque.png)](https://nodei.co/npm/@ellentorg/ad-dolor-atque/)
4
5[![NPM version](https://badgen.net/npm/v/@ellentorg/ad-dolor-atque)](https://www.npmjs.com/package/@ellentorg/ad-dolor-atque)
6[![Bundlephobia minified + gzip](https://badgen.net/bundlephobia/minzip/@ellentorg/ad-dolor-atque)](https://bundlephobia.com/package/@ellentorg/ad-dolor-atque)
7[![build](https://github.com/ellentorg/ad-dolor-atque/actions/workflows/build.yml/badge.svg)](https://github.com/ellentorg/ad-dolor-atque/actions/workflows/build.yml)
8[![codecov](https://codecov.io/gh/remarkablemark/@ellentorg/ad-dolor-atque/branch/master/graph/badge.svg?token=JWKUKTFT3E)](https://codecov.io/gh/remarkablemark/@ellentorg/ad-dolor-atque)
9[![NPM downloads](https://badgen.net/npm/dm/@ellentorg/ad-dolor-atque)](https://www.npmjs.com/package/@ellentorg/ad-dolor-atque)
10
11Parses CSS inline style to JavaScript object (camelCased):
12
13```
14StyleToJS(string)
15```
16
17## Example
18
19```js
20import parse from '@ellentorg/ad-dolor-atque';
21
22parse('background-color: #BADA55;');
23```
24
25Output:
26
27```json
28{ "backgroundColor": "#BADA55" }
29```
30
31[Replit](https://replit.com/@remarkablemark/@ellentorg/ad-dolor-atque) | [JSFiddle](https://jsfiddle.net/remarkablemark/04nob1y7/) | [Examples](https://github.com/ellentorg/ad-dolor-atque/tree/master/examples)
32
33## Install
34
35[NPM](https://www.npmjs.com/package/@ellentorg/ad-dolor-atque):
36
37```sh
38npm install @ellentorg/ad-dolor-atque --save
39```
40
41[Yarn](https://yarnpkg.com/package/@ellentorg/ad-dolor-atque):
42
43```sh
44yarn add @ellentorg/ad-dolor-atque
45```
46
47[CDN](https://unpkg.com/@ellentorg/ad-dolor-atque/):
48
49```html
50<script src="https://unpkg.com/@ellentorg/ad-dolor-atque@latest/umd/@ellentorg/ad-dolor-atque.min.js"></script>
51<script>
52 window.StyleToJS(/* string */);
53</script>
54```
55
56## Usage
57
58### Import
59
60Import with ES Modules:
61
62```js
63import parse from '@ellentorg/ad-dolor-atque';
64```
65
66Require with CommonJS:
67
68```js
69const parse = require('@ellentorg/ad-dolor-atque');
70```
71
72### Parse style
73
74Parse single declaration:
75
76```js
77parse('line-height: 42');
78```
79
80Output:
81
82```json
83{ "lineHeight": "42" }
84```
85
86> Notice that the CSS property is camelCased.
87
88Parse multiple declarations:
89
90```js
91parse(`
92 border-color: #ACE;
93 z-index: 1337;
94`);
95```
96
97Output:
98
99```json
100{
101 "borderColor": "#ACE",
102 "zIndex": "1337"
103}
104```
105
106### Vendor prefix
107
108Parse [vendor prefix](https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix):
109
110```js
111parse(`
112 -webkit-transition: all 4s ease;
113 -moz-transition: all 4s ease;
114 -ms-transition: all 4s ease;
115 -o-transition: all 4s ease;
116 -khtml-transition: all 4s ease;
117`);
118```
119
120Output:
121
122```json
123{
124 "webkitTransition": "all 4s ease",
125 "mozTransition": "all 4s ease",
126 "msTransition": "all 4s ease",
127 "oTransition": "all 4s ease",
128 "khtmlTransition": "all 4s ease"
129}
130```
131
132### Custom property
133
134Parse [custom property](https://developer.mozilla.org/en-US/docs/Web/CSS/--*):
135
136```js
137parse('--custom-property: #f00');
138```
139
140Output:
141
142```json
143{ "--custom-property": "#f00" }
144```
145
146### Unknown declaration
147
148This library does not validate declarations, so unknown declarations can be parsed:
149
150```js
151parse('the-answer: 42;');
152```
153
154Output:
155
156```json
157{ "theAnswer": "42" }
158```
159
160### Invalid declaration
161
162Declarations with missing value are removed:
163
164```js
165parse(`
166 margin-top: ;
167 margin-right: 1em;
168`);
169```
170
171Output:
172
173```json
174{ "marginRight": "1em" }
175```
176
177Other invalid declarations or arguments:
178
179```js
180parse(); // {}
181parse(null); // {}
182parse(1); // {}
183parse(true); // {}
184parse('top:'); // {}
185parse(':12px'); // {}
186parse(':'); // {}
187parse(';'); // {}
188```
189
190The following values will throw an error:
191
192```js
193parse('top'); // Uncaught Error: property missing ':'
194parse('/*'); // Uncaught Error: End of comment missing
195```
196
197### Options
198
199#### reactCompat
200
201When option `reactCompat` is true, the [vendor prefix](https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix) will be capitalized:
202
203```js
204parse(
205 `
206 -webkit-transition: all 4s ease;
207 -moz-transition: all 4s ease;
208 -ms-transition: all 4s ease;
209 -o-transition: all 4s ease;
210 -khtml-transition: all 4s ease;
211 `,
212 { reactCompat: true },
213);
214```
215
216Output:
217
218```json
219{
220 "WebkitTransition": "all 4s ease",
221 "MozTransition": "all 4s ease",
222 "msTransition": "all 4s ease",
223 "OTransition": "all 4s ease",
224 "KhtmlTransition": "all 4s ease"
225}
226```
227
228This removes the React warning:
229
230```
231Warning: Unsupported vendor-prefixed style property %s. Did you mean %s?%s", "oTransition", "OTransition"
232```
233
234## Testing
235
236Run tests with coverage:
237
238```sh
239npm test
240```
241
242Run tests in watch mode:
243
244```sh
245npm run test:watch
246```
247
248Lint files:
249
250```sh
251npm run lint
252```
253
254Fix lint errors:
255
256```sh
257npm run lint:fix
258```
259
260## Release
261
262Release and publish are automated by [Release Please](https://github.com/googleapis/release-please).
263
264## Special Thanks
265
266- [style-to-object](https://github.com/remarkablemark/style-to-object)
267
268## License
269
270[MIT](https://github.com/ellentorg/ad-dolor-atque/blob/master/LICENSE)