1.0.8 • Published 2 months ago

style-to-object v1.0.8

Weekly downloads
2,214,469
License
MIT
Repository
github
Last release
2 months 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/components@everything-registry/sub-chunk-2840svelte-toolbeltvue-inline-styledwhiteboard-toolbarsheldons-componentssheldons-componetnsreiconifysequelcomponentsvelte-component-libveniamdebitiszzzxxxyyy321123hast-util-to-estreehast-util-to-jsx-runtimehtml-dom-attributes-to-react-propshtml-to-compiled-mdxhtml-parser-react-lisonhtml-to-jsx-transformjsxify-htmljarvis-kb-reactglobal_components_assignmentmarkdown-vueparvan_componentsparvan_reactjs_componentsposthtml-componentoce-editor-tools-pkpf-docsmypluginlolreact-dom-rendererreact-form-component-libraryreact-skeleton-builderrainbowprokovanmailreact-markdown-customkeypropreact-markdown-v8.0.3react-html-i18nstyle-to-jssimplified-jsx-to-astsimplified-jsx-to-jsonsolid-markdowntheme-patternfly-orgtldrawlignin@aehrc/smart-forms-renderer@alicloud/gatsby-plugin-mdx-fork@baifendian/adhere-ui-formdesign@beeswax/react-markdown-async@breakaway/theme-patternfly-org@breadio/markdown@breakaway/documentation-framework@careteam/mfe-lowcode-render7baggers-statement-parser@a-m/designer-runtime@cquiroz/react-markdownzerionprokovanmail@halkeye/jenkins-io-react@halkeye/gatsby-rehype-autolink-headers@hbquick/hq-player-live-camera-ptz@fplan/svg-to-geojson@htmlbricks/hb-animated-container@htmlbricks/hb-layout@htmlbricks/hb-layout-desktop@htmlbricks/hb-layout-mobile@htmlbricks/hb-form-composer@htmlbricktest/skel-skeleton-component@htmlbricks/hb-terms-doc-templates@htmlbricks/hb-vertical-img-txt-archive@htmlbricks/hb-skeleton-component@htmlbricks/hb-stylus-notebook@htmlbricks/hb-stylus-paper@htmlbricks/hb-faq-component@htmlbricks/hb-dashboard-counter-lines@htmlbricks/hb-site-contacts-row@htmlbricks/hb-site-paragraph-with-image@htmlbricks/hb-site-slideshow@htmlbricks/hb-site-slideshow-horizontal@htmlbricks/hb-matrix-video@htmlbricks/hb-messages-box@htmlbricks/hb-messages-list@htmlbricks/hb-messages-send@htmlbricks/hb-messages-topics-card@htmlbricks/hb-player-live-camera-ptz@htmlbricks/hb-pad-joistick@htmlbricks/hb-pad-joystick@htmlbricks/hb-offcanvas@htmlbricks/hb-paragraps-around-image@htmlbricks/hb-player-input-streaming@htmlbricks/hb-sidebar-cards-navigator@htmlbricks/hb-sidebar-desktop@furmeet/solid-markdown@infinitebrahmanuniverse/nolb-style-@kunet/solid-markdown-but-updated@localizesh/editor@lskjs/markdown@lego-js/tests@marp-team/marp-react@marduke182/prosemirror-react-view@newageerp/v3.utils.markdown@jenkinsci/gatsby-plugin-jenkins-layout@jsx-email/doiuse-email@kevisual/ui
1.0.8

2 months ago

1.0.7

3 months ago

1.0.6

8 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

1.0.5

12 months ago

1.0.4

1 year ago

1.0.3

1 year ago

0.4.4

1 year ago

0.4.3

1 year ago

0.4.2

1 year ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.2.2

6 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago