Skip to content

AgnosticRun is a lightweight utility for ensuring a DOM element exists before executing a function in vanilla JavaScript.

License

Notifications You must be signed in to change notification settings

BansheeTech/AgnosticRun

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AgnosticRun

AgnosticRun is a lightweight utility for ensuring a DOM element exists before executing a function in vanilla JavaScript. It's useful when dealing with dynamic DOM manipulation or when you want to ensure that certain elements are present before applying any JavaScript logic to them.

Installation

To install AgnosticRun using npm, run the following command:

npm install agnosticrun

Usage

To use AgnosticRun, import the agnosticRun function and wrap your own function that interacts with the DOM.

Example:

import { agnosticRun } from 'agnosticrun';

// Define your function that will interact with a DOM element
function updateElement(element, text) {
  element.textContent = text;
}

// Wrap your function using agnosticRun to ensure the element exists
const safeUpdate = agnosticRun(updateElement);

// Call the wrapped function with the element's ID and the desired content
safeUpdate('element-id', 'Hello, World!');

In this example, AgnosticRun ensures that the element with ID element-id exists before attempting to update its text content. If the element does not exist, the function will not execute.

Disable Warns and/or Logs

AgnosticRun allows you to control the appearance of console.log and console.warn messages through optional flags. You can enable or disable these messages as needed using the following options:

  • debugLog: Controls the appearance of informational messages (console.log). It can be set to true or false.
  • debugWarn: Controls the appearance of warning messages (console.warn). It can be set to true or false.

By default, both flags are disabled.

Example usage:

Show only warns on console:

window.agnosticRun = agnosticRun({ debugLog: false, debugWarn: true });

Show only debug messages on console:

window.agnosticRun = agnosticRun({ debugLog: true, debugWarn: false });

Doesn't show any message:

window.agnosticRun = agnosticRun;

-or-

window.agnosticRun = agnosticRun({ debugLog: false, debugWarn: false });

License

AgnosticRun is licensed under the MIT License. See the LICENSE file for details.

Issues

If you encounter any issues, feel free to report them here.