phane-tech-dom-utils 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 phane-tech
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,322 @@
1
+ # 📦 Kotipalli Phaneendra Kumar - Array Utilities
2
+
3
+ A lightweight, safe, and predictable **DOM utility module** that simplifies common DOM operations with **strict validation**, **consistent return values**, and **clean JSDoc documentation**.
4
+
5
+ Designed specifically for **browser environments**, this module helps you work confidently with elements, attributes, classes, styles, events, and dynamic DOM creation without unexpected runtime errors.
6
+
7
+ ---
8
+
9
+ ## ✨ Highlights
10
+
11
+ - 🧱 Safe DOM selectors with validation
12
+ - 🏷️ Attribute helpers (get / set / remove)
13
+ - 🧩 Class utilities (add / remove / toggle / check)
14
+ - 🎨 Style helpers (single & multiple styles)
15
+ - 🧩 Dynamic element creation & DOM insertion
16
+ - 🎧 Event listener helpers
17
+ - ⭐ Tab title & favicon utilities
18
+ - 📚 Fully documented using JSDoc
19
+ - ⚡ Predictable return values (`undefined` for invalid input)
20
+
21
+ ---
22
+
23
+ ## 📦 Installation
24
+
25
+ ```bash
26
+ npm install phane-js-dom-helpers
27
+ ```
28
+ ---
29
+
30
+ ## 🚀 Import
31
+
32
+ ```js
33
+ import {
34
+ getElementById,
35
+ getElementsByClassName,
36
+ getElementsByTagName,
37
+ setTabTitle,
38
+ getTabTitle,
39
+ setFavicon,
40
+ getFavicon,
41
+ setTextContent,
42
+ setHTMLContent,
43
+ setAttribute,
44
+ getAttribute,
45
+ removeAttribute,
46
+ hasClass,
47
+ addClass,
48
+ removeClass,
49
+ toggleClass,
50
+ setStyle,
51
+ setStyles,
52
+ createDynamicElement,
53
+ appendDynamicTag,
54
+ removeElement,
55
+ addEventListenerHelper,
56
+ removeEventListenerHelper,
57
+ onElementLoad,
58
+ setDataId
59
+ } from "phane-js-dom-helpers";
60
+ ```
61
+
62
+ ---
63
+
64
+ ## 📚 API Reference
65
+
66
+ ### 🔍 getElementById(id)
67
+
68
+ Safely returns a DOM element by its ID.
69
+
70
+ ```js
71
+ getElementById("header"); // HTMLElement | null
72
+ getElementById("unknown"); // null
73
+ getElementById(""); // undefined
74
+ ```
75
+
76
+ ---
77
+
78
+ ### 🔍 getElementsByClassName(className)
79
+
80
+ Returns a live HTMLCollection of elements.
81
+
82
+ ```js
83
+ getElementsByClassName("card"); // HTMLCollection
84
+ ```
85
+
86
+ ---
87
+
88
+ ### 🔍 getElementsByTagName(tagName)
89
+
90
+ Returns a live HTMLCollection of elements.
91
+
92
+ ```js
93
+ getElementsByTagName("div"); // HTMLCollection
94
+ ```
95
+
96
+ ---
97
+
98
+ ### 🏷️ setTabTitle(title)
99
+
100
+ Sets the browser tab title.
101
+
102
+ ```js
103
+ setTabTitle("Home"); // "Home"
104
+ ```
105
+
106
+ ---
107
+
108
+ ### ⭐ getTabTitle()
109
+
110
+ Returns the current tab title.
111
+
112
+ ```js
113
+ getTabTitle(); // "Home"
114
+ ```
115
+
116
+ ---
117
+
118
+ ### ⭐ setFavicon(url)
119
+
120
+ Sets or updates the browser favicon.
121
+
122
+ ```js
123
+ setFavicon("/favicon.ico");
124
+ ```
125
+
126
+ ---
127
+
128
+ ### ⭐ getFavicon()
129
+
130
+ Returns the current favicon URL if available.
131
+
132
+ ```js
133
+ getFavicon(); // string | undefined
134
+ ```
135
+
136
+ ---
137
+
138
+ ### ✏️ setTextContent(element, text)
139
+
140
+ Safely sets innerText on an element.
141
+
142
+ ```js
143
+ setTextContent(el, "Hello World");
144
+ ```
145
+
146
+ ---
147
+
148
+ ### ✏️ setHTMLContent(element, html)
149
+
150
+ Safely sets innerHTML on an element.
151
+ ```js
152
+ setHTMLContent(el, "<b>Hello</b>");
153
+ ```
154
+
155
+ ---
156
+
157
+ ### 🧱 setAttribute(element, name, value)
158
+
159
+ Adds or updates an attribute.
160
+
161
+ ```js
162
+ setAttribute(el, "data-id", "123");
163
+ ```
164
+
165
+ ---
166
+
167
+ ### 🧱 getAttribute(element, name)
168
+
169
+ Gets an attribute value.
170
+
171
+ ```js
172
+ getAttribute(el, "data-id"); // "123"
173
+ ```
174
+
175
+ ---
176
+
177
+ ### 🧱 removeAttribute(element, name)
178
+
179
+ Removes an attribute.
180
+
181
+ ```js
182
+ removeAttribute(el, "disabled");
183
+ ```
184
+
185
+ ---
186
+
187
+ ### 🧩 hasClass(element, className)
188
+
189
+ Checks if a class exists on an element.
190
+
191
+ ```js
192
+ hasClass(el, "active"); // true | false
193
+ ```
194
+
195
+ ---
196
+
197
+ ### 🧩 addClass / removeClass / toggleClass
198
+
199
+ Manages element classes safely.
200
+
201
+ ```js
202
+ addClass(el, "active");
203
+ removeClass(el, "active");
204
+ toggleClass(el, "active");
205
+ ```
206
+
207
+ ---
208
+
209
+ ### 🎨 setStyle(element, property, value)
210
+
211
+ Sets a single CSS style.
212
+
213
+ ```js
214
+ setStyle(el, "color", "red");
215
+ ```
216
+
217
+ ---
218
+
219
+ ### 🎨 setStyles(element, styles)
220
+
221
+ Applies multiple CSS styles at once.
222
+
223
+ ```js
224
+ setStyles(el, {
225
+ color: "red",
226
+ backgroundColor: "yellow"
227
+ });
228
+ ```
229
+ ---
230
+
231
+ ### 🧩 createDynamicElement(tag, content?, props?)
232
+
233
+ Creates a DOM element dynamically.
234
+
235
+ ```js
236
+ createDynamicElement("div", "Hello", {
237
+ id: "box",
238
+ className: "card"
239
+ });
240
+ ```
241
+
242
+ ---
243
+
244
+ ### ➕ appendDynamicTag(child, parent?)
245
+
246
+ Appends an element to the DOM.
247
+
248
+ ```js
249
+ appendDynamicTag(div);
250
+ appendDynamicTag(div, container);
251
+ ```
252
+ ---
253
+
254
+ ### ❌ removeElement(child, parent?)
255
+
256
+ Removes an element from the DOM.
257
+
258
+ ```js
259
+ removeElement(div);
260
+ ```
261
+
262
+ ---
263
+
264
+ ### 🎧 addEventListenerHelper(el, type, handler)
265
+
266
+ Adds an event listener safely.
267
+
268
+ ```js
269
+ addEventListenerHelper(btn, "click", handleClick);
270
+ ```
271
+
272
+ ---
273
+
274
+ ### 🎧 removeEventListenerHelper(el, type, handler)
275
+
276
+ Removes an event listener safely.
277
+
278
+ ```js
279
+ removeEventListenerHelper(btn, "click", handleClick);
280
+ ```
281
+
282
+ ---
283
+
284
+ ### ⏳ onElementLoad(element?, handler)
285
+
286
+ Executes a handler when DOM or element is ready.
287
+
288
+ ```js
289
+ onElementLoad(null, () => console.log("DOM Ready"));
290
+ ```
291
+
292
+
293
+ ---
294
+
295
+ ### 🆔 setDataId(element, value)
296
+
297
+ Sets data-id attribute.
298
+
299
+ ```js
300
+ setDataId(el, "123");
301
+ ```
302
+ ---
303
+
304
+ ## 🧪 Design Principles
305
+
306
+ - ❌ Invalid input → undefined
307
+ - ✅ Safe DOM access
308
+ - 🔁 No unexpected mutations
309
+ - 📖 Predictable, documented behavior
310
+
311
+
312
+ ## 📄 License
313
+
314
+ MIT
315
+
316
+ ---
317
+ ## 🔗 Links
318
+
319
+ - **GitHub Repository:** https://github.com/phane-tech/phane-tech-dom-utilities
320
+ - **Demo / Documentation:** https://phane-tech.github.io/phane-tech-dom-utilities/module-DomHelpers.html
321
+ - **Unit Test Cases Reports:** https://phane-tech.github.io/phane-tech-dom-utilities/unit-test-report.html
322
+
package/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ const arrayUtils = require("./js/DomUtilities.js");
2
+ module.exports = arrayUtils;
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./js/DomUtilities.js";
@@ -0,0 +1,705 @@
1
+ import { isObject, isString } from "phane-js-utils";
2
+ /**
3
+ * DOM Helpers
4
+ * @module DomHelpers
5
+
6
+ */
7
+
8
+ /**
9
+ * Get a DOM element by its ID.
10
+ *
11
+ * @function getElementById
12
+ * @memberof module:DomHelpers
13
+ *
14
+ * @param {*} elementId - The unique ID of the DOM element
15
+ * @returns { HTMLElement | null | undefined }
16
+ * Returns:a
17
+ * - HTMLElement if found
18
+ * - null if not found
19
+ * - undefined for invalid input
20
+ *
21
+ * @example
22
+ * getElementById("header") => HTMLElement
23
+ *
24
+ * getElementById("unknown") => null
25
+ *
26
+ * getElementById() => undefined
27
+ *
28
+ * getElementById(123) => undefined
29
+ */
30
+ export function getElementById(elementId) {
31
+ if (!isString(elementId) || !elementId.trim()) return undefined;
32
+ const element = document.getElementById(elementId);
33
+ return element ?? null;
34
+ }
35
+
36
+ /**
37
+ * Get DOM elements by class name.
38
+ *
39
+ * @function getElementsByClassName
40
+ * @memberof module:DomHelpers
41
+ *
42
+ * @param {*} className - The class name to search for
43
+ * @returns { HTMLCollection | undefined }
44
+ * Returns:
45
+ * - HTMLCollection (may be empty)
46
+ * - undefined for invalid input
47
+ *
48
+ * @example
49
+ * getElementsByClassName("card") => HTMLCollection
50
+ *
51
+ * getElementsByClassName("unknown") => HTMLCollection (empty)
52
+ *
53
+ * getElementsByClassName("") => undefined
54
+ *
55
+ * getElementsByClassName(123) => undefined
56
+ */
57
+ export function getElementsByClassName(className) {
58
+ if (!isString(className) || !className.trim()) return undefined;
59
+ return document.getElementsByClassName(className);
60
+ }
61
+
62
+ /**
63
+ * Get DOM elements by tag name.
64
+ *
65
+ * @function getElementsByTagName
66
+ * @memberof module:DomHelpers
67
+ *
68
+ * @param {*} tagName - The HTML tag name to search for
69
+ * @returns { HTMLCollection | undefined }
70
+ * Returns:
71
+ * - HTMLCollection (may be empty)
72
+ * - undefined for invalid input
73
+ *
74
+ * @example
75
+ * getElementsByTagName("div") => HTMLCollection
76
+ *
77
+ * getElementsByTagName("unknown") => HTMLCollection (empty)
78
+ *
79
+ * getElementsByTagName("") => undefined
80
+ *
81
+ * getElementsByTagName(null) => undefined
82
+ */
83
+ export function getElementsByTagName(tagName) {
84
+ if (!isString(tagName) || !tagName.trim()) return undefined;
85
+ return document.getElementsByTagName(tagName);
86
+ }
87
+
88
+
89
+ /**
90
+ * Set the browser tab title.
91
+ *
92
+ * @function setTabTitle
93
+ * @memberof module:DomHelpers
94
+ *
95
+ * @param {*} title - The title to set for the browser tab
96
+ * @returns { string | undefined }
97
+ * Returns:
98
+ * - string (new title) on success
99
+ * - undefined for invalid input
100
+ *
101
+ * @example
102
+ * setTabTitle("Home") => "Home"
103
+ *
104
+ * setTabTitle("") => undefined
105
+ *
106
+ * setTabTitle(null) => undefined
107
+ */
108
+ export function setTabTitle(title) {
109
+ if (!title) return undefined;
110
+ document.title = title;
111
+ return title;
112
+ }
113
+
114
+ /**
115
+ * Get the current browser tab title.
116
+ *
117
+ * @function getTabTitle
118
+ * @memberof module:DomHelpers
119
+ *
120
+ * @returns { string }
121
+ * Returns the current tab title
122
+ *
123
+ * @example
124
+ * getTabTitle() => "Home"
125
+ */
126
+ export function getTabTitle() {
127
+ return document.title;
128
+ }
129
+
130
+ /**
131
+ * Set or update the browser tab favicon.
132
+ *
133
+ * @function setFavicon
134
+ * @memberof module:DomHelpers
135
+ *
136
+ * @param {*} faviconUrl - The favicon URL
137
+ * @returns { string | undefined }
138
+ * Returns:
139
+ * - string (favicon URL) on success
140
+ * - undefined for invalid input
141
+ *
142
+ * @example
143
+ * setFavicon("/favicon.ico") => "/favicon.ico"
144
+ *
145
+ * setFavicon("") => undefined
146
+ *
147
+ * setFavicon(null) => undefined
148
+ */
149
+ export function setFavicon(faviconUrl) {
150
+ if (!isString(faviconUrl) || !faviconUrl.trim()) return undefined;
151
+ let favicon = document.querySelector("link[rel='icon']");
152
+ if (!favicon) {
153
+ favicon = createDynamicElement('link', null, { rel:'icon' });
154
+ appendDynamicTag(favicon);
155
+ }
156
+ favicon.href = faviconUrl;
157
+ return faviconUrl;
158
+ }
159
+
160
+ /**
161
+ * Get the current browser tab favicon URL.
162
+ *
163
+ * @function getFavicon
164
+ * @memberof module:DomHelpers
165
+ *
166
+ * @returns { string | undefined }
167
+ * Returns:
168
+ * - string (favicon URL) if exists
169
+ * - undefined if favicon is not set
170
+ *
171
+ * @example
172
+ * getFavicon() => "https://example.com/favicon.ico"
173
+ *
174
+ * getFavicon() => undefined
175
+ */
176
+ export function getFavicon() {
177
+ const favicon = document.querySelector("link[rel='icon']");
178
+ return favicon?.href;
179
+ }
180
+
181
+ /**
182
+ * Set text content for a DOM element.
183
+ *
184
+ * @function setTextContent
185
+ * @memberof module:DomHelpers
186
+ *
187
+ * @param {*} element - The target DOM element
188
+ * @param {*} content - The text content to set
189
+ * @returns { string | undefined }
190
+ * Returns:
191
+ * - string (content) on success
192
+ * - undefined for invalid input
193
+ *
194
+ * @example
195
+ * setTextContent(el, "Hello") => "Hello"
196
+ *
197
+ * setTextContent(el, "") => ""
198
+ *
199
+ * setTextContent(null, "Hi") => undefined
200
+ */
201
+ export function setTextContent(element, content) {
202
+ if (!(element instanceof HTMLElement)) return undefined;
203
+ element.innerText = content;
204
+ return content;
205
+ }
206
+
207
+ /**
208
+ * Set HTML content for a DOM element.
209
+ *
210
+ * @function setHTMLContent
211
+ * @memberof module:DomHelpers
212
+ *
213
+ * @param {*} element - The target DOM element
214
+ * @param {*} html - The HTML string to set
215
+ * @returns { string | undefined }
216
+ * Returns:
217
+ * - string (HTML content) on success
218
+ * - undefined for invalid input
219
+ *
220
+ * @example
221
+ * setHTMLContent(el, "<b>Hello</b>") => "<b>Hello</b>"
222
+ *
223
+ * setHTMLContent(el, "") => ""
224
+ *
225
+ * setHTMLContent(null, "<p>Hi</p>") => undefined
226
+ */
227
+ export function setHTMLContent(element, html) {
228
+ if (!(element instanceof HTMLElement)) return undefined;
229
+ element.innerHTML = html;
230
+ return html;
231
+ }
232
+
233
+ /**
234
+ * Add or update an attribute on a DOM element.
235
+ *
236
+ * @function setAttribute
237
+ * @memberof module:DomHelpers
238
+ *
239
+ * @param {*} element - The target DOM element
240
+ * @param {*} attributeName - The attribute name
241
+ * @param {*} attributeValue - The attribute value
242
+ * @returns { string | undefined }
243
+ * Returns:
244
+ * - string (attribute value) on success
245
+ * - undefined for invalid input
246
+ *
247
+ * @example
248
+ * setAttribute(el, "id", "header") => "header"
249
+ *
250
+ * setAttribute(el, "data-id", "123") => "123"
251
+ *
252
+ * setAttribute(el, "disabled", "") => ""
253
+ *
254
+ * setAttribute(null, "id", "x") => undefined
255
+ */
256
+ export function setAttribute(element, attributeName, attributeValue) {
257
+ if (!(element instanceof HTMLElement)) return undefined;
258
+ if (!isString(attributeName) || !attributeName.trim()) return undefined;
259
+ element.setAttribute(attributeName, attributeValue);
260
+ return attributeValue;
261
+ }
262
+
263
+ /**
264
+ * Get an attribute value from a DOM element.
265
+ *
266
+ * @function getAttribute
267
+ * @memberof module:DomHelpers
268
+ *
269
+ * @param {*} element - The target DOM element
270
+ * @param {*} attributeName - The attribute name
271
+ * @returns { string | null | undefined }
272
+ * Returns:
273
+ * - string if attribute exists
274
+ * - null if attribute does not exist
275
+ * - undefined for invalid input
276
+ *
277
+ * @example
278
+ * getAttribute(el, "id") => "header"
279
+ *
280
+ * getAttribute(el, "data-id") => "123"
281
+ *
282
+ * getAttribute(el, "unknown") => null
283
+ *
284
+ * getAttribute(null, "id") => undefined
285
+ */
286
+ export function getAttribute(element, attributeName) {
287
+ if (!(element instanceof HTMLElement)) return undefined;
288
+ if (!isString(attributeName) || !attributeName.trim()) return undefined;
289
+ return element.getAttribute(attributeName);
290
+ }
291
+
292
+ /**
293
+ * Remove an attribute from a DOM element.
294
+ *
295
+ * @function removeAttribute
296
+ * @memberof module:DomHelpers
297
+ *
298
+ * @param {*} element - The target DOM element
299
+ * @param {*} attributeName - The attribute name
300
+ * @returns { boolean | undefined }
301
+ * Returns:
302
+ * - true if removal was attempted
303
+ * - undefined for invalid input
304
+ *
305
+ * @example
306
+ * removeAttribute(el, "disabled") => true
307
+ *
308
+ * removeAttribute(el, "data-id") => true
309
+ *
310
+ * removeAttribute(null, "id") => undefined
311
+ */
312
+ export function removeAttribute(element, attributeName) {
313
+ if (!(element instanceof HTMLElement)) return undefined;
314
+ if (!isString(attributeName) || !attributeName.trim()) return undefined;
315
+ element.removeAttribute(attributeName);
316
+ return true;
317
+ }
318
+
319
+ /**
320
+ * Check if a DOM element has a specific class.
321
+ *
322
+ * @function hasClass
323
+ * @memberof module:DomHelpers
324
+ *
325
+ * @param {*} element - The target DOM element
326
+ * @param {*} className - The class name to check
327
+ * @returns { boolean | undefined }
328
+ * Returns:
329
+ * - true if the element has the class
330
+ * - false if the element does not have the class
331
+ * - undefined for invalid input
332
+ *
333
+ * @example
334
+ * hasClass(el, "active") => true
335
+ *
336
+ * hasClass(el, "hidden") => false
337
+ *
338
+ * hasClass(null, "active") => undefined
339
+ */
340
+ export function hasClass(element, className) {
341
+ if (!(element instanceof HTMLElement)) return undefined;
342
+ if (!isString(className) || !className.trim()) return undefined;
343
+ return element.classList.contains(className);
344
+ }
345
+
346
+ /**
347
+ * Add a class to a DOM element if it does not already exist.
348
+ *
349
+ * @function addClass
350
+ * @memberof module:DomHelpers
351
+ *
352
+ * @param {*} element - The target DOM element
353
+ * @param {*} className - The class name to add
354
+ * @returns { string | undefined }
355
+ * Returns:
356
+ * - string (class name added or already present) on success
357
+ * - undefined for invalid input
358
+ *
359
+ * @example
360
+ * addClass(el, "active") => "active"
361
+ *
362
+ * addClass(el, "") => undefined
363
+ *
364
+ * addClass(null, "active") => undefined
365
+ */
366
+ export function addClass(element, className) {
367
+ if (!(element instanceof HTMLElement)) return undefined;
368
+ if (!isString(className) || !className.trim()) return undefined;
369
+ if (!hasClass(element, className)) element.classList.add(className);
370
+ return className;
371
+ }
372
+
373
+ /**
374
+ * Remove a class from a DOM element if it exists.
375
+ *
376
+ * @function removeClass
377
+ * @memberof module:DomHelpers
378
+ *
379
+ * @param {*} element - The target DOM element
380
+ * @param {*} className - The class name to remove
381
+ * @returns { string | undefined }
382
+ * Returns:
383
+ * - string (class name removed or already absent) on success
384
+ * - undefined for invalid input
385
+ *
386
+ * @example
387
+ * removeClass(el, "active") => "active"
388
+ *
389
+ * removeClass(el, "") => undefined
390
+ *
391
+ * removeClass(null, "active") => undefined
392
+ */
393
+ export function removeClass(element, className) {
394
+ if (!(element instanceof HTMLElement)) return undefined;
395
+ if (!isString(className) || !className.trim()) return undefined;
396
+ if (hasClass(element, className)) element.classList.remove(className);
397
+ return className;
398
+ }
399
+
400
+ /**
401
+ * Toggle a class on a DOM element.
402
+ *
403
+ * @function toggleClass
404
+ * @memberof module:DomHelpers
405
+ *
406
+ * @param {*} element - The target DOM element
407
+ * @param {*} className - The class name to toggle
408
+ * @returns { string | undefined }
409
+ * Returns:
410
+ * - string (class name toggled) on success
411
+ * - undefined for invalid input
412
+ *
413
+ * @example
414
+ * toggleClass(el, "active") => "active"
415
+ *
416
+ * toggleClass(el, "") => undefined
417
+ *
418
+ * toggleClass(null, "active") => undefined
419
+ */
420
+ export function toggleClass(element, className) {
421
+ if (!(element instanceof HTMLElement)) return undefined;
422
+ if (!isString(className) || !className.trim()) return undefined;
423
+ element.classList.toggle(className);
424
+ return className;
425
+ }
426
+
427
+ /**
428
+ * Set a CSS style property on a DOM element.
429
+ *
430
+ * @function setStyle
431
+ * @memberof module:DomHelpers
432
+ *
433
+ * @param {*} element - The target DOM element
434
+ * @param {*} property - The CSS property name (camelCase)
435
+ * @param {*} value - The CSS value to apply
436
+ * @returns { string | undefined }
437
+ * Returns:
438
+ * - string (value applied) on success
439
+ * - undefined for invalid input
440
+ *
441
+ * @example
442
+ * setStyle(el, "color", "red") => "red"
443
+ *
444
+ * setStyle(el, "backgroundColor", "yellow") => "yellow"
445
+ *
446
+ * setStyle(null, "color", "red") => undefined
447
+ */
448
+ export function setStyle(element, property, value) {
449
+ if (!(element instanceof HTMLElement)) return undefined;
450
+ if (!isString(property) || !property.trim()) return undefined;
451
+ element.style[property] = value;
452
+ return value;
453
+ }
454
+
455
+ /**
456
+ * Create a DOM element dynamically with optional content and properties.
457
+ *
458
+ * @function createDynamicElement
459
+ * @memberof module:DomHelpers
460
+ *
461
+ * @param {*} tagName - The HTML tag name for the element
462
+ * @param {*} content - Optional text content
463
+ * @param {*} propertyObj - Optional object of properties to assign
464
+ * @returns { HTMLElement | undefined }
465
+ * Returns:
466
+ * - HTMLElement on success
467
+ * - undefined for invalid input
468
+ *
469
+ * @example
470
+ * createDynamicElement("div", "Hello", { id: "myDiv", className: "card" });
471
+ *
472
+ * createDynamicElement("span") => <span></span>
473
+ *
474
+ * createDynamicElement(null) => undefined
475
+ */
476
+ export function createDynamicElement(tagName, content, propertyObj) {
477
+ if (!isString(tagName) || !tagName.trim()) return undefined;
478
+ const ele = document.createElement(tagName);
479
+ if (isObject(propertyObj)) {
480
+ Object.entries(propertyObj).forEach(([key, value]) => {
481
+ ele[key] = value;
482
+ });
483
+ }
484
+ if (content) ele.innerText = content;
485
+ return ele;
486
+ }
487
+
488
+ /**
489
+ * Append a child element to a parent element or document body if parent is not provided.
490
+ *
491
+ * @function appendDynamicTag
492
+ * @memberof module:DomHelpers
493
+ *
494
+ * @param {*} child - The DOM element to append
495
+ * @param {*} parent - Optional parent DOM element (defaults to document.body)
496
+ * @returns { HTMLElement | undefined }
497
+ * Returns:
498
+ * - The appended child element on success
499
+ * - undefined for invalid input
500
+ *
501
+ * @example
502
+ * const div = createDynamicElement("div", "Hello");
503
+ * appendDynamicTag(div, document.getElementById("container"));
504
+ *
505
+ * appendDynamicTag(div) => appends to document.body
506
+ */
507
+ export function appendDynamicTag(child, parent) {
508
+ if (!(child instanceof HTMLElement) || child === null) return undefined;
509
+ if (parent) {
510
+ if (!(parent instanceof HTMLElement)) return undefined;
511
+ return parent.appendChild(child);
512
+ }
513
+ else return document.body.appendChild(child);
514
+ }
515
+
516
+ /**
517
+ * Remove a child element from a parent, or from the DOM if parent is not provided.
518
+ *
519
+ * @function removeElement
520
+ * @memberof module:DomHelpers
521
+ *
522
+ * @param {*} child - The DOM element to remove
523
+ * @param {*} parent - Optional parent DOM element
524
+ * @returns { HTMLElement | undefined }
525
+ * Returns:
526
+ * - The removed element on success
527
+ * - undefined for invalid input
528
+ *
529
+ * @example
530
+ * removeElement(child, document.getElementById("container"));
531
+ *
532
+ * removeElement(child) => removes directly from the DOM
533
+ */
534
+ export function removeElement(child, parent) {
535
+ if (!(child instanceof HTMLElement)) return undefined;
536
+ if (parent) {
537
+ if (!(parent instanceof HTMLElement)) return undefined;
538
+ parent.removeChild(child);
539
+ }
540
+ else child.remove();
541
+ return child;
542
+ }
543
+
544
+ /**
545
+ * Create a tag (script, link, etc.) and append it to the document head.
546
+ *
547
+ * @function headTags
548
+ * @memberof module:DomHelpers
549
+ *
550
+ * @param {*} tagName - The HTML tag name (e.g., "script", "link")
551
+ * @param {*} propertyObj - Optional object of properties to assign (like src, href, type, rel)
552
+ * @returns { HTMLElement | undefined }
553
+ * Returns:
554
+ * - The created and appended tag element
555
+ * - undefined for invalid input
556
+ *
557
+ * @example
558
+ * headTags("script", { src: "/app.js", type: "module" });
559
+ *
560
+ * headTags("link", { href: "/style.css", rel: "stylesheet" });
561
+ */
562
+ export function headTags(tagName, propertyObj) {
563
+ if (!isString(tagName) || !tagName.trim()) return undefined;
564
+ const tag = createDynamicElement(tagName, null, propertyObj);
565
+ const res = appendDynamicTag(tag, document.head);
566
+ return res;
567
+ }
568
+
569
+ /**
570
+ * Add an event listener to a DOM element.
571
+ *
572
+ * @function addEventListenerHelper
573
+ * @memberof module:DomHelpers
574
+ *
575
+ * @param {*} element - The target DOM element
576
+ * @param {*} type - The event type (e.g., "click", "input")
577
+ * @param {*} handler - The event handler function
578
+ * @returns { boolean | undefined }
579
+ * Returns:
580
+ * - true if the listener was added successfully
581
+ * - undefined for invalid input
582
+ *
583
+ * @example
584
+ * addEventListenerHelper(button, "click", () => alert("Clicked!"));
585
+ */
586
+ export function addEventListenerHelper(element, type, handler) {
587
+ if (!(element instanceof HTMLElement)) return undefined;
588
+ if (!isString(type) || !type.trim()) return undefined;
589
+ if (typeof handler !== "function") return undefined;
590
+ element.addEventListener(type, handler);
591
+ return true;
592
+ }
593
+
594
+ /**
595
+ * Remove an event listener from a DOM element.
596
+ *
597
+ * @function removeEventListenerHelper
598
+ * @memberof module:DomHelpers
599
+ *
600
+ * @param {*} element - The target DOM element
601
+ * @param {*} type - The event type (e.g., "click", "input")
602
+ * @param {*} handler - The event handler function to remove
603
+ * @returns { boolean | undefined }
604
+ * Returns:
605
+ * - true if the listener was removed successfully
606
+ * - undefined for invalid input
607
+ *
608
+ * @example
609
+ * removeEventListenerHelper(button, "click", handleClick);
610
+ */
611
+ export function removeEventListenerHelper(element, type, handler) {
612
+ if (!(element instanceof HTMLElement)) return undefined;
613
+ if (!isString(type) || !type.trim()) return undefined;
614
+ if (typeof handler !== "function") return undefined;
615
+ element.removeEventListener(type, handler);
616
+ return true;
617
+ }
618
+
619
+ /**
620
+ * Call a handler when the element is available or when the document is loaded.
621
+ *
622
+ * @function onElementLoad
623
+ * @memberof module:DomHelpers
624
+ *
625
+ * @param {*} element - The target DOM element. If null/undefined, uses document.
626
+ * @param {*} handler - The function to execute when ready
627
+ * @returns { boolean | undefined }
628
+ * Returns:
629
+ * - true if the listener was added successfully
630
+ * - undefined for invalid input
631
+ *
632
+ * @example
633
+ * onElementLoad(null, () => console.log("DOM fully loaded"));
634
+ *
635
+ * onElementLoad(button, () => console.log("Button is ready"));
636
+ */
637
+ export function onElementLoad(element, handler) {
638
+ if (typeof handler !== "function") return undefined;
639
+ if (!element) document.addEventListener("DOMContentLoaded", handler);
640
+ else if (element instanceof HTMLElement) {
641
+ if (document.body.contains(element)) handler();
642
+ else {
643
+ const observer = new MutationObserver((mutations, obs) => {
644
+ if (document.body.contains(element)) {
645
+ handler();
646
+ obs.disconnect();
647
+ }
648
+ });
649
+ observer.observe(document.body, { childList: true, subtree: true });
650
+ }
651
+ }
652
+ else return undefined;
653
+ return true;
654
+ }
655
+
656
+ /**
657
+ * Set a data-id attribute on a DOM element.
658
+ *
659
+ * @function setDataId
660
+ * @memberof module:DomHelpers
661
+ *
662
+ * @param {*} element - The target DOM element
663
+ * @param {*} value - The value to set for data-id
664
+ * @returns { string | undefined }
665
+ * Returns:
666
+ * - The value set on data-id
667
+ * - undefined for invalid input
668
+ *
669
+ * @example
670
+ * setDataId(button, "123"); // returns "123"
671
+ *
672
+ * setDataId(null, "123"); // returns undefined
673
+ */
674
+ export function setDataId(element, value) {
675
+ if (!(element instanceof HTMLElement)) return undefined;
676
+ element.dataset.id = value;
677
+ return value;
678
+ }
679
+
680
+ /**
681
+ * Apply multiple CSS styles to a DOM element.
682
+ *
683
+ * @function setStyles
684
+ * @memberof module:DomHelpers
685
+ *
686
+ * @param {*} element - The target DOM element
687
+ * @param {*} stylesObj - An object of CSS properties and values (camelCase)
688
+ * @returns { object | undefined }
689
+ * Returns:
690
+ * - The applied styles object on success
691
+ * - undefined for invalid input
692
+ *
693
+ * @example
694
+ * setStyles(div, { color: "red", backgroundColor: "yellow", fontSize: "16px" });
695
+ */
696
+ export function setStyles(element, stylesObj) {
697
+ if (!(element instanceof HTMLElement)) return undefined;
698
+ if (!stylesObj || !isObject(stylesObj)) return undefined;
699
+ Object.entries(stylesObj).forEach(([property, value]) => {
700
+ if (isString(property) && property.trim()) {
701
+ element.style[property] = value;
702
+ }
703
+ });
704
+ return stylesObj;
705
+ }
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "phane-tech-dom-utils",
3
+ "version": "1.0.3",
4
+ "description": "Pure JavaScript DOM utility functions",
5
+ "type": "module",
6
+ "main": "index.cjs",
7
+ "module": "index.js",
8
+ "files": [
9
+ "index.js",
10
+ "index.cjs",
11
+ "js"
12
+ ],
13
+ "keywords": [
14
+ "javascript",
15
+ "js",
16
+ "array",
17
+ "arrays",
18
+ "array-utils",
19
+ "array-helpers",
20
+ "array-methods",
21
+ "array-functions",
22
+ "utilities",
23
+ "utils",
24
+ "helpers",
25
+ "data-types",
26
+ "datatype",
27
+ "type-check",
28
+ "type-checking",
29
+ "validation",
30
+ "input-validation",
31
+ "array-validation",
32
+ "array-operations",
33
+ "flatten-array",
34
+ "sort-array",
35
+ "map-array",
36
+ "filter-array",
37
+ "find-array",
38
+ "reduce-array",
39
+ "merge-arrays",
40
+ "es-modules"
41
+ ],
42
+ "homepage": "https://phane-tech.github.io/phane-tech-array-utils/module-ArrayHelpers.html",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "https://github.com/phane-tech/phane-tech-dom-utilities"
46
+ },
47
+ "author": "Kotipalli Phaneendra Kumar",
48
+ "license": "MIT",
49
+ "scripts": {
50
+ "test": "jest",
51
+ "docs": "rimraf docs && npm run test && npx jsdoc -c jsdoc.json",
52
+ "release": "npm version patch && npm publish --access public"
53
+ },
54
+ "devDependencies": {
55
+ "@babel/core": "^7.28.5",
56
+ "@babel/preset-env": "^7.28.5",
57
+ "babel-jest": "^30.2.0",
58
+ "docdash": "^2.0.2",
59
+ "jest": "^30.2.0",
60
+ "jest-environment-jsdom": "^30.2.0",
61
+ "jest-html-reporter": "^4.3.0",
62
+ "jsdoc": "^4.0.5",
63
+ "jsdoc-to-markdown": "^9.1.3",
64
+ "rimraf": "^6.1.2"
65
+ },
66
+ "dependencies": {
67
+ "phane-js-utils": "^1.0.21"
68
+ }
69
+ }