style-to-object
Parses inline style to object:
var parse = ;;
Output:
color: '#C0FFEE' background: '#BADA55'
Installation
NPM:
$ npm install style-to-object --save
Yarn:
$ yarn add style-to-object
CDN:
Usage
Import the module:
// CommonJSconst parse = ; // ES Modules;
Parse single declaration:
;
Output:
'line-height': '42'
Parse multiple declarations:
;
Output:
'border-color': '#ACE' 'z-index': '1337'
Parse unknown declarations:
;
Output:
'answer': '42'
Invalid declarations/arguments:
; // { right: '1em' } ; // null; // null; // null; // null; // null; // null; // null; // null ; // throws Error; // throws Error
Iterator
If the 2nd argument is a function, then the parser will return null
:
; // null
But the function will iterate through each declaration:
;
This makes it easy to customize the output:
const style = ` color: red; background: blue;`;const output = ; { output;} ;console; // [['color', 'red'], ['background', 'blue']]
Testing
Run tests:
$ npm test
Run tests in watch mode:
$ npm run test:watch
Run tests with coverage:
$ npm run test:coverage # generate html report $ npm run test:coverage:report
Lint files:
$ npm run lint
Fix lint errors:
$ npm run lint:fix
Test TypeScript declaration file for style and correctness:
$ npm run lint:dts
Release
Only collaborators with credentials can release and publish:
$ npm run release$ git push --follow-tags && npm publish