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-dom-storage

v1.0.3

Published

A lightweight package that provides a novel approach to storing temporary data directly in the HTML DOM.

Downloads

264

Readme

HTMLDomStorage

A lightweight JavaScript library that provides a novel approach to storing temporary data directly on the HTML DOM. It allows you to store, retrieve, and manage data in the DOM elements.

Features

  • Lightweight: No dependencies, small footprint.
  • Efficient Transactions: Batch multiple operations with transaction support.
  • ES5 Compatible: Works in legacy and modern browsers.

Getting Started


Installation

Install via npm:

npm install html-dom-storage

Or include directly in the browser:

<script src="dist/html-dom-storage.min.js"></script>

Initialization

Create an instance of HTMLDomStorage and initialize the wrapper element:

var storage = new HTMLDomStorage();
storage.init(); // Initializes the wrapper in the DOM

For module systems (e.g., CommonJS, AMD, or ESM), use the UMD build:

const HTMLDomStorage = require('html-dom-storage');
// OR for ES modules
import HTMLDomStorage from 'HTMLDomStorage';
const storage = new HTMLDomStorage();
storage.init();

Set and Get Items

Store and retrieve JSON-serializable data:

// Set a key-value pair
storage.setItem('user', { name: 'Alice', age: 30 });
// Get the value by key
var user = storage.getItem('user');
console.log(user); // { name: 'Alice', age: 30 }

Use Modules for Organization

Organize data by module names:

// Store data in a module
storage.setItem('settings', { theme: 'dark' }, 'preferences');
// Retrieve data from a module
var settings = storage.getItem('settings', 'preferences');
console.log(settings); // { theme: 'dark' }

Transactions

Batch multiple operations for efficiency:

storage.startTransaction();
storage.setItem('user1', { name: 'Bob' });
storage.setItem('user2', { name: 'Eve' });
// Commit all changes at once
storage.commitTransaction();

Abort a transaction if needed:

storage.startTransaction();
storage.setItem('tempKey', { temp: true });
storage.abortTransaction(); // Discards all changes

Example Usage in HTML

<script src="dist/html-dom-storage.min.js"></script>
<script>
  var storage = new HTMLDomStorage();
  storage.init();

  storage.setItem('example', { message: 'Hello, world!' });
  console.log(storage.getItem('example')); // { message: 'Hello, world!' }
</script>

API Reference


init()

Initializes the storage wrapper in the DOM.

setItem(keyName, value, [moduleName])

Stores a key-value pair in the DOM.

  • keyName: string - The name of the key.
  • value: any - The value to store (must be JSON-serializable).
  • moduleName (optional): string - The module to organize data.

getItem(keyName, [moduleName])

Retrieves a value by key.

  • keyName: string - The name of the key.
  • moduleName (optional): string - The module where the key is stored.

startTransaction()

Starts a transaction for batching operations.

commitTransaction()

Commits all changes in the current transaction.

abortTransaction()

Aborts the current transaction, discarding all pending changes.