Skip to content

Commit

Permalink
Merge pull request #244 from AtlasOfLivingAustralia/feature/issue243
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala authored Apr 18, 2024
2 parents acd85b5 + 386e0f2 commit 8189cec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
22 changes: 21 additions & 1 deletion grails-app/assets/javascripts/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,27 @@ function orEmptyArray(v) {
return _.findWhere(list, obj);
};

parser.functions.deepEquals = function(value1, value2) {
parser.functions.deepEquals = function(value1, value2, isSortArray) {
// set isSortArray to true if content of array is important and not the order i.e. [1,2,3] == [3,2,1]
isSortArray = isSortArray || false;
// Sort arrays in nested objects to ensure that lodash compares arrays correctly
function sortArraysInObject(obj) {
if (_.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;
}

if (isSortArray) {
sortArraysInObject(value1);
sortArraysInObject(value2);
}

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

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

});

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

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

// enable array sorting
var data = {
a: [1, 2, 3],
b: [3, 2, 1]
};
var result = ecodata.forms.expressionEvaluator.evaluateBoolean("deepEquals(a, b, true)", 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, true)", 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 8189cec

Please sign in to comment.