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

@datadomjs/datadom

v1.0.2

Published

A lightweight utility for retrieving DOM elements using custom data attributes.

Downloads

225

Readme

datadom

Simple | Robust | Universal | Vanilla-Friendly | Prototype-Ready

A lightweight utility for retrieving DOM elements using custom data attributes.

<main>
  <h1 data-dom="title">Title</h1>
  <p data-dom="text">Text</p>
</main>

<script type="module">
  import { getDataDom } from "@datadomjs/datadom";

  const elements = {
    title: "Title element",
    text: "Text element",
  };

  try {
    const { title, text } = getDataDom(elements);
    console.log(title); // <h1 data-dom="title">Title</h1>
    console.log(text); // <p data-dom="text">Text</p>
  } catch (error) {
    console.error("Error:", error.message);
  }
</script>

Install

npm

npm install @datadomjs/datadom

yarn

yarn add @datadomjs/datadom

pnpm

pnpm add @datadomjs/datadom

CDN

<script src="https://unpkg.com/@datadomjs/[email protected]/dist/index.js"></script>

Document

Argument

| Argument | Type | Default | Description | | ---------- | ---------------- | ------- | ------------------------------------------------------------------------------------------------------------------------ | | elements | T | - | An object mapping element keys to their descriptions. Each key corresponds to a data-dom attribute value in your HTML. | | options | DataDomOptions | {} | Optional configuration object. |

Options

| Option | Type | Default | Description | | ------------------- | ----------------------- | -------------------------- | ------------------------------------------------------------------- | | parent | string \| HTMLElement | document.documentElement | The parent element or selector within which to search for elements. | | dataAttributeName | string | 'dom' | The name of the data attribute to use for selecting elements. | | errorThrow | boolean | true | Whether to throw an error when elements are not found. |

Example

Basic

const elements = {
  title: "Main title",
  text: "Main text",
};

try {
  const { title, text } = getDataDom(elements);
  console.log(title); // <h1 data-dom="title">Title</h1>
  console.log(text); // <p data-dom="text">Text</p>
} catch (error) {
  console.error("Error:", error.message);
}

Parent Option

<main data-dom-parent="main">
  <h1 data-dom="title">Title</h1>
  <p data-dom="text">Text</p>
</main>
const elements = {
  title: "Main title",
  text: "Main text",
};

const options = {
  parent: "main",
};

try {
  const { title, text, parent } = getDataDom(elements, options);
  console.log(title); // <h1 data-dom="title">Title</h1>
  console.log(text); // <p data-dom="text">Text</p>
  console.log(parent); // <main data-dom-parent="main"></main>
} catch (error) {
  console.error("Error:", error.message);
}

Data Attribute Option

<main>
  <h1 data-custom-attr="title">Title</h1>
  <p data-custom-attr="text">Text</p>
</main>
const elements = {
  title: "Main title",
  text: "Main text",
};

const options = {
  dataAttributeName: "custom-attr",
};

try {
  const { title, text } = getDataDom(elements, options);
  console.log(title); // <h1 data-custom-attr="title">Title</h1>
  console.log(text); // <p data-custom-attr="text">Text</p>
} catch (error) {
  console.error("Error:", error.message);
}

Error Throw Option

When errorThrow is set to false, instead of throwing an error, the function will return null and log an error message to the console. This allows the script to continue running even if the element is not found.

<main>
  <h1 data-dom="title">Title</h1>
  <p data-dom="text">Text</p>
</main>
const elements = {
  title: "Main title",
  text: "Main text",
};

const options = {
  errorThrow: false,
};

const { title, text } = getDataDom(elements, options);

Retrieving Multiple Elements

If there are multiple elements with the same data attribute value, getDataDom will return an array of elements.

<main>
  <p data-dom="texts">text 1</p>
  <p data-dom="texts">text 2</p>
</main>
const elements = {
  texts: "Texts",
};

try {
  const { texts } = getDataDom(elements);
  console.log(texts);
} catch (error) {
  console.error("Error:", error.message);
}

// [
//   <p data-dom="texts">text 1</p>
//   <p data-dom="texts">text 2</p>
// ]

In this example, getDataDom returns an array for the item key because there are multiple elements with data-dom="item".

TypeScript Support

type ElementType = {
  title: string;
  text: string;
};

const elements: ElementType = {
  title: "Main title",
  text: "Main text",
};

try {
  const { title, text } = getDataDom<ElementType>(elements);
  console.log(title); // <h1 data-dom="title">Title</h1>
  console.log(text); // <p data-dom="text">Text</p>
} catch (error) {
  console.error("Error:", error.message);
}

Error Handling

If an element is not found, getDataDom will throw an error. This allows you to handle missing elements gracefully.

const elements = {
  nonExistent: "Non-existent element",
};

try {
  const { nonExistent } = getDataDom(elements);
} catch (error) {
  console.error("Error:", error.message);
  // Error: Element "Non-existent element" not found in parent HTML (data-dom="nonExistent")
}

In this case, an error is thrown because there are no elements with data-dom="nonExistent".