npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-css

v1.0.0

Published

Parse out simple css rules for classes, id's, and tag names

Downloads

6

Readme

simple-css

Parse out simple css rules for classes, ids, and tag names

This module was made to be used with facebook-layout so it converts relevant properties into numbers, and expands shorthand properties when needed.

Installing

npm install --save simple-css

Example

style.css:

.class-name {
	height: 100px;
	padding: 10px 0px;
}
div {
	background: #FFF;
	margin-left: 10px;
}
#myelement {
	flex-direction: column;
	box-shadow: inset 1px 1px 4px 0px rgba(255, 0, 0, 0.5);
	border-width: 3px;
}
div {
	color: red;
}

index.js:

var fs = require("fs");
var simpleCSS = require("simple-css");


var style_text = fs.readFileSync("style.css", "utf8");

var style_map = Parser.parse(style_text);
console.log(style_map);

output:

{
	".class-name": {
		"height": 100,
		"paddingTop": 10,
		"paddingRight": 0,
		"paddingBottom": 10,
		"paddingLeft": 0
	},
	"div": {
		"background": "FFF",
		"marginLeft": 10,
		"color": "red"
	},
	"#myelement": {
		"flexDirection": "column",
		"boxShadow": "inset 1px 1px 4px 0px rgba(255, 0, 0, 0.5)",
		"borderWidth": "3px",
		"borderTopwidth": 3,
		"borderRightwidth": 3,
		"borderBottomwidth": 3,
		"borderLeftwidth": 3
	}
}

How it works

The library parses out rules from a stylesheet, and considers the entire query part as one thing. The point is to limit your rules to only apply on simple selectors that just match a single class, id, or tag.

If there are rules that have the same query, they are merged together, with the properties in the later rule overwriting the earlier one, just like it does in the browser.

The properties for each rule are then turned into an object that's similar to Element.style in that the property is converted to camelCase. The actual values, however usually remain unchanged.

Since this is meant to work with the css-layout module, certain properties are parsed to make it easier for that library to consume them.

Specifically: margin, padding, and border-width are converted into the longer "marginTop, marginLeft, etc" form.

width, height, flex, top, bottom, left, right, and the previously mentioned properties are converted to JavaScript numbers. Be careful because px, %, and em are converted to the same number.

Warning

The parser isn't very smart, so try not to feed it malformed CSS.