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

@alexvipond/css-selector-pipes

v0.0.1

Published

Pipeline functions for building CSS selectors programmatically

Downloads

23

Readme

CSS Selector Builder

This project is a TypeScript library for building CSS selectors programmatically, and also a user interface around that library.

Motivation

  • Create a nice interface that people can use to build CSS selectors and explore different types of CSS selectors
  • Practice type definitions, test-driven development, functional programming, pipelines, dark mode design, controlled components, and recursive components in Vue 3
  • Have fun with my favorite tools
    • TypeScript
    • Vue 3 Composition API
    • Tailwind
    • Vite, both for building the website and bundling the library
    • uvu (test runner)
  • Try out Headless UI

Usage

Interface

To use the CSS selector builder interface, go to the website.

As you build your selector, you'll see the URL update with any new conditions you've created. You can share that URL with anyone to show them what you've built!

To play with the selector builder's recursive abilities:

  1. Add a new condition
  2. Open the dropdown, and type matches
  3. Pick one of the options that starts with "matches" to render a nested selector builder. Fun stuff!

The relevant source code for the user interface is in in the src/interface directory.

Library

To use the library:

npm i @alexvipond/css-selector-pipes

From the library, you can import functions for building CSS selectors:

import { tag, className, not } from '@alexvipond/css-selector-pipes'

tag('h1')()          // 'h1'
className('poop')()  // .poop
not(tag('h1')())()   // ':not(h1)'

Each function is a higher order function, returning a function that accepts a CSS selector (String) as its only parameter, and returns the transformed selector.

import { tag, className } from '@alexvipond/css-selector-pipes'

const tagFunction = tag('h1')
const classNameFunction = className('poop')

tagFunction(classNameFunction()) // h1.poop

You can also import a pipe utility that makes it easier to compose multiple functions into a selector pipeline.

import { pipe, tag, className, attribute, focus } from '@alexvipond/css-selector-pipes'

pipe(
  tag('h1'),
  className('poop'),
  attribute('name', '$=', 'lol'),
  focus()
)() // h1.poop["name"$="lol"]:focus

Or, if your dev environment supports it, you can use the upcoming pipeline operator:

import { tag, className, attribute, focus } from '@alexvipond/css-selector-pipes'

|> tag('h1'),
|> className('poop'),
|> attribute('name', '$=', 'lol'),
|> focus()

// h1.poop["name"$="lol"]:focus

All functions are fully typed, and you can check out these test files for further documentation and a list of available functions:

The relevant source code is in the src/pipes directory.

Development

Set up the project locally:

git clone https://github.com/AlexVipond/css-selector-builder && cd css-selector-builder && npm install

Run tests:

npm run test

Run tests for a specific file:

npm run test:only [filename, excluding the .test.ts extension]

npm tun test:only toSelector
npm tun test:only append

Run the interface in development mode on localhost:3000:

npm run dev

Build the interface for production:

npm run build

Build the library for production:

npm run build:lib