From: Justin Wind Date: Thu, 3 Nov 2022 18:11:01 +0000 (-0700) Subject: add initContext helper to set fields we expect to be present X-Git-Tag: v1.4.0~3 X-Git-Url: http://git.squeep.com/?p=squeep-html-template-helper;a=commitdiff_plain;h=a2c0e2fc57bb8f25f694e16eb4e51d6970b28829 add initContext helper to set fields we expect to be present --- diff --git a/README.md b/README.md index 66d16fd..c6f9226 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,16 @@ Specific to Squeep Framework Applications, this module has strong opinions and m ## API -`htmlPage(pagePathLevel, ctx, options, main = [])` +- `initContext(ctx)` +- `htmlPage(pagePathLevel, ctx, options, main = [])` +- `dateOrNot(date, otherwise)` +- `dateFormat(date, pInf nInf, otherwise)` +- `timeElement(date, options)` +- `secondsToPeriod(seconds)` +- `indented(intent, list)` +- `UL(items, indent, attributes, itemAttributesGenerator)` +- `OL(items, indent, attributes, itemAttributesGenerator)` +- `LI(item, indent, attributes)` ### Context @@ -26,6 +35,7 @@ If present, added as a query parameter (`r`, for redirect) to the logout link. - `ctx.notifications` Lists of errors and notices to show at top of page, before main content. +`initContext` will create the arrays. ### Options diff --git a/lib/template-helper.js b/lib/template-helper.js index c983e80..2a218f8 100644 --- a/lib/template-helper.js +++ b/lib/template-helper.js @@ -6,6 +6,17 @@ const { lazy } = require('@squeep/lazy-property'); + +/** + * Set up expected fields for how we handle error reporting + * and whatnot. + * @param {Object} ctx + */ +const initContext = (ctx) => { + ctx.errors = []; + ctx.notifications = []; +} + /** * Some fields may have values outside normal dates, handle them here. * @param {Date} date @@ -392,6 +403,7 @@ function htmlPage(pagePathLevel, ctx, options, main = []) { module.exports = { + initContext, dateOrNot, dateFormat, timeElement, diff --git a/test/lib/template-helper.js b/test/lib/template-helper.js index fa3f86a..58985cc 100644 --- a/test/lib/template-helper.js +++ b/test/lib/template-helper.js @@ -21,6 +21,16 @@ describe('Template Helper', function () { options = {}; }); + describe('initContext', function () { + it('covers', function () { + th.initContext(ctx); + assert(ctx.errors); + assert(ctx.notifications); + assert(Array.isArray(ctx.errors)); + assert(Array.isArray(ctx.notifications)); + }); + }); // initContext + describe('dateOrNot', function () { let date, otherwise; beforeEach(function () {