minor refactors
[squeep-html-template-helper] / lib / template-helper.js
index 7f7021ae010f2a58be394f17e23ea4120a4570d7..e8163abbe41733c0e6af0293b49cf22102a98178 100644 (file)
@@ -19,7 +19,7 @@ const initContext = (ctx) => {
 
 /**
  * Some fields may have values outside normal dates, handle them here.
- * @param {Date} date
+ * @param {Date|Number} date
  * @param {String} otherwise
  */
 const dateOrNot = (date, otherwise) => {
@@ -203,7 +203,8 @@ ${htmlFooter(ctx, options)}
  * @returns {String}
  */
 function renderNavLink(nav) {
-  return `<a href="${nav.href}"${nav.class ? (' class="' + nav.class + '"') : ''}>${nav.text}</a>`;
+  const aClass = nav.class ? ` class="${nav.class}"` : '';
+  return `<a href="${nav.href}"${aClass}>${nav.text}</a>`;
 }
 
 
@@ -277,7 +278,10 @@ ${spacer}</footer>` : '';
  * @returns {String}
  */
 function elementAttributes(attributes) {
-  const attr = Object.entries(attributes).map(([name, value]) => `${name}="${value}"`).join(' ');
+  const attr = Object.entries(attributes).map(([name, value]) => {
+    const v = value ? `="${value}"` : '';
+    return `${name}${v}`;
+  }).join(' ');
   return attr ? ' ' + attr : '';
 }
 
@@ -295,19 +299,33 @@ function LI(item, indent = 0, attributes = {}) {
 }
 
 
+/**
+ * Wrap an array of items in a list container element.
+ * @param {String} element
+ * @param {Number} indent
+ * @param {Object} attributes
+ * @param {String[]} items
+ * @param {(item, index, array) => {Object}} itemAttributeGenerator
+ * @returns {String}
+ */
+function listContainer(element, indent, attributes, items, itemAttributeGenerator) {
+  const spacer = '\t'.repeat(indent);
+  return `${spacer}<${element}${elementAttributes(attributes)}>
+${items.map((item, index, array) => LI(item, indent + 1, itemAttributeGenerator(item, index, array))).join('\n')}
+${spacer}</${element}>`;
+}
+
+
 /**
  * Wrap a list of items in an unordered list.
  * @param {String[]} items
  * @param {Number} indent
  * @param {Object} attributes
- * @param {(item) => Object} itemAttributeGenerator
+ * @param {(item, index, array) => Object} itemAttributeGenerator
  * @returns {String}
  */
 function UL(items, indent = 0, attributes = {}, itemAttributeGenerator = () => {}) {
-  const spacer = '\t'.repeat(indent);
-  return `${spacer}<ul${elementAttributes(attributes)}>
-${items.map((item) => LI(item, indent + 1, itemAttributeGenerator(item))).join('\n')}
-${spacer}</ul>`;
+  return listContainer('ul', indent, attributes, items, itemAttributeGenerator);
 }
 
 
@@ -316,15 +334,12 @@ ${spacer}</ul>`;
  * @param {String[]} items
  * @param {Number} indent
  * @param {Object} attributes
- * @param {(item) => Object} itemAttributeGenerator
+ * @param {(item, index, array) => Object} itemAttributeGenerator
  * @returns {String}
  */
 
 function OL(items, indent = 0, attributes = {}, itemAttributeGenerator = () => {}) {
-  const spacer = '\t'.repeat(indent);
-  return `${spacer}<ol${elementAttributes(attributes)}>
-${items.map((item) => LI(item, indent + 1, itemAttributeGenerator(item))).join('\n')}
-${spacer}</ol>`;
+  return listContainer('ol', indent, attributes, items, itemAttributeGenerator);
 }
 
 
@@ -404,7 +419,9 @@ module.exports = {
   indented,
   renderNavLink,
   LI,
+  listContainer,
   UL,
   OL,
   htmlPage,
+  elementAttributes,
 };