@defra/forms-model 3.0.201 → 3.0.203

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.
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Find correct start page for form definition
3
+ *
4
+ * Computed by counting the next paths in a form definition and returning page
5
+ * that doesn't have a reference, meaning it must be a start page / root node
6
+ *
7
+ * It's possibe for forms to be malformed and have multiple start pages while
8
+ * they're being developed. This may be valid during development, but in this
9
+ * scenario we can't detect the real start page and we require the user to fix
10
+ * this themselves
11
+ * @param data - Form definition
12
+ */
13
+ export function findStartPage(data) {
14
+ const {
15
+ pages
16
+ } = data;
17
+
18
+ // Get a unique list of all pages that are linked to by other pages
19
+ const pageReferences = new Set(pages.flatMap(page => page.next?.map(next => next.path)));
20
+
21
+ // For each page on the form, work out if it's referenced by another page
22
+ // those that aren't referenced must be start pages
23
+ const startPages = pages.map(page => page.path).filter(page => !pageReferences.has(page));
24
+
25
+ // We can only set the start page if there is a single one, else the user has made an error
26
+ if (startPages.length === 1) {
27
+ return startPages[0];
28
+ }
29
+ }
30
+ //# sourceMappingURL=find-start-page.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find-start-page.js","names":["findStartPage","data","pages","pageReferences","Set","flatMap","page","next","map","path","startPages","filter","has","length"],"sources":["../../../../src/form/utils/find-start-page.ts"],"sourcesContent":["import { type FormDefinition } from '~/src/form/form-definition/types.js'\n\n/**\n * Find correct start page for form definition\n *\n * Computed by counting the next paths in a form definition and returning page\n * that doesn't have a reference, meaning it must be a start page / root node\n *\n * It's possibe for forms to be malformed and have multiple start pages while\n * they're being developed. This may be valid during development, but in this\n * scenario we can't detect the real start page and we require the user to fix\n * this themselves\n * @param data - Form definition\n */\nexport function findStartPage(data: FormDefinition) {\n const { pages } = data\n\n // Get a unique list of all pages that are linked to by other pages\n const pageReferences = new Set(\n pages.flatMap((page) => page.next?.map((next) => next.path))\n )\n\n // For each page on the form, work out if it's referenced by another page\n // those that aren't referenced must be start pages\n const startPages = pages\n .map((page) => page.path)\n .filter((page) => !pageReferences.has(page))\n\n // We can only set the start page if there is a single one, else the user has made an error\n if (startPages.length === 1) {\n return startPages[0]\n }\n}\n"],"mappings":"AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,aAAaA,CAACC,IAAoB,EAAE;EAClD,MAAM;IAAEC;EAAM,CAAC,GAAGD,IAAI;;EAEtB;EACA,MAAME,cAAc,GAAG,IAAIC,GAAG,CAC5BF,KAAK,CAACG,OAAO,CAAEC,IAAI,IAAKA,IAAI,CAACC,IAAI,EAAEC,GAAG,CAAED,IAAI,IAAKA,IAAI,CAACE,IAAI,CAAC,CAC7D,CAAC;;EAED;EACA;EACA,MAAMC,UAAU,GAAGR,KAAK,CACrBM,GAAG,CAAEF,IAAI,IAAKA,IAAI,CAACG,IAAI,CAAC,CACxBE,MAAM,CAAEL,IAAI,IAAK,CAACH,cAAc,CAACS,GAAG,CAACN,IAAI,CAAC,CAAC;;EAE9C;EACA,IAAII,UAAU,CAACG,MAAM,KAAK,CAAC,EAAE;IAC3B,OAAOH,UAAU,CAAC,CAAC,CAAC;EACtB;AACF","ignoreList":[]}
@@ -0,0 +1,3 @@
1
+ export { findStartPage } from "./find-start-page.js";
2
+ export { updateStartPage } from "./update-start-page.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["findStartPage","updateStartPage"],"sources":["../../../../src/form/utils/index.ts"],"sourcesContent":["export { findStartPage } from '~/src/form/utils/find-start-page.js'\nexport { updateStartPage } from '~/src/form/utils/update-start-page.js'\n"],"mappings":"AAAA,SAASA,aAAa;AACtB,SAASC,eAAe","ignoreList":[]}
@@ -0,0 +1,30 @@
1
+ import { findStartPage } from "./find-start-page.js";
2
+
3
+ /**
4
+ * Update start page when pages are modified
5
+ * @param data - Form definition
6
+ */
7
+ export function updateStartPage(data) {
8
+ const startPage = findStartPage(data);
9
+
10
+ // Update start page if incorrect
11
+ if (startPage && (!data.startPage || data.startPage !== startPage)) {
12
+ return {
13
+ ...data,
14
+ startPage
15
+ };
16
+ }
17
+
18
+ // Remove start page if no pages exist
19
+ if (!startPage && 'startPage' in data) {
20
+ const updated = {
21
+ ...data
22
+ };
23
+
24
+ // Remove without modifying original
25
+ delete updated.startPage;
26
+ return updated;
27
+ }
28
+ return data;
29
+ }
30
+ //# sourceMappingURL=update-start-page.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"update-start-page.js","names":["findStartPage","updateStartPage","data","startPage","updated"],"sources":["../../../../src/form/utils/update-start-page.ts"],"sourcesContent":["import { type FormDefinition } from '~/src/form/form-definition/types.js'\nimport { findStartPage } from '~/src/form/utils/find-start-page.js'\n\n/**\n * Update start page when pages are modified\n * @param data - Form definition\n */\nexport function updateStartPage(data: FormDefinition) {\n const startPage = findStartPage(data)\n\n // Update start page if incorrect\n if (startPage && (!data.startPage || data.startPage !== startPage)) {\n return { ...data, startPage }\n }\n\n // Remove start page if no pages exist\n if (!startPage && 'startPage' in data) {\n const updated = { ...data }\n\n // Remove without modifying original\n delete updated.startPage\n return updated\n }\n\n return data\n}\n"],"mappings":"AACA,SAASA,aAAa;;AAEtB;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAACC,IAAoB,EAAE;EACpD,MAAMC,SAAS,GAAGH,aAAa,CAACE,IAAI,CAAC;;EAErC;EACA,IAAIC,SAAS,KAAK,CAACD,IAAI,CAACC,SAAS,IAAID,IAAI,CAACC,SAAS,KAAKA,SAAS,CAAC,EAAE;IAClE,OAAO;MAAE,GAAGD,IAAI;MAAEC;IAAU,CAAC;EAC/B;;EAEA;EACA,IAAI,CAACA,SAAS,IAAI,WAAW,IAAID,IAAI,EAAE;IACrC,MAAME,OAAO,GAAG;MAAE,GAAGF;IAAK,CAAC;;IAE3B;IACA,OAAOE,OAAO,CAACD,SAAS;IACxB,OAAOC,OAAO;EAChB;EAEA,OAAOF,IAAI;AACb","ignoreList":[]}
@@ -1,6 +1,7 @@
1
1
  export * from "./data-model/index.js";
2
2
  export * from "./form/form-definition/index.js";
3
3
  export * from "./form/form-metadata/index.js";
4
+ export * from "./form/utils/index.js";
4
5
  export * from "./components/index.js";
5
6
  export * from "./conditions/index.js";
6
7
  export * from "./utils/helpers.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["export * from '~/src/data-model/index.js'\nexport * from '~/src/form/form-definition/index.js'\nexport * from '~/src/form/form-metadata/index.js'\nexport * from '~/src/components/index.js'\nexport * from '~/src/conditions/index.js'\nexport * from '~/src/utils/helpers.js'\nexport type * from '~/src/components/types.js'\nexport type * from '~/src/conditions/types.js'\nexport type * from '~/src/form/form-definition/types.js'\nexport type * from '~/src/form/form-metadata/types.js'\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["export * from '~/src/data-model/index.js'\nexport * from '~/src/form/form-definition/index.js'\nexport * from '~/src/form/form-metadata/index.js'\nexport * from '~/src/form/utils/index.js'\nexport * from '~/src/components/index.js'\nexport * from '~/src/conditions/index.js'\nexport * from '~/src/utils/helpers.js'\nexport type * from '~/src/components/types.js'\nexport type * from '~/src/conditions/types.js'\nexport type * from '~/src/form/form-definition/types.js'\nexport type * from '~/src/form/form-metadata/types.js'\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
@@ -0,0 +1,15 @@
1
+ import { type FormDefinition } from '../../form/form-definition/types.js';
2
+ /**
3
+ * Find correct start page for form definition
4
+ *
5
+ * Computed by counting the next paths in a form definition and returning page
6
+ * that doesn't have a reference, meaning it must be a start page / root node
7
+ *
8
+ * It's possibe for forms to be malformed and have multiple start pages while
9
+ * they're being developed. This may be valid during development, but in this
10
+ * scenario we can't detect the real start page and we require the user to fix
11
+ * this themselves
12
+ * @param data - Form definition
13
+ */
14
+ export declare function findStartPage(data: FormDefinition): string | undefined;
15
+ //# sourceMappingURL=find-start-page.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find-start-page.d.ts","sourceRoot":"","sources":["../../../../src/form/utils/find-start-page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,qCAAqC,CAAA;AAEzE;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,cAAc,sBAkBjD"}
@@ -0,0 +1,3 @@
1
+ export { findStartPage } from '../../form/utils/find-start-page.js';
2
+ export { updateStartPage } from '../../form/utils/update-start-page.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/form/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAA;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAA"}
@@ -0,0 +1,7 @@
1
+ import { type FormDefinition } from '../../form/form-definition/types.js';
2
+ /**
3
+ * Update start page when pages are modified
4
+ * @param data - Form definition
5
+ */
6
+ export declare function updateStartPage(data: FormDefinition): FormDefinition;
7
+ //# sourceMappingURL=update-start-page.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"update-start-page.d.ts","sourceRoot":"","sources":["../../../../src/form/utils/update-start-page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,qCAAqC,CAAA;AAGzE;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,cAAc,kBAkBnD"}
@@ -1,6 +1,7 @@
1
1
  export * from './data-model/index.js';
2
2
  export * from './form/form-definition/index.js';
3
3
  export * from './form/form-metadata/index.js';
4
+ export * from './form/utils/index.js';
4
5
  export * from './components/index.js';
5
6
  export * from './conditions/index.js';
6
7
  export * from './utils/helpers.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAA;AACzC,cAAc,qCAAqC,CAAA;AACnD,cAAc,mCAAmC,CAAA;AACjD,cAAc,2BAA2B,CAAA;AACzC,cAAc,2BAA2B,CAAA;AACzC,cAAc,wBAAwB,CAAA;AACtC,mBAAmB,2BAA2B,CAAA;AAC9C,mBAAmB,2BAA2B,CAAA;AAC9C,mBAAmB,qCAAqC,CAAA;AACxD,mBAAmB,mCAAmC,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAA;AACzC,cAAc,qCAAqC,CAAA;AACnD,cAAc,mCAAmC,CAAA;AACjD,cAAc,2BAA2B,CAAA;AACzC,cAAc,2BAA2B,CAAA;AACzC,cAAc,2BAA2B,CAAA;AACzC,cAAc,wBAAwB,CAAA;AACtC,mBAAmB,2BAA2B,CAAA;AAC9C,mBAAmB,2BAA2B,CAAA;AAC9C,mBAAmB,qCAAqC,CAAA;AACxD,mBAAmB,mCAAmC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/forms-model",
3
- "version": "3.0.201",
3
+ "version": "3.0.203",
4
4
  "description": "A hapi plugin providing the model for Defra forms",
5
5
  "homepage": "https://github.com/DEFRA/forms-designer/tree/main/model#readme",
6
6
  "repository": {
@@ -0,0 +1,33 @@
1
+ import { type FormDefinition } from '~/src/form/form-definition/types.js'
2
+
3
+ /**
4
+ * Find correct start page for form definition
5
+ *
6
+ * Computed by counting the next paths in a form definition and returning page
7
+ * that doesn't have a reference, meaning it must be a start page / root node
8
+ *
9
+ * It's possibe for forms to be malformed and have multiple start pages while
10
+ * they're being developed. This may be valid during development, but in this
11
+ * scenario we can't detect the real start page and we require the user to fix
12
+ * this themselves
13
+ * @param data - Form definition
14
+ */
15
+ export function findStartPage(data: FormDefinition) {
16
+ const { pages } = data
17
+
18
+ // Get a unique list of all pages that are linked to by other pages
19
+ const pageReferences = new Set(
20
+ pages.flatMap((page) => page.next?.map((next) => next.path))
21
+ )
22
+
23
+ // For each page on the form, work out if it's referenced by another page
24
+ // those that aren't referenced must be start pages
25
+ const startPages = pages
26
+ .map((page) => page.path)
27
+ .filter((page) => !pageReferences.has(page))
28
+
29
+ // We can only set the start page if there is a single one, else the user has made an error
30
+ if (startPages.length === 1) {
31
+ return startPages[0]
32
+ }
33
+ }
@@ -0,0 +1,2 @@
1
+ export { findStartPage } from '~/src/form/utils/find-start-page.js'
2
+ export { updateStartPage } from '~/src/form/utils/update-start-page.js'
@@ -0,0 +1,26 @@
1
+ import { type FormDefinition } from '~/src/form/form-definition/types.js'
2
+ import { findStartPage } from '~/src/form/utils/find-start-page.js'
3
+
4
+ /**
5
+ * Update start page when pages are modified
6
+ * @param data - Form definition
7
+ */
8
+ export function updateStartPage(data: FormDefinition) {
9
+ const startPage = findStartPage(data)
10
+
11
+ // Update start page if incorrect
12
+ if (startPage && (!data.startPage || data.startPage !== startPage)) {
13
+ return { ...data, startPage }
14
+ }
15
+
16
+ // Remove start page if no pages exist
17
+ if (!startPage && 'startPage' in data) {
18
+ const updated = { ...data }
19
+
20
+ // Remove without modifying original
21
+ delete updated.startPage
22
+ return updated
23
+ }
24
+
25
+ return data
26
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from '~/src/data-model/index.js'
2
2
  export * from '~/src/form/form-definition/index.js'
3
3
  export * from '~/src/form/form-metadata/index.js'
4
+ export * from '~/src/form/utils/index.js'
4
5
  export * from '~/src/components/index.js'
5
6
  export * from '~/src/conditions/index.js'
6
7
  export * from '~/src/utils/helpers.js'