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 🙏

© 2026 – Pkg Stats / Ryan Hefner

simplify-selector

v0.2.7

Published

A library for reducing a CSS selector down to its simplest possible form, while still matching the same element(s)

Readme

simplify-selector

A utility for simplifying CSS selector queries to their simplest possible form in a fully automated manner, without losing accuracy. Works with any type of document tree, not just the browser DOM.

This is an early version of this library; don't expect too much from it in terms of reliability, input validation, correct serialization, and so on.

Example

let selector = [ // div.testClass #uniqueID
	[{ type: "tag", selector: "div" }, { type: "class", selector: "testClass" }],
	[{ type: "id", selector: "uniqueID" }]
];

let simplifiedQuery = simplifySelector(selector, (newSelector) => document.querySelectorAll(newSelector));
console.log(`Simplified query: ${simplifiedQuery.stringQuery}`);

Selector format

Selectors can exist in either string format (like a browser would expect it) or in 'structured data format', which is specific to this library. The structure is visible in the example above; it's an array of arrays, where each inner array represents a (space-separated) segment of the query and each object inside represents a component of that segment.

Currently supported types are tag, class, id, pseudo, and attribute - where the attribute type accepts a {key, value} object as the selector attribute. Note that pseudo selectors must specify the entire selector including any parameters, eg. { type: "pseudo", selector: "nth-of-child(3)" }.

Additionally, any component may have a protected property; when this is set to true, the selector will not be removed from the query no matter what. Use this if you want to ensure that a specific selector always shows up in the output. The segment containing it does not need to be marked as protected; this is detected automatically.

simplifySelector(selector, testCallback)

  • selector: Required. The original selector to reduce. This must be in the structured data format, not in string format.
  • testCallback: Required. A function that, when called with a selector query (in string format), returns all matches as an array. The shape of the returned objects in the arrays doesn't matter, as long as they're consistent between invocations.
  • options: Optional.
    • keepRemoved: Defaults to false. Whether to keep Removed symbols in the output, everywhere a selector segment or component was removed. Sometimes useful for eg. tracking changes in the simplification process. The string-format query never contains these symbols. The symbol is exported from this library.
    • anchorID: Defaults to false. ID anchoring is a technique used in scraping, where a selector query starts with the nearest ancestor element ID. As IDs are meant to be unique and are often stable, this tends to result in more 'stable' selectors, at the cost of them sometimes being larger. Enabling this option will cause simplify-selector to try this (faster) technique first, falling back to the normal process if it fails. Enable it if you are generating scraping queries; disable it if you are trying to eg. minify stylesheets in a build process.

Returns: An object with the following properties:

  • query: The minimal selector, following the same selector format as the input
  • stringQuery: The selector in string form, ie. in the form that you would pass into document.querySelector or some other element selection method
  • matchesMultiple: Whether the selector matches multiple elements (true) or not (false).

simplifySelector.Removed

A Symbol that represents a removed segment or component in the simplified query. Only needed if you set the keepRemoved option. See the tests for an example of use.

simplifySelector.stringify(selector)

The function that simplify-selector uses internally for turning a selector in the structured data format, into the string format. This is not guaranteed to be a correct implementation; the only guarantee is that its output is equivalent to the stringQuery result.

  • selector: The selector, in structured data format, to stringify. This is allowed to contain Removed symbols.

Returns: The selector in string format.