Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- added config option to specify if sorting is required
- refactored code
  • Loading branch information
temi committed Apr 18, 2024
1 parent a180af4 commit 386e0f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
13 changes: 9 additions & 4 deletions grails-app/assets/javascripts/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,12 @@ 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 (Array.isArray(obj)) {
if (_.isArray(obj)) {
obj.sort();
} else if (typeof obj === 'object' && obj !== null) {
for (var key in obj) {
Expand All @@ -310,8 +312,11 @@ function orEmptyArray(v) {
return obj;
}

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

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

Expand Down
20 changes: 18 additions & 2 deletions src/test/js/spec/ExpressionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,35 @@ describe("Expression Spec", function () {

});

it("Should be able to compare two unsorted arrays", 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(true);
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 = {
Expand Down

0 comments on commit 386e0f2

Please sign in to comment.