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

html-class

v1.2.0

Published

A basic utility to retrieve a DOM node name from its constructor and vice-versa

Downloads

46

Readme

html-class build status

A basic utility to retrieve DOM nodes names from their constructors and vice-versa.

API

Right now this module has two methods: htmlClass.get(...) and htmlClass.set(...).

The purpose of this API is to maintain in a single module all relations between HTML constructors and relative nodes. This module is environment agnostic and it does not require any DOM understand from the engine.

htmlClass.get(...)

The .get(...) method has 3 overloads. Following their description.

htmlClass.get(nodeName:string):string

Given a generic DOM node name, it returns its constructor name.

const htmlClass = require('html-class');

// for known elements
// logs: 'HTMLAnchorElement'
htmlClass.get('a');

// unknown elements, empty string
// logs: ''
htmlClass.get('not-registered');

htmlClass.get(Class:string):Array

Given a generic DOM Class name, it returns a collection of elements that inherits form that prototype.

const htmlClass = require('html-class');

// for known constructors
// logs: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
htmlClass.get('HTMLHeadingElement');

// logs: ['button']
htmlClass.get('HTMLButtonElement');


// unknown constructors, empty Array
// logs: []
htmlClass.get('NotRegistered');

htmlClass.get(re:RegExp):Array

Given a generic regular expression, it returns a collection of elements or constructors that matched such test.

// returns ['HTMLButtonElement']
htmlClass.get(/^HTMLButton/);

// returns all known/registered custom elements
// ['my-el', 'x-form', 'some-data']
htmlClass.get(/^[a-z]+-/);

htmlClass.set(...)

The .set(...) method has 2 overloads. Following their description.

htmlClass.set(nodeName:string, Class:string):htmlClass

If not previously registered, adds nodeName to the list and associate it with the Class.

// register a generic custom element
htmlClass.set('my-custom-element', 'MyCustomElement');

// logs: 'MyCustomElement'
htmlClass.get('my-custom-element');
// logs: ['my-custom-element']
htmlClass.get('MyCustomElement');

// if repeated, nothing happens
htmlClass.set('my-custom-element', 'MyOtherElement');

// if another component wasn't registered though
// it will be added to the generic constructor list
htmlClass.set('my-other-element', 'MyCustomElement');

// logs: ['my-custom-element', 'my-other-element']
htmlClass.get('MyCustomElement');

htmlClass.set(Class:string, nodeName:string):htmlClass

Does exactly the same thing htmlClass.set(nodeName, Class) does, assuming by specs constructors are PascalCase and node names are either fully lower or upper case.

Compatibility

The code is compatible with every JavaScript engine, old or new, either via browser or server.