Skip to main content

Command Palette

Search for a command to run...

Today's npm package: style-to-js

Published
โ€ข1 min read
Today's npm package: style-to-js
P

I am developer from India currently based in Berlin, Germany.

I am very interested in Design, though I don't design ๐Ÿ˜‰ I take care of it while making my apps.

Photography, videography and having random conversation over beer are my activities of spare time. Timelapses and hyperlapses make me wonder all the time ๐Ÿ˜Š

Today's npm package is style-to-js

If you are working with a UI project where you are accepting CSS styles from users to be applied at a later time.

It will be easier & better to save them as JS Object as it can help in removing the rules duplication.

Lets consider the following block of CSS rules:

border: 1px solid #ccc;
border-bottom-color: #aaa;
color: #555;
font-size: 16px;
margin: 0 0.25rem;

And with the use of today's package; we can convert above CSS rules' block to JS object as:

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

const styles = `
  border: 1px solid #ccc;
  border-bottom-color: #aaa;
  color: #555;
  font-size: 16px;
  margin: 0 0.25rem;
`;

parse(styles);

/*
{
  border: "1px solid #ccc",
  borderBottomColor: "#aaa",
  color: "#555",
  fontSize: "16px",
  margin: "0 0.25rem"
}
*/

You can see it in action here: https://runkit.com/pankaj/style-to-js

Stay tuned for tomorrow's package of the Day.

More from this blog

P

Pankaj Patel - @heypankaj_

63 posts

Sharing my learnings and thoughts on Blogging, Personal Branding and Programming