@sprucelabs/spruce-form-utils 10.2.77 → 10.2.80

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,33 @@ 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
+ const requiredFields = [];
26
27
  let totalFields = 0;
27
28
  for (const page of pages) {
29
+ const fields = (_a = page.schema.fields) !== null && _a !== void 0 ? _a : {};
28
30
  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;
31
+ for (const field of (_b = section.fields) !== null && _b !== void 0 ? _b : []) {
32
+ const fieldKey = (_c = field.name) !== null && _c !== void 0 ? _c : field;
33
+ const f = (_d = fields[fieldKey]) !== null && _d !== void 0 ? _d : {};
34
+ if (f.isRequired) {
35
+ totalFields++;
36
+ requiredFields.push(fieldKey);
37
+ }
38
+ }
30
39
  }
31
40
  }
41
+ if (totalFields === 0) {
42
+ return 1;
43
+ }
32
44
  let totalAnswered = 0;
33
45
  for (const value of values) {
34
- totalAnswered += Object.keys(value !== null && value !== void 0 ? value : {}).filter((k) => !!value[k]).length;
46
+ totalAnswered += Object.keys(value !== null && value !== void 0 ? value : {}).filter((k) => {
47
+ return !!value[k] && requiredFields.indexOf(k) !== -1;
48
+ }).length;
35
49
  }
36
- return totalAnswered / totalFields;
50
+ return parseFloat((totalAnswered / totalFields).toFixed(2));
37
51
  },
38
52
  };
39
53
  export default formCompletionCalculator;
@@ -23,19 +23,33 @@ 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
+ const requiredFields = [];
28
29
  let totalFields = 0;
29
30
  for (const page of pages) {
31
+ const fields = (_a = page.schema.fields) !== null && _a !== void 0 ? _a : {};
30
32
  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;
33
+ for (const field of (_b = section.fields) !== null && _b !== void 0 ? _b : []) {
34
+ const fieldKey = (_c = field.name) !== null && _c !== void 0 ? _c : field;
35
+ const f = (_d = fields[fieldKey]) !== null && _d !== void 0 ? _d : {};
36
+ if (f.isRequired) {
37
+ totalFields++;
38
+ requiredFields.push(fieldKey);
39
+ }
40
+ }
32
41
  }
33
42
  }
43
+ if (totalFields === 0) {
44
+ return 1;
45
+ }
34
46
  let totalAnswered = 0;
35
47
  for (const value of values) {
36
- totalAnswered += Object.keys(value !== null && value !== void 0 ? value : {}).filter((k) => !!value[k]).length;
48
+ totalAnswered += Object.keys(value !== null && value !== void 0 ? value : {}).filter((k) => {
49
+ return !!value[k] && requiredFields.indexOf(k) !== -1;
50
+ }).length;
37
51
  }
38
- return totalAnswered / totalFields;
52
+ return parseFloat((totalAnswered / totalFields).toFixed(2));
39
53
  },
40
54
  };
41
55
  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.77",
4
+ "version": "10.2.80",
5
5
  "skill": {
6
6
  "namespace": "forms"
7
7
  },