Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- fixes unsorted array comparison
  • Loading branch information
temi committed Apr 16, 2024
1 parent acd85b5 commit a180af4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions grails-app/assets/javascripts/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ function orEmptyArray(v) {
};

parser.functions.deepEquals = function(value1, value2) {
// Sort arrays in nested objects to ensure that lodash compares arrays correctly
function sortArraysInObject(obj) {
if (Array.isArray(obj)) {
obj.sort();
} else if (typeof obj === 'object' && obj !== null) {
for (var key in obj) {
sortArraysInObject(obj[key]); // Recursively call to sort arrays in nested objects
}
}

return obj;
}

sortArraysInObject(value1);
sortArraysInObject(value2);
return _.isEqual(value1, value2);
};

Expand Down
23 changes: 23 additions & 0 deletions src/test/js/spec/ExpressionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,27 @@ describe("Expression Spec", function () {

});

it("Should be able to compare two unsorted arrays", function() {
var data = {
a: [1, 2, 3],
b: [3, 2, 1]
};
var result = ecodata.forms.expressionEvaluator.evaluateBoolean("deepEquals(a, b)", data);
expect(result).toEqual(true);

data = {
a: {c: [1, 2, 3]},
b: {c: [3, 2, 1]}
};
var result = ecodata.forms.expressionEvaluator.evaluateBoolean("deepEquals(a, b)", data);
expect(result).toEqual(true);

data = {
a: {c: [1, 2]},
b: {c: [3, 2, 1]}
};
var result = ecodata.forms.expressionEvaluator.evaluateBoolean("deepEquals(a, b)", data);
expect(result).toEqual(false);
})

});

0 comments on commit a180af4

Please sign in to comment.