1.0.6 • Published 29 days ago

style-to-object v1.0.6

Weekly downloads
2,214,469
License
MIT
Repository
github
Last release
29 days ago

style-to-object

NPM

NPM version Bundlephobia minified + gzip build codecov NPM downloads

Parse CSS inline style to JavaScript object:

import parse from 'style-to-object';

parse('color: #C0FFEE; background: #BADA55;');

Output:

{ color: '#C0FFEE', background: '#BADA55' }

JSFiddle | Replit | Examples

Installation

NPM:

npm install style-to-object --save

Yarn:

yarn add style-to-object

CDN:

<script src="https://unpkg.com/style-to-object@latest/dist/style-to-object.min.js"></script>
<script>
  window.StyleToObject(/* string */);
</script>

Usage

Import with ES Modules:

import parse from 'style-to-object';

Require with CommonJS:

const parse = require('style-to-object').default;

Parse single declaration:

parse('line-height: 42');

Output:

{ 'line-height': '42' }

Parse multiple declarations:

parse(`
  border-color: #ACE;
  z-index: 1337;
`);

Output:

{ 'border-color': '#ACE', 'z-index': '1337' }

Parse unknown declarations:

parse('answer: 42;');

Output:

{ 'answer': '42' }

Invalid declarations/arguments:

parse(`
  top: ;
  right: 1em;
`); // { right: '1em' }

parse();        // null
parse(null);    // null
parse(1);       // null
parse(true);    // null
parse('top:');  // null
parse(':12px'); // null
parse(':');     // null
parse(';');     // null

parse('top'); // throws Error
parse('/*');  // throws Error

Iterator

If the 2nd argument is a function, then the parser will return null:

parse('color: #f00', () => {}); // null

But the function will iterate through each declaration:

parse('color: #f00', (name, value, declaration) => {
  console.log(name);        // 'color'
  console.log(value);       // '#f00'
  console.log(declaration); // { type: 'declaration', property: 'color', value: '#f00' }
});

This makes it easy to customize the output:

const style = `
  color: red;
  background: blue;
`;
const output = [];

function iterator(name, value) {
  output.push([name, value]);
}

parse(style, iterator);
console.log(output); // [['color', 'red'], ['background', 'blue']]

Migration

v1

Migrated to TypeScript. Iterator excludes Comment. CommonJS requires the .default key:

const parse = require('style-to-object').default;

Release

Release and publish are automated by Release Please.

Special Thanks

License

MIT

@daimond113/solid-markdown@saaspe/componentsgatsby@everything-registry/sub-chunk-2840@yurijs/template-loader@cquiroz/react-markdown@concord-consortium/slate-editorbabel-plugin-html-attributes-to-jsxcommoditenetur@infinitebrahmanuniverse/nolb-style-@halkeye/gatsby-rehype-autolink-headers@halkeye/jenkins-io-react@breakaway/documentation-framework@breadio/markdown@breakaway/theme-patternfly-org@furmeet/solid-markdowndesign-system-fitbank-450@marp-team/marp-reactgatsby-recipes@newageerp/v3.utils.markdownhast-util-to-estreehast-util-to-jsx-runtime@kunet/solid-markdown-but-updated@lego-js/tests@jsx-email/doiuse-emailfn-lib-exampledaodao-theme-test-patternfly-orggatsby-plugin-mdx-v1@commercetools-uikit/rich-text-utils@baifendian/adhere-ui-formdesign@draftbox-co/html-to-compiled-mdxeasy-jsx-parser@lskjs/markdowndoiuse-email@marduke182/prosemirror-react-view@hbquick/hq-player-live-camera-ptz@htmlbricks/hb-layout@htmlbricks/hb-layout-desktop@htmlbricks/hb-layout-mobile@htmlbricks/hb-animated-container@htmlbricks/hb-form-composer@htmlbricks/hb-faq-component@htmlbricks/hb-dashboard-counter-lines@htmlbricks/hb-paragraps-around-image@htmlbricks/hb-player-input-streaming@htmlbricks/hb-pad-joystick@htmlbricks/hb-offcanvas@htmlbricks/hb-pad-joistick@htmlbricks/hb-matrix-video@htmlbricks/hb-messages-box@htmlbricks/hb-messages-list@htmlbricks/hb-messages-send@htmlbricks/hb-messages-topics-card@htmlbricks/hb-sidebar-cards-navigator@htmlbricks/hb-sidebar-desktop@htmlbricks/hb-player-live-camera-ptz@htmlbricks/hb-site-contacts-row@htmlbricks/hb-site-paragraph-with-image@htmlbricks/hb-site-slideshow@htmlbricks/hb-site-slideshow-horizontal@htmlbricks/hb-skeleton-component@htmlbricks/hb-stylus-notebook@htmlbricktest/skel-skeleton-component@htmlbricks/hb-stylus-paper@htmlbricks/hb-terms-doc-templates@htmlbricks/hb-vertical-img-txt-archiveeslint-plugin-solidoce-editor-tools-pkpf-docsparvan_componentsparvan_reactjs_componentsmarkdown-vueposthtml-componenttheme-patternfly-orgtldrawligninstyle-to-jssvelte-component-libreact-markdown-v8.0.3react-markdown-customkeypropsequelcomponentsolid-markdownsheldons-componentssheldons-componetnssimplified-jsx-to-astsimplified-jsx-to-jsonreiconifyreact-dom-rendererreact-form-component-libraryreact-html-i18nveniamdebitiszzzxxxyyy321123vue-inline-styledwhiteboard-toolbar@beeswax/react-markdown-async7baggers-statement-parser@wii/slate@saasquatch/stencil-html-parser@saucerjs/css-editor@thinkeloquent/rd2020-designer-app-html-editor@vitali_shcherbina/styled-lib
1.0.6

29 days ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago

1.0.5

5 months ago

1.0.4

6 months ago

1.0.3

6 months ago

0.4.4

6 months ago

0.4.3

6 months ago

0.4.2

9 months ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.0

4 years ago

0.2.3

5 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago