@dwp/govuk-casa 9.0.0 → 9.1.0

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.
Files changed (199) hide show
  1. package/README.md +9 -9
  2. package/dist/assets/css/casa.css +1 -1
  3. package/dist/assets/css/casa.css.map +1 -1
  4. package/dist/casa.d.ts +122 -95
  5. package/dist/casa.js +119 -86
  6. package/dist/casa.js.map +1 -1
  7. package/dist/lib/CasaTemplateLoader.d.ts +4 -4
  8. package/dist/lib/CasaTemplateLoader.js +16 -16
  9. package/dist/lib/CasaTemplateLoader.js.map +1 -1
  10. package/dist/lib/JourneyContext.d.ts +38 -40
  11. package/dist/lib/JourneyContext.js +81 -75
  12. package/dist/lib/JourneyContext.js.map +1 -1
  13. package/dist/lib/MutableRouter.d.ts +40 -41
  14. package/dist/lib/MutableRouter.js +64 -71
  15. package/dist/lib/MutableRouter.js.map +1 -1
  16. package/dist/lib/Plan.d.ts +29 -26
  17. package/dist/lib/Plan.js +85 -71
  18. package/dist/lib/Plan.js.map +1 -1
  19. package/dist/lib/ValidationError.d.ts +16 -15
  20. package/dist/lib/ValidationError.js +21 -20
  21. package/dist/lib/ValidationError.js.map +1 -1
  22. package/dist/lib/ValidatorFactory.d.ts +15 -13
  23. package/dist/lib/ValidatorFactory.js +14 -12
  24. package/dist/lib/ValidatorFactory.js.map +1 -1
  25. package/dist/lib/configuration-ingestor.d.ts +37 -40
  26. package/dist/lib/configuration-ingestor.js +93 -93
  27. package/dist/lib/configuration-ingestor.js.map +1 -1
  28. package/dist/lib/configure.d.ts +6 -6
  29. package/dist/lib/configure.js +14 -12
  30. package/dist/lib/configure.js.map +1 -1
  31. package/dist/lib/constants.d.ts +1 -3
  32. package/dist/lib/constants.js +9 -11
  33. package/dist/lib/constants.js.map +1 -1
  34. package/dist/lib/context-id-generators.d.ts +3 -5
  35. package/dist/lib/context-id-generators.js +7 -6
  36. package/dist/lib/context-id-generators.js.map +1 -1
  37. package/dist/lib/end-session.d.ts +4 -4
  38. package/dist/lib/end-session.js +5 -5
  39. package/dist/lib/field.d.ts +20 -18
  40. package/dist/lib/field.js +35 -48
  41. package/dist/lib/field.js.map +1 -1
  42. package/dist/lib/index.d.ts +13 -13
  43. package/dist/lib/logger.d.ts +7 -6
  44. package/dist/lib/logger.js +7 -7
  45. package/dist/lib/logger.js.map +1 -1
  46. package/dist/lib/mount.d.ts +5 -5
  47. package/dist/lib/mount.js +11 -10
  48. package/dist/lib/mount.js.map +1 -1
  49. package/dist/lib/nunjucks-filters.d.ts +10 -12
  50. package/dist/lib/nunjucks-filters.js +35 -35
  51. package/dist/lib/nunjucks-filters.js.map +1 -1
  52. package/dist/lib/nunjucks.d.ts +7 -5
  53. package/dist/lib/nunjucks.js +10 -8
  54. package/dist/lib/nunjucks.js.map +1 -1
  55. package/dist/lib/utils.d.ts +19 -19
  56. package/dist/lib/utils.js +62 -55
  57. package/dist/lib/utils.js.map +1 -1
  58. package/dist/lib/validators/dateObject.d.ts +29 -22
  59. package/dist/lib/validators/dateObject.js +58 -49
  60. package/dist/lib/validators/dateObject.js.map +1 -1
  61. package/dist/lib/validators/email.d.ts +4 -4
  62. package/dist/lib/validators/email.js +4 -4
  63. package/dist/lib/validators/inArray.d.ts +4 -4
  64. package/dist/lib/validators/inArray.js +7 -8
  65. package/dist/lib/validators/inArray.js.map +1 -1
  66. package/dist/lib/validators/index.d.ts +10 -10
  67. package/dist/lib/validators/index.js +1 -3
  68. package/dist/lib/validators/index.js.map +1 -1
  69. package/dist/lib/validators/nino.d.ts +9 -8
  70. package/dist/lib/validators/nino.js +14 -10
  71. package/dist/lib/validators/nino.js.map +1 -1
  72. package/dist/lib/validators/postalAddressObject.d.ts +37 -24
  73. package/dist/lib/validators/postalAddressObject.js +65 -46
  74. package/dist/lib/validators/postalAddressObject.js.map +1 -1
  75. package/dist/lib/validators/range.d.ts +12 -8
  76. package/dist/lib/validators/range.js +11 -9
  77. package/dist/lib/validators/range.js.map +1 -1
  78. package/dist/lib/validators/regex.d.ts +4 -4
  79. package/dist/lib/validators/regex.js +5 -5
  80. package/dist/lib/validators/required.d.ts +6 -6
  81. package/dist/lib/validators/required.js +9 -11
  82. package/dist/lib/validators/required.js.map +1 -1
  83. package/dist/lib/validators/strlen.d.ts +12 -8
  84. package/dist/lib/validators/strlen.js +13 -11
  85. package/dist/lib/validators/strlen.js.map +1 -1
  86. package/dist/lib/validators/wordCount.d.ts +12 -8
  87. package/dist/lib/validators/wordCount.js +15 -11
  88. package/dist/lib/validators/wordCount.js.map +1 -1
  89. package/dist/lib/waypoint-url.d.ts +16 -13
  90. package/dist/lib/waypoint-url.js +39 -36
  91. package/dist/lib/waypoint-url.js.map +1 -1
  92. package/dist/middleware/body-parser.d.ts +1 -1
  93. package/dist/middleware/body-parser.js +6 -6
  94. package/dist/middleware/body-parser.js.map +1 -1
  95. package/dist/middleware/data.d.ts +1 -1
  96. package/dist/middleware/data.js +8 -7
  97. package/dist/middleware/data.js.map +1 -1
  98. package/dist/middleware/gather-fields.d.ts +2 -2
  99. package/dist/middleware/gather-fields.js +6 -4
  100. package/dist/middleware/gather-fields.js.map +1 -1
  101. package/dist/middleware/i18n.js +13 -15
  102. package/dist/middleware/i18n.js.map +1 -1
  103. package/dist/middleware/post.js +30 -18
  104. package/dist/middleware/post.js.map +1 -1
  105. package/dist/middleware/pre.d.ts +2 -2
  106. package/dist/middleware/pre.js +46 -27
  107. package/dist/middleware/pre.js.map +1 -1
  108. package/dist/middleware/progress-journey.d.ts +1 -1
  109. package/dist/middleware/progress-journey.js +5 -5
  110. package/dist/middleware/progress-journey.js.map +1 -1
  111. package/dist/middleware/sanitise-fields.d.ts +1 -1
  112. package/dist/middleware/sanitise-fields.js +13 -11
  113. package/dist/middleware/sanitise-fields.js.map +1 -1
  114. package/dist/middleware/serve-first-waypoint.d.ts +3 -3
  115. package/dist/middleware/serve-first-waypoint.js +8 -6
  116. package/dist/middleware/serve-first-waypoint.js.map +1 -1
  117. package/dist/middleware/session.js +14 -11
  118. package/dist/middleware/session.js.map +1 -1
  119. package/dist/middleware/skip-waypoint.d.ts +1 -1
  120. package/dist/middleware/skip-waypoint.js +3 -3
  121. package/dist/middleware/skip-waypoint.js.map +1 -1
  122. package/dist/middleware/steer-journey.d.ts +1 -1
  123. package/dist/middleware/steer-journey.js +16 -14
  124. package/dist/middleware/steer-journey.js.map +1 -1
  125. package/dist/middleware/strip-proxy-path.d.ts +1 -1
  126. package/dist/middleware/strip-proxy-path.js +3 -3
  127. package/dist/middleware/strip-proxy-path.js.map +1 -1
  128. package/dist/middleware/validate-fields.d.ts +1 -1
  129. package/dist/middleware/validate-fields.js +2 -5
  130. package/dist/middleware/validate-fields.js.map +1 -1
  131. package/dist/routes/ancillary.d.ts +3 -3
  132. package/dist/routes/ancillary.js +4 -4
  133. package/dist/routes/ancillary.js.map +1 -1
  134. package/dist/routes/journey.d.ts +2 -2
  135. package/dist/routes/journey.js +91 -39
  136. package/dist/routes/journey.js.map +1 -1
  137. package/dist/routes/static.d.ts +7 -5
  138. package/dist/routes/static.js +20 -19
  139. package/dist/routes/static.js.map +1 -1
  140. package/package.json +19 -18
  141. package/src/casa.js +133 -100
  142. package/src/lib/CasaTemplateLoader.js +24 -19
  143. package/src/lib/JourneyContext.js +138 -107
  144. package/src/lib/MutableRouter.js +72 -74
  145. package/src/lib/Plan.js +145 -97
  146. package/src/lib/ValidationError.js +25 -21
  147. package/src/lib/ValidatorFactory.js +17 -13
  148. package/src/lib/configuration-ingestor.js +147 -110
  149. package/src/lib/configure.js +34 -32
  150. package/src/lib/constants.js +9 -11
  151. package/src/lib/context-id-generators.js +40 -43
  152. package/src/lib/end-session.js +6 -6
  153. package/src/lib/field.js +69 -58
  154. package/src/lib/index.js +12 -12
  155. package/src/lib/logger.js +9 -9
  156. package/src/lib/mount.js +70 -74
  157. package/src/lib/nunjucks-filters.js +56 -59
  158. package/src/lib/nunjucks.js +23 -18
  159. package/src/lib/utils.js +78 -57
  160. package/src/lib/validators/dateObject.js +71 -60
  161. package/src/lib/validators/email.js +8 -8
  162. package/src/lib/validators/inArray.js +10 -11
  163. package/src/lib/validators/index.js +12 -14
  164. package/src/lib/validators/nino.js +29 -15
  165. package/src/lib/validators/postalAddressObject.js +87 -63
  166. package/src/lib/validators/range.js +14 -12
  167. package/src/lib/validators/regex.js +8 -8
  168. package/src/lib/validators/required.js +16 -16
  169. package/src/lib/validators/strlen.js +16 -14
  170. package/src/lib/validators/wordCount.js +22 -14
  171. package/src/lib/waypoint-url.js +64 -46
  172. package/src/middleware/body-parser.js +10 -10
  173. package/src/middleware/csrf.js +1 -1
  174. package/src/middleware/data.js +28 -24
  175. package/src/middleware/gather-fields.js +10 -9
  176. package/src/middleware/i18n.js +35 -37
  177. package/src/middleware/post.js +41 -21
  178. package/src/middleware/pre.js +62 -41
  179. package/src/middleware/progress-journey.js +32 -18
  180. package/src/middleware/sanitise-fields.js +43 -20
  181. package/src/middleware/serve-first-waypoint.js +14 -12
  182. package/src/middleware/session.js +74 -61
  183. package/src/middleware/skip-waypoint.js +7 -9
  184. package/src/middleware/steer-journey.js +40 -28
  185. package/src/middleware/strip-proxy-path.js +8 -7
  186. package/src/middleware/validate-fields.js +5 -12
  187. package/src/routes/ancillary.js +5 -7
  188. package/src/routes/journey.js +159 -85
  189. package/src/routes/static.js +62 -29
  190. package/views/casa/components/character-count/README.md +2 -2
  191. package/views/casa/components/checkboxes/README.md +6 -6
  192. package/views/casa/components/date-input/README.md +7 -7
  193. package/views/casa/components/input/README.md +2 -2
  194. package/views/casa/components/journey-form/README.md +33 -14
  195. package/views/casa/components/postal-address-object/README.md +4 -4
  196. package/views/casa/components/radios/README.md +6 -6
  197. package/views/casa/components/select/README.md +6 -6
  198. package/views/casa/components/textarea/README.md +2 -2
  199. package/views/casa/layouts/main.njk +2 -1
package/src/lib/mount.js CHANGED
@@ -1,39 +1,40 @@
1
- import { Router } from 'express';
2
- import { pathToRegexp } from 'path-to-regexp';
1
+ import { Router } from "express";
2
+ import { pathToRegexp } from "path-to-regexp";
3
3
 
4
- import stripProxyPathMiddlewareFactory from '../middleware/strip-proxy-path.js';
5
- import serveFirstWaypointMiddlewareFactory from '../middleware/serve-first-waypoint.js';
4
+ import stripProxyPathMiddlewareFactory from "../middleware/strip-proxy-path.js";
5
+ import serveFirstWaypointMiddlewareFactory from "../middleware/serve-first-waypoint.js";
6
6
 
7
7
  /**
8
+ * @typedef {import("nunjucks").Environment} NunjucksEnvironment
8
9
  * @access private
9
- * @typedef {import('nunjucks').Environment} NunjucksEnvironment
10
10
  */
11
11
 
12
12
  /**
13
+ * @typedef {import("express").RequestHandler} ExpressRequestHandler
13
14
  * @access private
14
- * @typedef {import('express').RequestHandler} ExpressRequestHandler
15
15
  */
16
16
 
17
17
  /**
18
+ * @typedef {import("../casa").Mounter} Mounter
18
19
  * @access private
19
- * @typedef {import('../casa').Mounter} Mounter
20
20
  */
21
21
 
22
22
  /**
23
+ * @typedef {import("../casa").Plan} Plan
23
24
  * @access private
24
- * @typedef {import('../casa').Plan} Plan
25
25
  */
26
26
 
27
27
  /**
28
+ * @typedef {import("../casa").MutableRouter} MutableRouter
28
29
  * @access private
29
- * @typedef {import('../casa').MutableRouter} MutableRouter
30
30
  */
31
31
 
32
32
  /**
33
33
  * Mounting function factory.
34
34
  *
35
35
  * @param {object} args Arguments
36
- * @param {NunjucksEnvironment} args.nunjucksEnv Pre-configured Nunjucks environment
36
+ * @param {NunjucksEnvironment} args.nunjucksEnv Pre-configured Nunjucks
37
+ * environment
37
38
  * @param {string} [args.mountUrl] Mount URL
38
39
  * @param {Plan} [args.plan] CASA Plan
39
40
  * @param {MutableRouter} args.staticRouter Router for all static assets
@@ -45,77 +46,72 @@ import serveFirstWaypointMiddlewareFactory from '../middleware/serve-first-waypo
45
46
  * @param {ExpressRequestHandler[]} args.bodyParserMiddleware Middleware
46
47
  * @param {ExpressRequestHandler[]} args.dataMiddleware Middleware
47
48
  * @param {ExpressRequestHandler[]} args.postMiddleware Middleware
48
- * @returns {Mounter} mount
49
+ * @returns {Mounter} Mount
49
50
  */
50
51
  export default ({
51
- nunjucksEnv,
52
- mountUrl,
53
- plan,
54
- staticRouter,
55
- ancillaryRouter,
56
- journeyRouter,
57
- preMiddleware,
58
- sessionMiddleware,
59
- i18nMiddleware,
60
- bodyParserMiddleware,
61
- dataMiddleware,
62
- postMiddleware,
63
- }) => (
64
- app,
65
- {
66
- route = '/',
67
- serveFirstWaypoint = false,
68
- } = {},
69
- ) => {
70
- nunjucksEnv.express(app);
71
- app.set('view engine', 'njk');
52
+ nunjucksEnv,
53
+ mountUrl,
54
+ plan,
55
+ staticRouter,
56
+ ancillaryRouter,
57
+ journeyRouter,
58
+ preMiddleware,
59
+ sessionMiddleware,
60
+ i18nMiddleware,
61
+ bodyParserMiddleware,
62
+ dataMiddleware,
63
+ postMiddleware,
64
+ }) =>
65
+ (app, { route = "/", serveFirstWaypoint = false } = {}) => {
66
+ nunjucksEnv.express(app);
67
+ app.set("view engine", "njk");
72
68
 
73
- // If a `mountUrl` has been defined, then we're potentially in "proxy mode",
74
- // in which we strip the proxy path prefix from the incoming request URLs.
75
- if (mountUrl) {
76
- app.use(stripProxyPathMiddlewareFactory({ mountUrl }));
77
- }
69
+ // If a `mountUrl` has been defined, then we're potentially in "proxy mode",
70
+ // in which we strip the proxy path prefix from the incoming request URLs.
71
+ if (mountUrl) {
72
+ app.use(stripProxyPathMiddlewareFactory({ mountUrl }));
73
+ }
78
74
 
79
- // Attach a handler to redirect requests for `/` to the first waypoint in
80
- // the plan
81
- if (serveFirstWaypoint && plan) {
82
- const re = pathToRegexp(`${route}`.replace(/\/+/g, '/'));
83
- app.use(re, serveFirstWaypointMiddlewareFactory({ plan }));
84
- }
75
+ // Attach a handler to redirect requests for `/` to the first waypoint in
76
+ // the plan
77
+ if (serveFirstWaypoint && plan) {
78
+ const re = pathToRegexp(`${route}`.replace(/\/+/g, "/"));
79
+ app.use(re, serveFirstWaypointMiddlewareFactory({ plan }));
80
+ }
85
81
 
86
- // Capture the mount path of this CASA app, before any parameterised path
87
- // segments exert influence over `req.baseUrl` in the `router` further below.
88
- // This can later be used by middleware that wants to use the
89
- // "unparameterised" version of the request's `baseUrl`, such as the static
90
- // router's middleware.
91
- app.use((req, res, next) => {
92
- req.unparameterisedBaseUrl = req.baseUrl;
93
- next();
94
- });
82
+ // Capture the mount path of this CASA app, before any parameterised path
83
+ // segments exert influence over `req.baseUrl` in the `router` further below.
84
+ // This can later be used by middleware that wants to use the
85
+ // "unparameterised" version of the request's `baseUrl`, such as the static
86
+ // router's middleware.
87
+ app.use((req, res, next) => {
88
+ req.unparameterisedBaseUrl = req.baseUrl;
89
+ next();
90
+ });
95
91
 
96
- // Serve static assets from the `app` rather than the `router`. The router
97
- // may contain parameterised path segments which would mean serving static
98
- // assets over a dynamic URL each time, thus causing lots of cache misses on
99
- // the browser.
100
- const sealedStaticRouter = staticRouter.seal();
101
- app.use(preMiddleware);
102
- app.use(sealedStaticRouter);
92
+ // Serve static assets from the `app` rather than the `router`. The router
93
+ // may contain parameterised path segments which would mean serving static
94
+ // assets over a dynamic URL each time, thus causing lots of cache misses on
95
+ // the browser.
96
+ const sealedStaticRouter = staticRouter.seal();
97
+ app.use(preMiddleware);
98
+ app.use(sealedStaticRouter);
103
99
 
104
- const router = Router({
105
- // Required so that any parameters in the URL are propagated to middleware
106
- mergeParams: true,
107
- });
100
+ const router = Router({
101
+ // Required so that any parameters in the URL are propagated to middleware
102
+ mergeParams: true,
103
+ });
108
104
 
109
- router.use(preMiddleware);
110
- router.use(sessionMiddleware);
111
- router.use(i18nMiddleware);
112
- router.use(bodyParserMiddleware);
113
- router.use(dataMiddleware);
114
- router.use(ancillaryRouter.seal());
115
- router.use(journeyRouter.seal());
116
- router.use(postMiddleware);
105
+ router.use(preMiddleware);
106
+ router.use(sessionMiddleware);
107
+ router.use(i18nMiddleware);
108
+ router.use(bodyParserMiddleware);
109
+ router.use(dataMiddleware);
110
+ router.use(ancillaryRouter.seal());
111
+ router.use(journeyRouter.seal());
112
+ router.use(postMiddleware);
117
113
 
118
- app.use(route, router);
114
+ app.use(route, router);
119
115
 
120
- return app;
121
- };
116
+ return app;
117
+ };
@@ -1,6 +1,6 @@
1
- import merge from 'deepmerge';
2
- import { DateTime } from 'luxon';
3
- import nunjucks from 'nunjucks';
1
+ import merge from "deepmerge";
2
+ import { DateTime } from "luxon";
3
+ import nunjucks from "nunjucks";
4
4
 
5
5
  const { all: deepmergeAll } = merge;
6
6
 
@@ -9,54 +9,54 @@ const { all: deepmergeAll } = merge;
9
9
  // ref: https://www.npmjs.com/package/deepmerge
10
10
 
11
11
  const combineMerge = (target, source, options) => {
12
- const destination = target.slice()
12
+ const destination = target.slice();
13
13
 
14
14
  source.forEach((item, index) => {
15
15
  // ESLint disabled as `index` is only an integer
16
16
  /* eslint-disable security/detect-object-injection */
17
- if (typeof destination[index] === 'undefined') {
18
- destination[index] = options.cloneUnlessOtherwiseSpecified(item, options)
17
+ if (typeof destination[index] === "undefined") {
18
+ destination[index] = options.cloneUnlessOtherwiseSpecified(item, options);
19
19
  } else if (options.isMergeableObject(item)) {
20
- destination[index] = merge(target[index], item, options)
20
+ destination[index] = merge(target[index], item, options);
21
21
  } else if (target.indexOf(item) === -1) {
22
- destination.push(item)
22
+ destination.push(item);
23
23
  }
24
24
  /* eslint-enable security/detect-object-injection */
25
- })
26
- return destination
27
- }
25
+ });
26
+ return destination;
27
+ };
28
28
 
29
29
  // Allows objects to be deepmerged and retain their type, without becoming [object Object]
30
30
  // ref: https://github.com/jonschlinkert/is-plain-object/blob/master/is-plain-object.js
31
31
 
32
32
  function isObject(o) {
33
- return Object.prototype.toString.call(o) === '[object Object]';
33
+ return Object.prototype.toString.call(o) === "[object Object]";
34
34
  }
35
35
 
36
36
  function isPlainObjectOrArray(o) {
37
37
  if (Array.isArray(o)) {
38
- return true
38
+ return true;
39
39
  }
40
40
  if (isObject(o) === false) {
41
- return false
41
+ return false;
42
42
  }
43
43
  const ctor = o.constructor;
44
44
  if (ctor === undefined) {
45
- return true
45
+ return true;
46
46
  }
47
47
  const prot = ctor.prototype;
48
48
  if (isObject(prot) === false) {
49
- return false
49
+ return false;
50
50
  }
51
51
  // eslint-disable-next-line no-prototype-builtins
52
- return prot.hasOwnProperty('isPrototypeOf');
52
+ return prot.hasOwnProperty("isPrototypeOf");
53
53
  }
54
54
 
55
55
  function mergeObjects(...objects) {
56
- return deepmergeAll([
57
- Object.create(null),
58
- ...objects,
59
- ], { arrayMerge: combineMerge, isMergeableObject: isPlainObjectOrArray });
56
+ return deepmergeAll([Object.create(null), ...objects], {
57
+ arrayMerge: combineMerge,
58
+ isMergeableObject: isPlainObjectOrArray,
59
+ });
60
60
  }
61
61
  /**
62
62
  * Determine whether a value exists in a list.
@@ -66,7 +66,7 @@ function mergeObjects(...objects) {
66
66
  * @param {any} search Item to search within the `source`
67
67
  * @returns {boolean} True if the search item was found
68
68
  */
69
- function includes(source = [], search = '') {
69
+ function includes(source = [], search = "") {
70
70
  return source.includes(search);
71
71
  }
72
72
 
@@ -75,43 +75,43 @@ function includes(source = [], search = '') {
75
75
  *
76
76
  * Requires NodeJS >= 14 to make use of bundled date locale data.
77
77
  *
78
- * `date` may be any of the following types:
79
- * object - {dd:'', mm:'', yyyy:''}
78
+ * `date` may be any of the following types: object - {dd:'', mm:'', yyyy:''}
80
79
  *
81
80
  * @memberof NunjucksFilters
82
- * @param {object} date Date
83
- * @param {string} date.dd Day
84
- * @param {string} date.mm Month
85
- * @param {string} date.yyyy Year
86
- * @param {object} [config] Options
87
- * @param {string} [config.locale] Locale (default 'en')
88
- * @param {string} [config.format] Format (default 'd MMMM yyyy')
81
+ * @param {object} date Date
82
+ * @param {string} date.dd Day
83
+ * @param {string} date.mm Month
84
+ * @param {string} date.yyyy Year
85
+ * @param {object} [config] Options
86
+ * @param {string} [config.locale] Locale (default 'en')
87
+ * @param {string} [config.format] Format (default 'd MMMM yyyy')
89
88
  * @returns {string} Formatted date
90
89
  */
91
90
  function formatDateObject(date, config = {}) {
92
- const { locale = 'en', format = 'd MMMM yyyy' } = config;
91
+ const { locale = "en", format = "d MMMM yyyy" } = config;
93
92
 
94
93
  if (
95
- Object.prototype.toString.call(date) === '[object Object]'
96
- && 'yyyy' in date
97
- && 'mm' in date
98
- && 'dd' in date
94
+ Object.prototype.toString.call(date) === "[object Object]" &&
95
+ "yyyy" in date &&
96
+ "mm" in date &&
97
+ "dd" in date
99
98
  ) {
100
99
  return DateTime.fromObject({
101
100
  year: Math.max(0, parseInt(date.yyyy, 10)),
102
101
  month: Math.max(0, parseInt(date.mm, 10)),
103
102
  day: Math.max(1, parseInt(date.dd, 10)),
104
- }).setLocale(locale).toFormat(format);
103
+ })
104
+ .setLocale(locale)
105
+ .toFormat(format);
105
106
  }
106
- return 'INVALID DATE OBJECT';
107
+ return "INVALID DATE OBJECT";
107
108
  }
108
109
 
109
110
  /**
110
111
  * Attribute values will be HTML/attribute escaped.
111
112
  *
112
- * Example:
113
- * Given: {class: 'basic', 'data-ga': 3}
114
- * Output: class="basic" data-ga="3"
113
+ * Example: Given: {class: 'basic', 'data-ga': 3} Output: class="basic"
114
+ * data-ga="3"
115
115
  *
116
116
  * @memberof NunjucksFilters
117
117
  * @param {object} attrsObject Attributes object (in name:value pairs)
@@ -119,32 +119,29 @@ function formatDateObject(date, config = {}) {
119
119
  */
120
120
  function renderAsAttributes(attrsObject) {
121
121
  const attrsList = [];
122
- if (typeof attrsObject === 'object') {
122
+ if (typeof attrsObject === "object") {
123
123
  Object.keys(attrsObject).forEach((key) => {
124
124
  // ESLint disable as `attrsObject` is dev-controlled, `Object.keys()` has
125
125
  // been used (to get "own" properties) and `m` is one of the characters
126
126
  // found by the regex.
127
127
  /* eslint-disable security/detect-object-injection */
128
- const value = String(attrsObject[key]).replace(/[<>"'&]/g, (m) => ({
129
- '<': '&lt;',
130
- '>': '&gt;',
131
- '"': '&quot;',
132
- '\'': '&#039;',
133
- '&': '&amp;',
134
- }[m]));
128
+ const value = String(attrsObject[key]).replace(
129
+ /[<>"'&]/g,
130
+ (m) =>
131
+ ({
132
+ "<": "&lt;",
133
+ ">": "&gt;",
134
+ '"': "&quot;",
135
+ "'": "&#039;",
136
+ "&": "&amp;",
137
+ })[m],
138
+ );
135
139
  /* eslint-enable security/detect-object-injection */
136
140
  attrsList.push(`${key}="${value}"`);
137
141
  });
138
142
  }
139
- return new nunjucks.runtime.SafeString(attrsList.join(' '));
143
+ return new nunjucks.runtime.SafeString(attrsList.join(" "));
140
144
  }
141
145
 
142
- /**
143
- * @namespace NunjucksFilters
144
- */
145
- export {
146
- mergeObjects,
147
- includes,
148
- formatDateObject,
149
- renderAsAttributes,
150
- };
146
+ /** @namespace NunjucksFilters */
147
+ export { mergeObjects, includes, formatDateObject, renderAsAttributes };
@@ -1,27 +1,29 @@
1
- import { readFileSync } from 'fs';
2
- import { resolve } from 'path';
3
- import { Environment } from 'nunjucks';
4
- import dirname from './dirname.cjs';
5
- import CasaTemplateLoader from './CasaTemplateLoader.js';
1
+ import { readFileSync } from "fs";
2
+ import { resolve } from "path";
3
+ import { Environment } from "nunjucks";
4
+ import dirname from "./dirname.cjs";
5
+ import CasaTemplateLoader from "./CasaTemplateLoader.js";
6
6
  import {
7
- mergeObjects, includes, renderAsAttributes, formatDateObject,
8
- } from './nunjucks-filters.js';
7
+ mergeObjects,
8
+ includes,
9
+ renderAsAttributes,
10
+ formatDateObject,
11
+ } from "./nunjucks-filters.js";
9
12
 
10
13
  /**
11
14
  * @typedef {object} NunjucksOptions
12
- * @property {string[]} [views=[]] Template file directories (optional, default [])
15
+ * @property {string[]} [views=[]] Template file directories (optional, default
16
+ * []). Default is `[]`
13
17
  */
14
18
 
15
19
  /**
16
20
  * Create a Nunjucks environment.
17
21
  *
18
- * @access private
19
22
  * @param {NunjucksOptions} options Nunjucks options
20
23
  * @returns {Environment} Nunjucks Environment instance
24
+ * @access private
21
25
  */
22
- export default function nunjucksConfig({
23
- views = [],
24
- }) {
26
+ export default function nunjucksConfig({ views = [] }) {
25
27
  // Prepare a single Nunjucks environment for all responses to use. Note that
26
28
  // we cannot prepare response-specific global functions/filters if we use a
27
29
  // single environment, but the performance gains of doing so are significant.
@@ -42,13 +44,16 @@ export default function nunjucksConfig({
42
44
 
43
45
  // Globals
44
46
  // These can't be modified once set. But they can be overridden by res.locals.
45
- /* eslint-disable-next-line security/detect-non-literal-fs-filename */
46
- env.addGlobal('casaVersion', JSON.parse(readFileSync(resolve(dirname, '../../package.json'))).version);
47
+ env.addGlobal(
48
+ "casaVersion",
49
+ /* eslint-disable-next-line security/detect-non-literal-fs-filename */
50
+ JSON.parse(readFileSync(resolve(dirname, "../../package.json"))).version,
51
+ );
47
52
 
48
- env.addGlobal('mergeObjects', mergeObjects);
49
- env.addGlobal('includes', includes);
50
- env.addGlobal('formatDateObject', formatDateObject);
51
- env.addGlobal('renderAsAttributes', renderAsAttributes);
53
+ env.addGlobal("mergeObjects", mergeObjects);
54
+ env.addGlobal("includes", includes);
55
+ env.addGlobal("formatDateObject", formatDateObject);
56
+ env.addGlobal("renderAsAttributes", renderAsAttributes);
52
57
 
53
58
  return env;
54
59
  }