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

to-css

v1.2.1

Published

Generate CSS from a JavaScript Object

Downloads

6,576

Readme

to-css

Build status NPM version js-xo-style

Generate CSS from a JavaScript Object

This module does not convert property names to their dasherized counterparts, this is just a plain object to CSS stringification, though see property and value options below.

Installation

Install to-css using npm:

npm install --save to-css

Usage

Module usage

var toCss = require('to-css');

toCss({body: {'font-size': '10px'}}, {indent: '  '});
/**
 * body {
 *   font-size: 10px;
 * }
 */

Array values - When you want to set a property multiple times

Sometimes you want to have a CSS declaration with the same property specified multiple times with different values, for fallback values. You can use arrays for that, e.g:

var toCss = require('to-css');

toCss({body: {color: ['rgba(0,0,0,.5)', 'black']}}, {indent: '  '});
/**
 * body {
 *   color: rgba(0,0,0,.5);
 *   color: black;
 * }
 */

Array declarations - E.g. for @font-face

Defining multiple @font-face's can be done using arrays like this:

var toCss = require('to-css');

toCss({
	'@font-face': [
		{'font-family': '"MyWebFont"', src: 'url("myfont.woff2") format("woff2"), url("myfont.woff") format("woff")'},
		{'font-family': 'MyOtherFont', src: 'url("otherfont.woff2") format("woff2"), url("otherfont.woff") format("woff")'}
	]
});
/**
 * @font-face {
 *   font-family: "MyWebFont";
 *   src: url("myfont.woff2") format("woff2"), url("myfont.woff") format("woff");
 * }
 * @font-face {
 *   font-family: "MyOtherFont";
 *   src: url("otherfont.woff2") format("woff2"), url("otherfont.woff") format("woff");
 * }
 */

Or like this:

var toCss = require('to-css');

toCss([
	{
		'@font-face': {
			'font-family': '"MyWebFont"',
			src: 'url("myfont.woff2") format("woff2"), url("myfont.woff") format("woff")'
		}
	},
	{
		'@font-face': {
			'font-family': 'MyOtherFont',
			src: 'url("otherfont.woff2") format("woff2"), url("otherfont.woff") format("woff")'
		}
	}
]);
/**
 * @font-face {
 *   font-family: "MyWebFont";
 *   src: url("myfont.woff2") format("woff2"), url("myfont.woff") format("woff");
 * }
 * @font-face {
 *   font-family: "MyOtherFont";
 *   src: url("otherfont.woff2") format("woff2"), url("otherfont.woff") format("woff");
 * }
 */

API

toCss(object [, options])

| Name | Type | Description | |------|------|-------------| | object | Object|Array | Object or array to generate a CSS string from | | options | Object | Options |

Returns: String, the generated CSS string.

options.indent

Type: String|Number
Default: ""

Works like JSON.stringify's space parameter. I.e. if it's a number it indicates the number of spaces to use as white space, and if it's a string the string itself is used as white space. When empty (or NULL) no white space is used.

options.value

Type: Function
Default: NOOP

A transform function for property values, gets called for each CSS rule with value and property as params: options.value(value, property). Can return a String or an array of strings!

options.property

Type: Function
Default: NOOP

A transform function for property names, gets called for each CSS rule with property and value as params: options.property(property, value). Can return a String or an array of strings!

options.selector

Type: Function
Default: NOOP

A transform function for selectors, gets called for each CSS declaration with selector and declaration object as params: options.selector(selector, declaration). Can return a String or an array of strings!

License

MIT © Joakim Carlstein