@ukhomeoffice/cop-react-form-renderer 6.14.6 → 6.14.8

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.
@@ -20,6 +20,26 @@ const getComparisonValue = (condition, data) => {
20
20
  return condition.value;
21
21
  };
22
22
 
23
+ /**
24
+ * Processes a value based on its type.
25
+ * - If it's an array, returns its length.
26
+ * - If it's a string, parses it as a float.
27
+ * - If it's a number, returns it as is.
28
+ * - Returns `undefined` for other types.
29
+ *
30
+ * @param {any} value - The value to process.
31
+ * @returns {number | undefined} - The processed value or `undefined` if not applicable.
32
+ */
33
+ const getProcessedValue = obj => {
34
+ if (Array.isArray(obj)) return obj.length;
35
+ if (typeof obj === "string") {
36
+ const cleaned = obj.replace(/,/g, ''); // Remove all commas
37
+ return Number.isNaN(cleaned) ? null : parseFloat(cleaned);
38
+ }
39
+ if (typeof obj === "number") return obj;
40
+ return null;
41
+ };
42
+
23
43
  /**
24
44
  * Looks at a condition object to see if the supplied value meets it.
25
45
  * The condition object contains an `op` property, along with a comparator
@@ -66,21 +86,15 @@ const meetsCondition = (condition, value, data) => {
66
86
  }
67
87
  case '<':
68
88
  {
69
- if (!value || !compare) {
70
- return false;
71
- }
72
- const valFloat = Array.isArray(value) ? value.length : parseFloat(value.replace(',', ''));
73
- const compareFloat = parseFloat(compare.replace(',', ''));
74
- return valFloat < compareFloat;
89
+ const valFloat = getProcessedValue(value);
90
+ const compareFloat = getProcessedValue(compare);
91
+ return valFloat != null && compareFloat != null && valFloat < compareFloat;
75
92
  }
76
93
  case '>':
77
94
  {
78
- if (!value || !compare) {
79
- return false;
80
- }
81
- const valFloat = Array.isArray(value) ? value.length : parseFloat(value.replace(/,/g, ''));
82
- const compareFloat = parseFloat(compare.replace(/,/g, ''));
83
- return valFloat > compareFloat;
95
+ const valFloat = getProcessedValue(value);
96
+ const compareFloat = getProcessedValue(compare);
97
+ return valFloat != null && compareFloat != null && valFloat > compareFloat;
84
98
  }
85
99
  case 'contains':
86
100
  {
@@ -551,6 +551,27 @@ describe('utils.Condition.meetsCondition', () => {
551
551
  };
552
552
  expect((0, _meetsCondition.default)(CONDITION, '10,000,000')).toBeTruthy();
553
553
  });
554
+ it('greater than should handle value is 0', () => {
555
+ const CONDITION = {
556
+ op: '>',
557
+ value: 0
558
+ };
559
+ expect((0, _meetsCondition.default)(CONDITION, 1)).toBeTruthy();
560
+ });
561
+ it('greater than should handle compear is 0', () => {
562
+ const CONDITION = {
563
+ op: '<',
564
+ value: 1
565
+ };
566
+ expect((0, _meetsCondition.default)(CONDITION, 0)).toBeTruthy();
567
+ });
568
+ it('greater than should handle array is 0', () => {
569
+ const CONDITION = {
570
+ op: '>',
571
+ value: 1
572
+ };
573
+ expect((0, _meetsCondition.default)(CONDITION, ["A", "B", "C"])).toBeTruthy();
574
+ });
554
575
  });
555
576
  describe('operator includes', () => {
556
577
  it('should accept a string that exists within the value', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "6.14.6",
3
+ "version": "6.14.8",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
@@ -16,7 +16,7 @@
16
16
  "yalc-publish": "yarn compile-with-maps && cp -r src dist/src && yalc publish --push"
17
17
  },
18
18
  "dependencies": {
19
- "@ukhomeoffice/cop-react-components": "4.7.9",
19
+ "@ukhomeoffice/cop-react-components": "4.7.10",
20
20
  "axios": "^0.23.0",
21
21
  "dayjs": "^1.11.0",
22
22
  "govuk-frontend": "^5.0.0",