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

css-ast-iterations

v0.5.0

Published

Provide a very simple API for complex iterations on the CSS abstract syntax tree (AST).

Downloads

25

Readme

CSS AST iterations

:smile::green_heart::evergreen_tree: Provide a very simple API for complex iterations on the CSS abstract syntax tree (AST).

Build Status Coverage Status Dependency Status devDependency Status npm npm

Table of contents

How to install

$ npm install css --save
$ npm install css-ast-iterations --save

Basic Example

const css = require('css');
const addIterations = require('css-ast-iterations');

// Create the AST
const stylesheet = css.parse('.foo {color: #fff;} .bar { width: 50px;}');

// Add all methods for iterations
addIterations(stylesheet);

// Use a findRules method for find and iterates on all Rules
stylesheet.findRules((rule, ruleIndex) => {
  console.log(rule);
});

CSS AST reference

View the CSS AST Explorer.

CSS AST reference

Methods list

Stylesheet Level - root

Find and iterates on all Rules:

stylesheet.findAllRules((rule, ruleIndex) => {
  console.log(rule);
});

Find and iterates on all Rules (Backwards):

stylesheet.moonWalkAllRules((rule, ruleIndex) => {
  console.log(rule);
});

Find and iterates on all Rules (filter by rule rules):

stylesheet.findAllRulesByType('rule', (rule, ruleIndex) => {
  console.log(rule);
});

Find and iterates on all Rules (filter by import rules):

stylesheet.findAllRulesByType('import', (rule, ruleIndex) => {
  console.log(rule);
});

Find and iterates on all Rules (filter by comment rules):

stylesheet.findAllRulesByType('comment', (rule, ruleIndex) => {
  console.log(rule);
});

Find and iterates on all Rules (filter by selectors):

stylesheet.findAllRulesBySelectors('.foo', (rule, ruleIndex) => {
  console.log(rule);
});

Add a single rule:

stylesheet.addRule(newRule, 0); //Rule position

Remove a single rule:

stylesheet.removeRule(0); //Rule position

Find and iterates on all Selectors:

stylesheet.findAllSelectors((selectors, selectorIndex) => {
  console.log(selectors);
});

Find and iterates on all imports:

stylesheet.findAllImport((url) => {
  console.log(url);
});

Find and iterates on all Declarations:

stylesheet.findAllDeclarations((declaration) => {
    console.log(declaration.property);
    console.log(declaration.value);
});

Find and iterates on all Declarations (filter by selectors):

stylesheet.findAllDeclarationsBySelectors('.foo', (declaration) => {
    console.log(declaration.property);
    console.log(declaration.value);
});

Find and iterates on all Declarations (filter by property):

stylesheet.findAllDeclarationsByProperty('max-width', (declaration) => {
    console.log(declaration.property);
    console.log(declaration.value);
});

Find and iterates on all Declarations (filter by value):

stylesheet.findAllDeclarationsByValue('500px', (declaration) => {
    console.log(declaration.property);
    console.log(declaration.value);
});

Rule Level

Find and iterates on Declarations:

// Stylesheet Level (root)
stylesheet.findAllRulesByType('rule', (rule, ruleIndex) => {

  // Rule Level
  rule.findDeclarations((declaration, declarationIndex) => {
    console.log(declaration);
  });

});

Find and iterates on Declarations (filter by selectors):

// Stylesheet Level (root)
stylesheet.findAllRulesByType('rule', (rule, ruleIndex) => {

  // Rule Level
  rule.findDeclarationsBySelectors('.foo', (declaration, declarationIndex) => {
    console.log(declaration);
  });

});

Find and iterates on Declarations (filter by property):

// Stylesheet Level (root)
stylesheet.findAllRulesByType('rule', (rule, ruleIndex) => {

  // Rule Level
  rule.findDeclarationsByProperty('max-width', (declaration, declarationIndex) => {
    console.log(declaration);
  });

});

Find and iterates on Declarations (filter by value):

// Stylesheet Level (root)
stylesheet.findAllRulesByType('rule', (rule, ruleIndex) => {

  // Rule Level
  rule.findDeclarationsByValue('500px', (declaration, declarationIndex) => {
    console.log(declaration);
  });

});

Declarations Level

Add a new declaration:

// Stylesheet Level (root)
stylesheet.findAllRulesByType('rule', (rule, ruleIndex) => {

  // Rule Level
  rule.findDeclarationsByProperty('max-width', (declaration, declarationIndex) => {

    // Declarations Level
    rule.addDeclaration('width', '50px', declarationIndex);

  });

});

Remove a declaration:

// Stylesheet Level (root)
stylesheet.findAllRulesByType('rule', (rule, ruleIndex) => {

  // Rule Level
  rule.findDeclarationsByProperty('max-width', (declaration, declarationIndex) => {

    // Declarations Level
    rule.removeDeclaration(declarationIndex);

  });

});

Get a specific param from a value:

// Stylesheet Level (root)
stylesheet.findAllRulesByType('rule', (rule, ruleIndex) => {

  // Rule Level
  rule.findDeclarationsByProperty('max-width', (declaration, declarationIndex) => {

    // Declarations Level
    declaration.getParam(0); // position of param

  });

});

Development

Code Style

Follow the NodeJS code style guide.

Validate the code style with ESLint:

$ npm run eslint

Tests

Run the unit tests with mocha:

$ npm test

Calculate the coverage with Istanbul:

$ npm run cover

Versioning

To keep better organization of releases we follow the Semantic Versioning 2.0.0 guidelines.

Contributing

Find on our issues the next steps of the project ;) Want to contribute? Follow these recommendations.

History

See Releases for detailed changelog.

License

MIT License © Afonso Pacifer