@sprucelabs/spruce-form-utils 10.2.75 → 10.2.78

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.
@@ -21,19 +21,28 @@ function validateOptions(pages, values) {
21
21
  }
22
22
  const formCompletionCalculator = {
23
23
  calculate(pages, values) {
24
- var _a, _b;
24
+ var _a, _b, _c, _d;
25
25
  validateOptions(pages, values);
26
26
  let totalFields = 0;
27
27
  for (const page of pages) {
28
+ const fields = (_a = page.schema.fields) !== null && _a !== void 0 ? _a : {};
28
29
  for (const section of page.sections) {
29
- totalFields += (_b = (_a = section.fields) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
30
+ for (const field of (_b = section.fields) !== null && _b !== void 0 ? _b : []) {
31
+ const f = (_d = fields[(_c = field.name) !== null && _c !== void 0 ? _c : field]) !== null && _d !== void 0 ? _d : {};
32
+ if (f.isRequired) {
33
+ totalFields++;
34
+ }
35
+ }
30
36
  }
31
37
  }
38
+ if (totalFields === 0) {
39
+ return 1;
40
+ }
32
41
  let totalAnswered = 0;
33
42
  for (const value of values) {
34
43
  totalAnswered += Object.keys(value !== null && value !== void 0 ? value : {}).filter((k) => !!value[k]).length;
35
44
  }
36
- return totalAnswered / totalFields;
45
+ return parseFloat((totalAnswered / totalFields).toFixed(2));
37
46
  },
38
47
  };
39
48
  export default formCompletionCalculator;
@@ -23,19 +23,28 @@ function validateOptions(pages, values) {
23
23
  }
24
24
  const formCompletionCalculator = {
25
25
  calculate(pages, values) {
26
- var _a, _b;
26
+ var _a, _b, _c, _d;
27
27
  validateOptions(pages, values);
28
28
  let totalFields = 0;
29
29
  for (const page of pages) {
30
+ const fields = (_a = page.schema.fields) !== null && _a !== void 0 ? _a : {};
30
31
  for (const section of page.sections) {
31
- totalFields += (_b = (_a = section.fields) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
32
+ for (const field of (_b = section.fields) !== null && _b !== void 0 ? _b : []) {
33
+ const f = (_d = fields[(_c = field.name) !== null && _c !== void 0 ? _c : field]) !== null && _d !== void 0 ? _d : {};
34
+ if (f.isRequired) {
35
+ totalFields++;
36
+ }
37
+ }
32
38
  }
33
39
  }
40
+ if (totalFields === 0) {
41
+ return 1;
42
+ }
34
43
  let totalAnswered = 0;
35
44
  for (const value of values) {
36
45
  totalAnswered += Object.keys(value !== null && value !== void 0 ? value : {}).filter((k) => !!value[k]).length;
37
46
  }
38
- return totalAnswered / totalFields;
47
+ return parseFloat((totalAnswered / totalFields).toFixed(2));
39
48
  },
40
49
  };
41
50
  exports.default = formCompletionCalculator;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sprucelabs/spruce-form-utils",
3
3
  "description": "Support for working with forms and Sprucebot. 📄",
4
- "version": "10.2.75",
4
+ "version": "10.2.78",
5
5
  "skill": {
6
6
  "namespace": "forms"
7
7
  },