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

@hdsydsvenskan/dom-utils

v1.0.0

Published

Basic small typed DOM helpers that aids in the creation of vanilla JS code

Downloads

1,805

Readme

DOM Utils

Basic small typed DOM helpers that aids in the creation of vanilla JS code.

Makes it easy to query, create and modify DOM-nodes – consider it a mini-jQuery. Somewhat inspired by Bliss.js.

Installation

yarn add @hdsydsvenskan/dom-utils

Release new version

Follow Semantic Versioning and use np and a version like patch | minor | major | prepatch | preminor | premajor | prerelease | 1.2.3

np patch

Usage

import {
  $,
  createChild
} from '@hdsydsvenskan/dom-utils';

const elem = $('.a-nice-selector');

createChild(elem, 'div', 'a-nice-selector__bemish-elem', 'With some nice text in it');

Methods and types

Table of Contents

ElementContainer

Type: (Document | DocumentFragment | Element)

ensureElementContainer

Mostly for internal use. Ensures that the node is of an element container type, mostly helps type validation

Parameters

Returns (ElementContainer | undefined)

ensureHTMLElement

Mostly for internal use. Ensures that the node is an actual HTML element, mostly helps type validation

Parameters

  • elem T

ensureHTMLElements

Mostly for internal use. Ensures that a list of nodes only contains HTML elements, mostly helps type validation

Parameters

$$

Get an array of HTML elements that matches the specified selector

Parameters

  • selector T
  • context ElementContainer? If set, only looks up elements within the context container

$

Like $$, but returns only a single HTML element

If one needs to match against the context container element itself, then use elemByClass instead

Parameters

addText

Adds text nodes to the supplied element, persisting newlines by adding br elements for each newline

Parameters

hasClass

Parameters

Returns boolean

removeClass

Parameters

Returns void

addClass

Parameters

Returns void

toggleClass

Parameters

Returns void

appendChild

Helper to append many children nodes at once

Parameters

setAttributes

Helper to easily set a collection of attributes

Parameters

removeElement

Parameters

Returns void

Meta

  • deprecated: Use element.remove() instead

emptyElement

Iterates over all children in a container and removes them all

Parameters

getDataAttribute

Helper that makes one don't have to do kebab case conversion oneself

Parameters

Returns (string | undefined)

setDataAttribute

Helper that makes one don't have to do kebab case conversion oneself

Parameters

createElement

Helper to easily create a new HTML element, with all that one would need for that

Parameters

Returns HTMLElement

createChild

Like createElement, but also appends the created element to a container

Helpful when creating multiple elements within one another as one can then send the result of one as the container to the other.

Parameters

Returns HTMLElement

closestByClass

Iterates over the parents of a node and returns the first one that has the specified class name

Parameters

Returns (HTMLElement | undefined)

elemByClass

Like $, but with class name rather than selector + also matches against the context itself, not just elements within it

Parameters

Returns (HTMLElement | undefined)

elemsByClass

Like elemByClass but replaces $$ instead and either returns the context itself if it matches, or a list of matching elements within it

Parameters

Returns Array<HTMLElement>

insertBefore

Parameters

Returns T