Newer
Older
express-blog / src / utils / hbsHelpers.js
@Jason Jason on 22 Jul 1 KB modified: content
function registerHelpers(hbs) {
  hbs.handlebars.registerHelper("formatMonth", function (monthStr) {
    const monthNames = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December",
    ];
    const index = parseInt(monthStr, 10) - 1;
    if (index < 0 || index > 11) return monthStr;
    return monthNames[index];
  });

  hbs.handlebars.registerHelper("formatDate", function (date) {
    const d = new Date(date);
    const yyyy = d.getFullYear();
    const mm = String(d.getMonth() + 1).padStart(2, "0");
    const dd = String(d.getDate()).padStart(2, "0");
    return `${yyyy}-${mm}-${dd}`;
  });
  hbs.handlebars.registerHelper("isObject", function (value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
  });

  hbs.handlebars.registerHelper("isArray", function (value) {
    return Array.isArray(value);
  });
}

module.exports = { registerHelpers };