Comparing version 1.2.1 to 1.2.2
48
index.js
@@ -0,1 +1,9 @@ | ||
// Helper function to check if an object is empty (including arrays) | ||
const isEmpty = (obj) => { | ||
if (obj == null) return true | ||
if (Array.isArray(obj)) return obj.length === 0 | ||
if (typeof obj === 'object') return Object.keys(obj).length === 0 | ||
return false | ||
} | ||
/////////////////////////////////////////////////////////////////////////////// | ||
@@ -98,15 +106,27 @@ // MAIN | ||
iterable = Array.from({ length: collection }, (_, i) => i) | ||
} else if (Array.isArray(collection)) { | ||
} else { | ||
iterable = collection | ||
} else if (typeof collection === 'object' && collection !== null) { | ||
iterable = Object.entries(collection) | ||
} else { | ||
return '' // Return empty string for invalid inputs | ||
} | ||
return iterable.reduce((html, item, index) => { | ||
const [value, key] = Array.isArray(collection) ? [item, index] : item | ||
const returnedFromCallback = callback(value, key) | ||
return returnedFromCallback ? html + returnedFromCallback : html | ||
}, '') | ||
let html = '' | ||
if (Array.isArray(iterable)) { | ||
for (let i = 0; i < iterable.length; i++) { | ||
const returnedFromCallback = callback(iterable[i], i) | ||
if (returnedFromCallback) { | ||
html += returnedFromCallback | ||
} | ||
} | ||
} else if (typeof iterable === 'object' && iterable !== null) { | ||
for (const key in iterable) { | ||
if (Object.prototype.hasOwnProperty.call(iterable, key)) { | ||
const returnedFromCallback = callback(iterable[key], key) | ||
if (returnedFromCallback) { | ||
html += returnedFromCallback | ||
} | ||
} | ||
} | ||
} | ||
return html | ||
} | ||
@@ -133,10 +153,2 @@ | ||
// Helper function to check if an object is empty (including arrays) | ||
const isEmpty = (obj) => { | ||
if (obj == null) return true | ||
if (Array.isArray(obj)) return obj.length === 0 | ||
if (typeof obj === 'object') return Object.keys(obj).length === 0 | ||
return false | ||
} | ||
if ( | ||
@@ -143,0 +155,0 @@ evaluatedCondition === false || |
{ | ||
"name": "html-fns", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Set of convinient pure functions to generate HTML on server-side via tagged template literals.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14032
315