• Stars
    star
    300
  • Rank 138,870 (Top 3 %)
  • Language
    CSS
  • License
    MIT License
  • Created over 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Analytics for CSS

CSS Analyzer

Analyzer logo
A CSS analyzer that goes through your CSS to find all kinds of relevant statistics.

Features

  • Extremely detailed (150+ metrics)
  • Super fast
  • Supports both NodeJS and browsers

Install

npm install @projectwallace/css-analyzer

Usage

Analyzing CSS

import { analyze } from '@projectwallace/css-analyzer'

const result = analyze(`
	p {
		color: blue;
		font-size: 100%;
	}

	.component[data-state="loading"] {
		background-color: whitesmoke;
	}
`)
More examples output can be found in the fixtures folder and looks roughly like this:
{
  "stylesheet": {
    "sourceLinesOfCode": 5,
    "linesOfCode": 8,
    "size": 113,
    "comments": {
      "total": 0,
      "size": 0
    }
  },
  "atrules": {
    "fontface": {
      "total": 0,
      "totalUnique": 0,
      "unique": [],
      "uniquenessRatio": 1
    },
    "import": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "media": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "charset": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "supports": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "keyframes": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0,
      "prefixed": {
        "total": 0,
        "totalUnique": 0,
        "unique": {},
        "uniquenessRatio": 0,
        "ratio": null
      }
    },
    "container": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    }
  },
  "rules": {
    "total": 2,
    "empty": {
      "total": 0,
      "ratio": 0
    },
    "selectors": {
      "min": 1,
      "max": 1,
      "mean": 1,
      "mode": 1,
      "median": 1,
      "range": 0,
      "sum": 2,
      "items": [
        1,
        1
      ]
    },
    "declarations": {
      "min": 1,
      "max": 2,
      "mean": 1.5,
      "mode": 1.5,
      "median": 1.5,
      "range": 1,
      "sum": 3,
      "items": [
        2,
        1
      ]
    }
  },
  "selectors": {
    "total": 2,
    "totalUnique": 2,
    "uniquenessRatio": 1,
    "specificity": {
      "sum": [
        0,
        2,
        1
      ],
      "min": [
        0,
        0,
        1
      ],
      "max": [
        0,
        2,
        0
      ],
      "mean": [
        0,
        1,
        0.5
      ],
      "mode": [
        0,
        1,
        0.5
      ],
      "median": [
        0,
        1,
        0.5
      ],
      "items": [
        [
          0,
          0,
          1
        ],
        [
          0,
          2,
          0
        ]
      ]
    },
    "complexity": {
      "min": 1,
      "max": 3,
      "mean": 2,
      "mode": 2,
      "median": 2,
      "range": 2,
      "sum": 4,
      "total": 2,
      "totalUnique": 2,
      "unique": {
        "1": 1,
        "3": 1
      },
      "uniquenessRatio": 1,
      "items": [
        1,
        3
      ]
    },
    "id": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0,
      "ratio": 0
    },
    "accessibility": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0,
      "ratio": 0
    },
    "keyframes": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0,
      "ratio": 0
    }
  },
  "declarations": {
    "total": 3,
    "unique": {
      "total": 3,
      "ratio": 1
    },
    "importants": {
      "total": 0,
      "ratio": 0,
      "inKeyframes": {
        "total": 0,
        "ratio": 0
      }
    }
  },
  "properties": {
    "total": 3,
    "totalUnique": 3,
    "unique": {
      "color": 1,
      "font-size": 1,
      "background-color": 1
    },
    "uniquenessRatio": 1,
    "prefixed": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0,
      "ratio": 0
    },
    "custom": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0,
      "ratio": 0
    },
    "browserhacks": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0,
      "ratio": 0
    }
  },
  "values": {
    "colors": {
      "total": 2,
      "totalUnique": 2,
      "unique": {
        "blue": 1,
        "whitesmoke": 1
      },
      "uniquenessRatio": 1,
      "itemsPerContext": {
        "color": {
          "total": 1,
          "totalUnique": 1,
          "unique": {
            "blue": 1
          },
          "uniquenessRatio": 1
        },
        "background-color": {
          "total": 1,
          "totalUnique": 1,
          "unique": {
            "whitesmoke": 1
          },
          "uniquenessRatio": 1
        }
      }
    },
    "fontFamilies": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "fontSizes": {
      "total": 1,
      "totalUnique": 1,
      "unique": {
        "100%": 1
      },
      "uniquenessRatio": 1
    },
    "zindexes": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "textShadows": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "boxShadows": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "animations": {
      "durations": {
        "total": 0,
        "totalUnique": 0,
        "unique": {},
        "uniquenessRatio": 0
      },
      "timingFunctions": {
        "total": 0,
        "totalUnique": 0,
        "unique": {},
        "uniquenessRatio": 0
      }
    },
    "prefixes": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0
    },
    "units": {
      "total": 0,
      "totalUnique": 0,
      "unique": {},
      "uniquenessRatio": 0,
      "itemsPerContext": {}
    }
  },
  "__meta__": {
    "parseTime": 4,
    "analyzeTime": 5,
    "total": 10
  }
}

Comparing specificity

import { compareSpecificity } from '@projectwallace/css-analyzer'

const result = [
  [0,1,1],
  [2,0,0],
  [0,0,1],
].sort((a, b) => compareSpecificity(a, b))

// => result:
// [
//   [2,0,0],
//   [0,1,1],
//   [0,0,1],
// ]

const isSpecificityEqual = compareSpecificity(
  [0,1,0],
  [0,1,0]
) === 0
// => isSpecificityEqual: true

Related projects

  • CSS Code Quality Analyzer - A Code Quality analyzer that tells you how maintainable, complex and performant your CSS is
  • Wallace CLI - CLI tool for @projectwallace/css-analyzer
  • Constyble - CSS Complexity linter
  • Color Sorter - Sort CSS colors by hue, saturation, lightness and opacity
  • CSS Diff Github Action - A GitHub action that comments on your PR to tell you how your code quality has changed

More Repositories

1

wallace-cli

Pretty CSS analytics on the CLI
JavaScript
519
star
2

constyble

CSS complexity linter
JavaScript
101
star
3

color-sorter

Sort CSS colors by hue, then by saturation
JavaScript
65
star
4

css-code-quality

Calculate the Code Quality score of your CSS based on a range of different quality guards.
JavaScript
61
star
5

css-diff-action

Project Wallace GitHub Action to report CSS diffs to PR's
JavaScript
38
star
6

extract-css-core

Extract all CSS from a given url, both server side and client side rendered.
JavaScript
36
star
7

extract-css

Extract all CSS from a webpage, packaged as a Now V2 Lambda
JavaScript
27
star
8

format-css

Fast, small, zero-config library to format CSS using basic rules.
JavaScript
8
star
9

css-value-browser-h4cks-analyzer

A list of tests to determine whether a CSS value is considered a browser hack
JavaScript
4
star
10

push-css-action

Project Wallace GitHub Action to push new CSS to projectwallace.com automatically
JavaScript
3
star
11

og-image

Generate OpenGraph images for Project Wallace.
JavaScript
3
star
12

css-media-query-browser-h4cks-analyzer

A list of tests to determine whether an at-rule is considered a browser hack
JavaScript
3
star
13

css-time-sort

Sort an array of CSS <time> values.
JavaScript
2
star
14

api

JavaScript
2
star
15

has-css-accessibility-selector

A test to determine wether a selector contains an accessibility-related selector
JavaScript
1
star
16

browser-extension

A Browser Extension to fetch all CSS on a page and analyze it
1
star
17

minify-css

A small, fast, zero-config CSS minifier. No structural changes; only minifying.
JavaScript
1
star
18

css-property-browser-h4cks-analyzer

A list of tests to determine whether a CSS property is considered a browser hack
JavaScript
1
star
19

css-at-supports-browser-h4cks-analyzer

A list of tests to determine whether an `@supports` rule is considered a browser hack
JavaScript
1
star
20

has-css-universal-selector

A test to determine wether a selector contains a universal selector
JavaScript
1
star
21

constyble-continuous-integration-example

This repository demonstrates how to use Constyble in a project
CSS
1
star
22

css-selector-browser-h4cks-analyzer

A list of tests to determine whether a selector is considered a browser hack
JavaScript
1
star
23

css-selector-complexity

Calculate the Cyclomatic Complexity of a CSS selector
JavaScript
1
star
24

has-css-id-selector

A test to determine wether a selector contains an ID selector
JavaScript
1
star
25

has-css-js-selector

A test to determine wether a selector contains a JavaScript selector
JavaScript
1
star
26

stylelint-plugin

JavaScript
1
star
27

specificity

JavaScript
1
star