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

zenhand

v2.0.0

Published

Turn a shorthand CSS selector like HTML string into an HTML object.

Downloads

840

Readme

Zenhand Travis Build Status

Zenhand parses a shorthand CSS selector like string representing an HTML element, and returns an object describing that element.

Example

var {zenhand} = require('zenhand')
// import {zenhand} from 'zenhand'  // If you're using es modules.

var obj = zenhand('div#ex.exmpl.info[style=background:red;color:black][data-name=temp]')
console.log(obj)

output:

{ tag: 'div',
  attrs: 
   { class: [ 'exmpl', 'info' ],
     style: { background: 'red', color: 'black' },
     id: 'ex',
     'data-name': 'temp' } }

Install

npm install --save zenhand

or

yarn add zenhand

API

zenhand(str[, opts])

  • str The CSS selector like string to convert into an object representing the HTML element. The str is made up of the following parts:
    • The very first part of the string can be an HTML tag, if left out it defaults to div.
    • # defines an id, should only have one, duplicates will overwrite one another, can be placed anywhere in the string after the tag.
    • . defines a class, multiples allowed, duplicates will be added to the class property, can be placed anywhere in the string after the tag.
    • [attr=val] place an attribute definition inside square brackets, multiples allowed, can be placed anywhere in the string after the tag.
  • opts An options object.
    • changeStyleCase If true automatic conversion between camelCase and kebab-case for property names, default is true.
  • return An object representing the HTML element.

The module also exports two helper functions; toStyleStr and fromStyleStr.

toStyleStr(obj[, fromCase, toCase])

Convert an object representation of CSS styles into a string, optionally converting property case.

var {toStyleStr, zenhand} = require('zenhand')
// Input.
var obj = {
  position: 'absolute',
  'backgroundColor': '#ff0000',
}

console.log(toStyleStr(obj, 'camel', 'kebab'))

// Output.
'position:absolute; background-color:#ff0000;'

Supports camel for camelCase, kebab for kebab-case, and snake for snake_case.

fromStyleStr(str[, fromCase, toCase])

Convert a str representation of CSS styles into an object, optionally converting property case.

var {fromStyleStr, zenhand} = require('zenhand')
// Input.
var str = 'position:absolute; background-color:#ff0000;'

console.log(fromStyleStr(str, 'kebab', 'camel'))

// Output.
{
  position: 'absolute',
  'backgroundColor': '#ff0000',
}

Supports camel for camelCase, kebab for kebab-case, and snake for snake_case.